Class: Badgeapi::BadgeapiObject
- Inherits:
-
Object
- Object
- Badgeapi::BadgeapiObject
show all
- Defined in:
- lib/badgeapi/badgeapi_object.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.all(params = {}) ⇒ Object
106
107
108
|
# File 'lib/badgeapi/badgeapi_object.rb', line 106
def all params = {}
request "get", "#{Badgeapi.api_url}/#{collection_path}", params
end
|
.attribute_a_badge_object?(name) ⇒ Boolean
94
95
96
97
98
99
100
|
# File 'lib/badgeapi/badgeapi_object.rb', line 94
def attribute_a_badge_object? name
if object_classes.has_key?(name) || object_classes.has_key?(name.singularize)
true
else
false
end
end
|
.collection_name ⇒ Object
Also known as:
collection_path
5
6
7
|
# File 'lib/badgeapi/badgeapi_object.rb', line 5
def collection_name
name.demodulize.pluralize.underscore
end
|
.create(params = {}) ⇒ Object
110
111
112
|
# File 'lib/badgeapi/badgeapi_object.rb', line 110
def create params = {}
request "post", "#{Badgeapi.api_url}/#{collection_path}", member_name => params
end
|
.destroy(id) ⇒ Object
118
119
120
|
# File 'lib/badgeapi/badgeapi_object.rb', line 118
def destroy(id)
request "delete", "#{Badgeapi.api_url}/#{collection_path}/#{id}"
end
|
.find(id, params = {}) ⇒ Object
102
103
104
|
# File 'lib/badgeapi/badgeapi_object.rb', line 102
def find id, params = {}
request "get", "#{Badgeapi.api_url}/#{collection_path}/#{id}", params
end
|
.from_response(response) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/badgeapi/badgeapi_object.rb', line 21
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 |subattributes|
map_json_to_object(subattributes)
end
else
map_json_to_object attributes
end
end
end
|
.handle_api_error(error) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/badgeapi/badgeapi_object.rb', line 149
def self.handle_api_error error
error_object = error['error']
case error_object["type"]
when "invalid_request_error"
raise InvalidRequestError.new(error_object["message"], error_object["status"], error)
when "api_error"
raise APIError.new(error_object["message"], error_object["status"], error)
else
raise APIError.new("Unknown error tyep #{error_object['type']}", error_object["status"], error)
end
end
|
.map_instant_variables_to_child_records(attributes, record) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/badgeapi/badgeapi_object.rb', line 81
def map_instant_variables_to_child_records attributes, record
attributes.each do |name, value|
if value.class == Array && value.count > 0
if attribute_a_badge_object? name
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
end
|
.map_instant_variables_to_record(attributes, record) ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/badgeapi/badgeapi_object.rb', line 57
def map_instant_variables_to_record attributes, record
attributes.each do |name, value|
if attribute_a_badge_object? name
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
end
|
.map_json_to_object(attributes) ⇒ Object
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/badgeapi/badgeapi_object.rb', line 46
def map_json_to_object attributes
if attributes['object'].nil?
record = new
else
record = object_classes.fetch(attributes['object'].singularize).new
end
map_instant_variables_to_record attributes, record
record
end
|
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/badgeapi/badgeapi_object.rb', line 68
def map_related_object object, attributes
if attributes.class == Array
attributes.map do |subattributes|
map_related_object object, subattributes
end
else
record = object.new
map_instant_variables_to_child_records attributes, record
record
end
end
|
.member_name ⇒ Object
10
11
12
|
# File 'lib/badgeapi/badgeapi_object.rb', line 10
def member_name
name.demodulize.underscore
end
|
.object_classes ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/badgeapi/badgeapi_object.rb', line 37
def object_classes
@object_classes ||= {
'recipient' => Recipient,
'collection' => Collection,
'badge' => Badge,
'required_badge' => Badge
}
end
|
.request(method, url, params = {}) ⇒ Object
14
15
16
17
18
19
|
# File 'lib/badgeapi/badgeapi_object.rb', line 14
def request method, url, params = {}
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
114
115
116
|
# File 'lib/badgeapi/badgeapi_object.rb', line 114
def update id, params = {}
request "patch", "#{Badgeapi.api_url}/#{collection_path}/#{id}", member_name => params
end
|
Instance Method Details
#inspect ⇒ Object
123
124
125
126
|
# File 'lib/badgeapi/badgeapi_object.rb', line 123
def inspect
id_as_string = (respond_to?(:id) && !id.nil?) ? " id=#{id}" : ""
"#<#{self.class}:0x#{object_id.to_s(16)}#{id_as_string}> JSON: " + to_json
end
|
#remove_read_only_params ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/badgeapi/badgeapi_object.rb', line 128
def remove_read_only_params
params = JSON.parse(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")
end
|
#save ⇒ Object
141
142
143
144
145
146
147
|
# File 'lib/badgeapi/badgeapi_object.rb', line 141
def save
params = remove_read_only_params
self.class.request "patch", "#{Badgeapi.api_url}/#{self.class.collection_path}/#{id}",
self.class.member_name => params
end
|