badgeapi 0.3.10 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.rubocop.yml +147 -0
- data/README.md +44 -12
- data/Rakefile +24 -11
- data/badgeapi.gemspec +23 -22
- data/lib/badgeapi/badge.rb +24 -27
- data/lib/badgeapi/badgeapi_object.rb +160 -147
- data/lib/badgeapi/collection.rb +6 -9
- data/lib/badgeapi/errors/api_error.rb +3 -4
- data/lib/badgeapi/errors/badgeapi_error.rb +15 -15
- data/lib/badgeapi/errors/invalid_request_error.rb +3 -4
- data/lib/badgeapi/recipient.rb +13 -16
- data/lib/badgeapi/version.rb +1 -1
- data/lib/badgeapi.rb +13 -15
- data/test/badge/badge_test.rb +71 -150
- data/test/badgeapi_test.rb +4 -0
- data/test/collection/collection_test.rb +43 -77
- data/test/fixtures/badge_all.yml +113 -0
- data/test/fixtures/{bad_api_key.yml → badge_all_bad_user.yml} +14 -13
- data/test/fixtures/{all_badges_bad_user.yml → badge_all_badges_bad_user.yml} +16 -57
- data/test/fixtures/badge_all_expanded.yml +184 -0
- data/test/fixtures/badge_all_from_collection.yml +52 -0
- data/test/fixtures/badge_all_issued.yml +99 -0
- data/test/fixtures/{all_badges_limited.yml → badge_all_limited.yml} +10 -8
- data/test/fixtures/{create_badge.yml → badge_create.yml} +20 -16
- data/test/fixtures/{create_new_badge_failure.yml → badge_create_new.yml} +28 -22
- data/test/fixtures/badge_destroy.yml +147 -0
- data/test/fixtures/badge_destroy_error.yml +147 -0
- data/test/fixtures/badge_error.yml +54 -6
- data/test/fixtures/{issue_badge_to_bad_user.yml → badge_issue_to_bad_user.yml} +8 -6
- data/test/fixtures/badge_issue_to_user.yml +107 -0
- data/test/fixtures/{issue_badge_to_user_with_library_card.yml → badge_issue_to_user_with_library_card.yml} +21 -17
- data/test/fixtures/{one_badge.yml → badge_one.yml} +10 -8
- data/test/fixtures/{one_badge_expanded.yml → badge_one_expanded.yml} +11 -9
- data/test/fixtures/badge_requirements.yml +150 -105
- data/test/fixtures/badge_revoke_from_user.yml +107 -0
- data/test/fixtures/{update_badge_via_update.yml → badge_update_via_update.yml} +31 -25
- data/test/fixtures/{update_badge_via_update_slug_history.yml → badge_update_via_update_slug_history.yml} +41 -33
- data/test/fixtures/{all_collection.yml → collection_all.yml} +11 -9
- data/test/fixtures/collection_all_expanded.yml +123 -0
- data/test/fixtures/{all_collection_limit.yml → collection_all_limit.yml} +10 -8
- data/test/fixtures/{create_collection.yml → collection_create.yml} +20 -16
- data/test/fixtures/{create_new_collection_failure.yml → collection_create_new_failure.yml} +36 -28
- data/test/fixtures/{destroy_collection.yml → collection_destroy.yml} +36 -28
- data/test/fixtures/{destroy_collection_error.yml → collection_destroy_error.yml} +36 -28
- data/test/fixtures/collection_error.yml +108 -12
- data/test/fixtures/{one_collection.yml → collection_one.yml} +10 -8
- data/test/fixtures/collection_one_expanded.yml +106 -0
- data/test/fixtures/{update_collection.yml → collection_update.yml} +40 -32
- data/test/fixtures/{update_collection_via_update.yml → collection_update_via_update.yml} +30 -24
- data/test/fixtures/{bad_Recipietn_request.yml → recipient_bad_user_data.yml} +26 -22
- data/test/fixtures/recipient_with_badges.yml +20 -108
- data/test/fixtures/recipient_with_badges_unicard.yml +151 -8
- data/test/recipient/recipient_test.rb +11 -29
- metadata +69 -73
- data/test/fixtures/all_badges.yml +0 -121
- data/test/fixtures/all_badges_expanded.yml +0 -196
- data/test/fixtures/all_badges_from_collection.yml +0 -52
- data/test/fixtures/all_badges_issued.yml +0 -430
- data/test/fixtures/all_collection_expanded.yml +0 -131
- data/test/fixtures/bad_Recipient.yml +0 -183
- data/test/fixtures/destroy_badge.yml +0 -277
- data/test/fixtures/destroy_badge_error.yml +0 -231
- data/test/fixtures/issue_already_owned_badge.yml +0 -49
- data/test/fixtures/issue_badge_to_user.yml +0 -199
- data/test/fixtures/one_collection_expanded.yml +0 -104
- data/test/fixtures/revoke_badge_from_user.yml +0 -199
- data/test/fixtures/revoke_badge_not_issued.yml +0 -49
@@ -1,149 +1,162 @@
|
|
1
|
-
#lib/badgeapi/badgeapi_object.rb
|
2
|
-
|
1
|
+
# lib/badgeapi/badgeapi_object.rb
|
3
2
|
module Badgeapi
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
3
|
+
class BadgeapiObject
|
4
|
+
class << self
|
5
|
+
def collection_name
|
6
|
+
name.demodulize.pluralize.underscore
|
7
|
+
end
|
8
|
+
alias_method :collection_path, :collection_name
|
9
|
+
|
10
|
+
def member_name
|
11
|
+
name.demodulize.underscore
|
12
|
+
end
|
13
|
+
|
14
|
+
def request method, url, params = {}
|
15
|
+
connection = Faraday.new(ssl: { ca_file: Badgeapi.ssl_ca_cert })
|
16
|
+
|
17
|
+
connection.token_auth(Badgeapi.api_key)
|
18
|
+
from_response connection.send(method, url, params)
|
19
|
+
end
|
20
|
+
|
21
|
+
def from_response response
|
22
|
+
attributes = JSON.parse(response.body)
|
23
|
+
|
24
|
+
if attributes.include?("error")
|
25
|
+
handle_api_error attributes
|
26
|
+
else
|
27
|
+
if attributes.class == Array
|
28
|
+
attributes.map do |subattributes|
|
29
|
+
map_json_to_object(subattributes)
|
30
|
+
end
|
31
|
+
else
|
32
|
+
map_json_to_object attributes
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def object_classes
|
38
|
+
@object_classes ||= {
|
39
|
+
'recipient' => Recipient,
|
40
|
+
'collection' => Collection,
|
41
|
+
'badge' => Badge,
|
42
|
+
'required_badge' => Badge
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
def map_json_to_object attributes
|
47
|
+
if attributes['object'].nil?
|
48
|
+
record = new
|
49
|
+
else
|
50
|
+
record = object_classes.fetch(attributes['object'].singularize).new
|
51
|
+
end
|
52
|
+
|
53
|
+
map_instant_variables_to_record attributes, record
|
54
|
+
record
|
55
|
+
end
|
56
|
+
|
57
|
+
def map_instant_variables_to_record attributes, record
|
58
|
+
attributes.each do |name, value|
|
59
|
+
if attribute_a_badge_object? name
|
60
|
+
child = map_related_object object_classes.fetch(name.singularize), value
|
61
|
+
record.instance_variable_set "@#{name}", child
|
62
|
+
else
|
63
|
+
record.instance_variable_set "@#{name}", value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def map_related_object object, attributes
|
69
|
+
if attributes.class == Array
|
70
|
+
attributes.map do |subattributes|
|
71
|
+
# Recursion: loop through until every object is mapped
|
72
|
+
map_related_object object, subattributes
|
73
|
+
end
|
74
|
+
else
|
75
|
+
record = object.new
|
76
|
+
map_instant_variables_to_child_records attributes, record
|
77
|
+
record
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def map_instant_variables_to_child_records attributes, record
|
82
|
+
attributes.each do |name, value|
|
83
|
+
if value.class == Array && value.count > 0
|
84
|
+
if attribute_a_badge_object? name
|
85
|
+
child = map_related_object object_classes.fetch(name.singularize), value
|
86
|
+
record.instance_variable_set "@#{name}", child
|
87
|
+
end
|
88
|
+
else
|
89
|
+
record.instance_variable_set "@#{name}", value
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def attribute_a_badge_object? name
|
95
|
+
if object_classes.has_key?(name) || object_classes.has_key?(name.singularize)
|
96
|
+
true
|
97
|
+
else
|
98
|
+
false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def find id, params = {}
|
103
|
+
request "get", "#{Badgeapi.api_url}/#{collection_path}/#{id}", params
|
104
|
+
end
|
105
|
+
|
106
|
+
def all params = {}
|
107
|
+
request "get", "#{Badgeapi.api_url}/#{collection_path}", params
|
108
|
+
end
|
109
|
+
|
110
|
+
def create params = {}
|
111
|
+
request "post", "#{Badgeapi.api_url}/#{collection_path}", member_name => params
|
112
|
+
end
|
113
|
+
|
114
|
+
def update id, params = {}
|
115
|
+
request "patch", "#{Badgeapi.api_url}/#{collection_path}/#{id}", member_name => params
|
116
|
+
end
|
117
|
+
|
118
|
+
def destroy(id)
|
119
|
+
request "delete", "#{Badgeapi.api_url}/#{collection_path}/#{id}"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def inspect
|
124
|
+
id_as_string = (respond_to?(:id) && !id.nil?) ? " id=#{id}" : ""
|
125
|
+
"#<#{self.class}:0x#{object_id.to_s(16)}#{id_as_string}> JSON: " + to_json
|
126
|
+
end
|
127
|
+
|
128
|
+
def remove_read_only_params
|
129
|
+
# Remove params that cannot be saved as they are not permitted through strong_params on api
|
130
|
+
params = JSON.parse(to_json)
|
131
|
+
|
132
|
+
params.delete("id")
|
133
|
+
params.delete("created_at")
|
134
|
+
params.delete("updated_at")
|
135
|
+
params.delete("points")
|
136
|
+
params.delete("total_points_available")
|
137
|
+
params.delete("badge_count")
|
138
|
+
params.delete("object")
|
139
|
+
end
|
140
|
+
|
141
|
+
def save
|
142
|
+
# Remove params that cannot be saved as they are not permitted through strong_params on api
|
143
|
+
params = remove_read_only_params
|
144
|
+
|
145
|
+
self.class.request "patch", "#{Badgeapi.api_url}/#{self.class.collection_path}/#{id}",
|
146
|
+
self.class.member_name => params
|
147
|
+
end
|
148
|
+
|
149
|
+
def self.handle_api_error error
|
150
|
+
error_object = error['error']
|
151
|
+
|
152
|
+
case error_object["type"]
|
153
|
+
when "invalid_request_error"
|
154
|
+
raise InvalidRequestError.new(error_object["message"], error_object["status"], error)
|
155
|
+
when "api_error"
|
156
|
+
raise APIError.new(error_object["message"], error_object["status"], error)
|
157
|
+
else
|
158
|
+
raise APIError.new("Unknown error tyep #{error_object['type']}", error_object["status"], error)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
149
162
|
end
|
data/lib/badgeapi/collection.rb
CHANGED
@@ -1,10 +1,7 @@
|
|
1
|
-
#lib/badgeapi/collection.rb
|
2
|
-
|
1
|
+
# lib/badgeapi/collection.rb
|
3
2
|
module Badgeapi
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
end
|
3
|
+
class Collection < BadgeapiObject
|
4
|
+
attr_reader :id, :created_at, :updated_at, :badges, :total_points_available, :object, :badge_count
|
5
|
+
attr_accessor :name, :description
|
6
|
+
end
|
7
|
+
end
|
@@ -1,18 +1,18 @@
|
|
1
1
|
module Badgeapi
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
class BadgeapiError < StandardError
|
3
|
+
attr_reader :message
|
4
|
+
attr_reader :http_status
|
5
|
+
attr_reader :json_body
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
def initialize(message = nil, http_status = nil, json_body = nil)
|
8
|
+
@message = message
|
9
|
+
@http_status = http_status
|
10
|
+
@json_body = json_body
|
11
|
+
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
13
|
+
def to_s
|
14
|
+
status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
|
15
|
+
"#{status_string}#{@message}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/badgeapi/recipient.rb
CHANGED
@@ -1,18 +1,15 @@
|
|
1
|
-
#lib/badgeapi/collection.rb
|
2
|
-
|
1
|
+
# lib/badgeapi/collection.rb
|
3
2
|
module Badgeapi
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
3
|
+
class Recipient < BadgeapiObject
|
4
|
+
attr_reader :total_score, :badges_total, :bronze_count, :silver_count, :gold_count, :platinum_count, :badges,
|
5
|
+
:object, :username, :created_at, :updated_at
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def find params = {}
|
9
|
+
request "get", "#{Badgeapi.api_url}/#{collection_path}", params
|
10
|
+
end
|
11
|
+
|
12
|
+
undef_method :all, :create, :update, :destroy
|
13
|
+
end
|
14
|
+
end
|
18
15
|
end
|
data/lib/badgeapi/version.rb
CHANGED
data/lib/badgeapi.rb
CHANGED
@@ -14,23 +14,21 @@ require_relative "badgeapi/errors/api_error"
|
|
14
14
|
require_relative "badgeapi/errors/invalid_request_error"
|
15
15
|
|
16
16
|
module Badgeapi
|
17
|
+
@api_base = 'https://badgeapi.lancaster.ac.uk/v1'
|
17
18
|
|
18
|
-
|
19
|
+
class << self
|
20
|
+
attr_accessor :api_key, :api_base, :ssl_ca_cert
|
21
|
+
end
|
19
22
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
+
def self.api_url
|
24
|
+
@api_base
|
25
|
+
end
|
23
26
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
def self.api_key
|
29
|
-
@api_key
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.ssl_ca_cert
|
33
|
-
@ssl_ca_cert
|
34
|
-
end
|
27
|
+
def self.api_key
|
28
|
+
@api_key
|
29
|
+
end
|
35
30
|
|
31
|
+
def self.ssl_ca_cert
|
32
|
+
@ssl_ca_cert
|
33
|
+
end
|
36
34
|
end
|