Class: Badgeapi::BadgeapiObject

Inherits:
Object
  • Object
show all
Defined in:
lib/badgeapi/badgeapi_object.rb

Direct Known Subclasses

Badge, Collection, Recipient

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all(params = {}) ⇒ Object



95
96
97
# File 'lib/badgeapi/badgeapi_object.rb', line 95

def all params = {}
	request "get", "#{Badgeapi.api_url}/#{collection_path}", params
end

.collection_nameObject Also known as: collection_path



8
9
10
# File 'lib/badgeapi/badgeapi_object.rb', line 8

def collection_name
	name.demodulize.pluralize.underscore
end

.create(params = {}) ⇒ Object



99
100
101
# File 'lib/badgeapi/badgeapi_object.rb', line 99

def create params={}
	request "post", "#{Badgeapi.api_url}/#{collection_path}", member_name => params
end

.destroy(id) ⇒ Object



107
108
109
# File 'lib/badgeapi/badgeapi_object.rb', line 107

def destroy(id)
	request "delete", "#{Badgeapi.api_url}/#{collection_path}/#{id}"
end

.find(id, params = {}) ⇒ Object



91
92
93
# File 'lib/badgeapi/badgeapi_object.rb', line 91

def find id, params ={}
	request "get", "#{Badgeapi.api_url}/#{collection_path}/#{id}", params
end

.from_response(response) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/badgeapi/badgeapi_object.rb', line 27

def from_response response
	attributes = JSON.parse(response.body)

	if attributes.include?("error")
		handle_api_error attributes
	else
		if attributes.class == Array
			attributes.map do |attributes|
				map_json_to_object(attributes)
			end
		else
			map_json_to_object attributes
		end
	end
end

.map_json_to_object(attributes) ⇒ Object



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

def map_json_to_object attributes
	if attributes['object'] != nil
		record = object_classes.fetch(attributes['object'].singularize).new
	else
		record = new
	end

	attributes.each do |name, value|
		if object_classes.has_key?(name) || object_classes.has_key?(name.singularize)
			child = map_related_object object_classes.fetch(name.singularize), value
			record.instance_variable_set "@#{name}", child
		else
			record.instance_variable_set "@#{name}", value
		end
	end
	record
end


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

def map_related_object object, attributes
	if attributes.class == Array
		attributes.map do |attributes|
			map_related_object object, attributes
		end
	else
		record = object.new
		attributes.each do |name, value|
			if value.class == Array && value.count > 0
				if object_classes.has_key?(name) || object_classes.has_key?(name.singularize)
					child = map_related_object object_classes.fetch(name.singularize), value
					record.instance_variable_set "@#{name}", child
				end
			else
				record.instance_variable_set "@#{name}", value
			end
		end
		record
	end
end

.member_nameObject



13
14
15
# File 'lib/badgeapi/badgeapi_object.rb', line 13

def member_name
	name.demodulize.underscore
end

.object_classesObject



43
44
45
46
47
48
49
50
# File 'lib/badgeapi/badgeapi_object.rb', line 43

def object_classes
	@object_classes ||= {
		'recipient' => Recipient,
		'collection' => Collection,
		'badge'=> Badge,
		'required_badge'=>Badge
	}
end

.request(method, url, params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/badgeapi/badgeapi_object.rb', line 17

def request method, url, params={}
	#connection = Faraday.new(:ssl => {:verify => false})
	connection = Faraday.new(:ssl => {
		:ca_file => Badgeapi.ssl_ca_cert
	})

	connection.token_auth(Badgeapi.api_key)
	from_response connection.send(method, url, params)
end

.update(id, params = {}) ⇒ Object



103
104
105
# File 'lib/badgeapi/badgeapi_object.rb', line 103

def update id, params = {}
	request "patch", "#{Badgeapi.api_url}/#{collection_path}/#{id}", member_name => params
end

Instance Method Details

#inspectObject



113
114
115
116
# File 'lib/badgeapi/badgeapi_object.rb', line 113

def inspect
	id_as_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
	"#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_as_string}> JSON: " + self.to_json
end

#saveObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/badgeapi/badgeapi_object.rb', line 118

def save
	# Remove params that cannot be saved as they are not permitted through strong_params on api
	params = JSON.parse(self.to_json)

	params.delete("id")
	params.delete("created_at")
	params.delete("updated_at")
	params.delete("points")
	params.delete("total_points_available")
	params.delete("badge_count")
	params.delete("object")

	self.class.request "patch", "#{Badgeapi.api_url}/#{self.class.collection_path}/#{id}", self.class.member_name => params
end