badgeapi 0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +15 -0
- data/badgeapi.gemspec +33 -0
- data/lib/badgeapi/badge.rb +29 -0
- data/lib/badgeapi/badgeapi_object.rb +149 -0
- data/lib/badgeapi/collection.rb +10 -0
- data/lib/badgeapi/errors/api_error.rb +5 -0
- data/lib/badgeapi/errors/badgeapi_error.rb +18 -0
- data/lib/badgeapi/errors/invalid_request_error.rb +5 -0
- data/lib/badgeapi/recipient.rb +18 -0
- data/lib/badgeapi/version.rb +3 -0
- data/lib/badgeapi.rb +36 -0
- data/test/badge/badge_test.rb +469 -0
- data/test/badgeapi_test.rb +20 -0
- data/test/collection/collection_test.rb +275 -0
- data/test/fixtures/all_badges.yml +121 -0
- data/test/fixtures/all_badges_bad_user.yml +138 -0
- data/test/fixtures/all_badges_expanded.yml +196 -0
- data/test/fixtures/all_badges_from_collection.yml +52 -0
- data/test/fixtures/all_badges_issued.yml +430 -0
- data/test/fixtures/all_badges_limited.yml +49 -0
- data/test/fixtures/all_collection.yml +50 -0
- data/test/fixtures/all_collection_expanded.yml +131 -0
- data/test/fixtures/all_collection_limit.yml +49 -0
- data/test/fixtures/bad_Recipient.yml +183 -0
- data/test/fixtures/bad_Recipietn_request.yml +93 -0
- data/test/fixtures/bad_api_key.yml +49 -0
- data/test/fixtures/badge_error.yml +47 -0
- data/test/fixtures/badge_requirements.yml +351 -0
- data/test/fixtures/collection_error.yml +91 -0
- data/test/fixtures/create_badge.yml +97 -0
- data/test/fixtures/create_collection.yml +97 -0
- data/test/fixtures/create_new_badge_failure.yml +143 -0
- data/test/fixtures/create_new_collection_failure.yml +189 -0
- data/test/fixtures/destroy_badge.yml +277 -0
- data/test/fixtures/destroy_badge_error.yml +231 -0
- data/test/fixtures/destroy_collection.yml +185 -0
- data/test/fixtures/destroy_collection_error.yml +185 -0
- data/test/fixtures/issue_already_owned_badge.yml +49 -0
- data/test/fixtures/issue_badge_to_bad_user.yml +50 -0
- data/test/fixtures/issue_badge_to_user.yml +199 -0
- data/test/fixtures/issue_badge_to_user_with_library_card.yml +103 -0
- data/test/fixtures/one_badge.yml +49 -0
- data/test/fixtures/one_badge_expanded.yml +50 -0
- data/test/fixtures/one_collection.yml +49 -0
- data/test/fixtures/one_collection_expanded.yml +104 -0
- data/test/fixtures/recipient_with_badges.yml +187 -0
- data/test/fixtures/recipient_with_badges_unicard.yml +50 -0
- data/test/fixtures/revoke_badge_from_user.yml +199 -0
- data/test/fixtures/revoke_badge_not_issued.yml +49 -0
- data/test/fixtures/update_badge_via_update.yml +145 -0
- data/test/fixtures/update_badge_via_update_slug_history.yml +191 -0
- data/test/fixtures/update_collection.yml +191 -0
- data/test/fixtures/update_collection_via_update.yml +145 -0
- data/test/recipient/recipient_test.rb +108 -0
- data/test/test_helper.rb +11 -0
- metadata +274 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ff525cd89255e0ca871bf73be2df0a4bdb51bdb4
|
4
|
+
data.tar.gz: d12d2686a38ba0c9b0bb5a5f86e46b2927567857
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7aaa17617b920cf4b10812bd8c7a5996acdab7cca2ae684868e08af42c2e0211975e7abfc7258441ec2f143f2e895711598c5d1efdb19366b42f5d5aba0a33e7
|
7
|
+
data.tar.gz: 37accdf894db35092d71d13e33caf76b2321fc4e94a7ea39dc73013a7827653daf0d663edc39322ea5c4161ee8fed7d08cfa8696109b01c9f1682733720b3d7a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Tom Skarbek Wazynski
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# .
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem '.'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install .
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/./fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
desc "Open an pry/irb session preloaded with badgeapi"
|
4
|
+
task :console do
|
5
|
+
irb = "pry"
|
6
|
+
# Check if pry installed
|
7
|
+
begin
|
8
|
+
gem "pry"
|
9
|
+
rescue Gem::LoadError
|
10
|
+
irb = "irb"
|
11
|
+
end
|
12
|
+
puts "Loading badgeapi gem with #{irb}"
|
13
|
+
|
14
|
+
exec "#{irb} -I lib -r badgeapi.rb"
|
15
|
+
end
|
data/badgeapi.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'badgeapi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "badgeapi"
|
8
|
+
spec.version = Badgeapi::VERSION
|
9
|
+
spec.authors = ["Tom Skarbek Wazynski"]
|
10
|
+
spec.email = ["wazynski@gmail.com"]
|
11
|
+
spec.summary = %q{A basic API wrapper for Lancaster University's Badge API.}
|
12
|
+
spec.description = %q{Allows you to connect to Lancaster University's Badge API to manipulate badges, issue badges and display badges.}
|
13
|
+
spec.homepage = "http://innovationhub.lancaster.ac.uk"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "minitest", "~> 5.5"
|
22
|
+
spec.add_development_dependency "minitest-focus", "~> 1.1"
|
23
|
+
spec.add_development_dependency "vcr", "~> 2.9"
|
24
|
+
spec.add_development_dependency "webmock", "~> 1.20"
|
25
|
+
|
26
|
+
# So it works with rails 4 and 3
|
27
|
+
spec.add_dependency "activesupport", "> 3.2"
|
28
|
+
spec.add_dependency "faraday", "~> 0.9"
|
29
|
+
spec.add_dependency "json", "~> 1.8"
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#lib/badgeapi/badge.rb
|
2
|
+
module Badgeapi
|
3
|
+
class Badge < BadgeapiObject
|
4
|
+
|
5
|
+
attr_reader :id, :created_at, :updated_at, :collection, :points, :required_badges, :image, :object
|
6
|
+
attr_accessor :name, :description, :requirements, :requirements, :hint, :recipient_id, :collection_id, :issuer_id, :issued_at, :level, :auto_issue, :status
|
7
|
+
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def issue id, params = {}
|
12
|
+
request "post", "#{Badgeapi.api_url}/#{collection_path}/#{id}/issue", params
|
13
|
+
end
|
14
|
+
|
15
|
+
def revoke id, params = {}
|
16
|
+
request "post", "#{Badgeapi.api_url}/#{collection_path}/#{id}/revoke", params
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_badge_requirement id, required_id
|
20
|
+
request "post", "#{Badgeapi.api_url}/#{collection_path}/#{id}/requires", {required_badge: {id: required_id}}
|
21
|
+
end
|
22
|
+
|
23
|
+
def remove_badge_requirement id, required_id
|
24
|
+
request "delete", "#{Badgeapi.api_url}/#{collection_path}/#{id}/requires", {required_badge: {id: required_id}}
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
#lib/badgeapi/badgeapi_object.rb
|
2
|
+
|
3
|
+
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
|
149
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Badgeapi
|
2
|
+
class BadgeapiError < StandardError
|
3
|
+
attr_reader :message
|
4
|
+
attr_reader :http_status
|
5
|
+
attr_reader :json_body
|
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
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
|
15
|
+
"#{status_string}#{@message}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#lib/badgeapi/collection.rb
|
2
|
+
|
3
|
+
module Badgeapi
|
4
|
+
class Recipient < BadgeapiObject
|
5
|
+
|
6
|
+
attr_reader :total_score, :badges_total, :bronze_count, :silver_count, :gold_count, :platinum_count, :badges, :object
|
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
|
18
|
+
end
|
data/lib/badgeapi.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'faraday'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
require_relative "badgeapi/badgeapi_object"
|
6
|
+
require_relative "badgeapi/version"
|
7
|
+
require_relative "badgeapi/badge"
|
8
|
+
require_relative "badgeapi/collection"
|
9
|
+
require_relative "badgeapi/recipient"
|
10
|
+
|
11
|
+
# Errors
|
12
|
+
require_relative "badgeapi/errors/badgeapi_error"
|
13
|
+
require_relative "badgeapi/errors/api_error"
|
14
|
+
require_relative "badgeapi/errors/invalid_request_error"
|
15
|
+
|
16
|
+
module Badgeapi
|
17
|
+
|
18
|
+
@api_base = 'https://badgeapi.lancaster.ac.uk/v1'
|
19
|
+
|
20
|
+
class << self
|
21
|
+
attr_accessor :api_key, :api_base, :ssl_ca_cert
|
22
|
+
end
|
23
|
+
|
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
|
35
|
+
|
36
|
+
end
|