Class: PrintfulAPI::APIResource

Inherits:
Object
  • Object
show all
Defined in:
lib/printful_api/api_resource.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.api_attribute_listObject

Returns the value of attribute api_attribute_list.



6
7
8
# File 'lib/printful_api/api_resource.rb', line 6

def api_attribute_list
  @api_attribute_list
end

Instance Attribute Details

#raw_dataObject

Returns the value of attribute raw_data.



4
5
6
# File 'lib/printful_api/api_resource.rb', line 4

def raw_data
  @raw_data
end

Class Method Details

.api_attributes(*args) ⇒ Object



9
10
11
12
13
# File 'lib/printful_api/api_resource.rb', line 9

def self.api_attributes( *args )
	self.api_attribute_list = (self.api_attribute_list || []).concat(args)

	attr_accessor *args
end

.belongs_to(attribute_name, args = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/printful_api/api_resource.rb', line 31

def self.belongs_to( attribute_name, args = {} )
	args[:class] = args[:class] if args[:class].is_a? String
	args[:class] ||= "PrintfulAPI::#{APIResource.camelize( attribute_name.to_s )}"
	args[:foreign_key] ||= "#{attribute_name}_id"

	self.api_attributes *[attribute_name.to_sym]

	define_method("#{attribute_name}") do
		attribute_value = self.instance_variable_get( "@#{attribute_name}" )
		attribute_value ||= args[:class].new.get( args[:foreign_key] ) if self.respond_to?( args[:foreign_key] )
		self.instance_variable_set( "@#{attribute_name}", attribute_value )
	end

	define_method("#{attribute_name}=") do |data|
		if data.is_a?(PrintfulAPI::APIResource)
			self.instance_variable_set("@#{attribute_name}", data)
		else
			model = args[:class].constantize.new.load_data(data) if data.present?
			self.instance_variable_set( "@#{attribute_name}", model )
		end
	end
end

.camelize(str) ⇒ Object



94
95
96
# File 'lib/printful_api/api_resource.rb', line 94

def self.camelize( str )
	str.split('_').map {|w| w.capitalize}.join
end

.has_many(attribute_name, args = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/printful_api/api_resource.rb', line 15

def self.has_many( attribute_name, args = {} )
	args[:class] = args[:class] if args[:class].is_a? String
	args[:class] ||= "PrintfulAPI::#{APIResource.singularize( APIResource.camelize(attribute_name.to_s ) )}"

	self.api_attributes *[attribute_name.to_sym]

      	define_method("#{attribute_name}=") do |array|
		if array.present?
			array = array.collect do |data|
				args[:class].constantize.new.load_data(data)
			end
		end
		self.instance_variable_set("@#{attribute_name}", array)
	end
end

.singularize(str) ⇒ Object



98
99
100
101
# File 'lib/printful_api/api_resource.rb', line 98

def self.singularize( str )
	str = str[0..-2] if str[-1] == 's'
	str
end

Instance Method Details

#load_data(data) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/printful_api/api_resource.rb', line 54

def load_data( data )
	self.raw_data = data
	data.each do |key,value|

		if self.respond_to? "#{key}="
			self.send("#{key}=", value)
		else
			puts "Accessor doesn't exist #{self.class.name}\##{"#{key}="}"
		end

	end

	self

end

#to_hObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/printful_api/api_resource.rb', line 70

def to_h()
	hash = {}
	self.class.api_attribute_list.each do |attribute_name|
		value = self.send(attribute_name)
		if value.is_a? Array

			hash[attribute_name] = value.collect do |array_item|
				if array_item.is_a? APIResource
					hash[attribute_name] = array_item.to_h
				else
					hash[attribute_name] = array_item
				end
			end

		elsif value.is_a? APIResource
			hash[attribute_name] = value.to_h
		else
			hash[attribute_name] = value
		end
	end

	hash
end