badgeapi 0.3.10 → 1.0.0

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