Skip to content

Commit

Permalink
Gem now can add and remove badge requirements. required_badges are tu…
Browse files Browse the repository at this point in the history
…rned into badge objects. Updated test and passing.
  • Loading branch information
Tom Skarbek Wazynski committed Jul 21, 2015
1 parent c74e9f4 commit 84a3e26
Show file tree
Hide file tree
Showing 39 changed files with 674 additions and 359 deletions.
11 changes: 9 additions & 2 deletions lib/badgeapi/badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module Badgeapi
class Badge < BadgeapiObject

attr_reader :id, :created_at, :updated_at, :collection, :image_greyscale, :points
attr_reader :id, :created_at, :updated_at, :collection, :image_greyscale, :points, :required_badges
attr_accessor :name, :description, :requirements, :requirements, :hint, :image, :recipient_id, :collection_id, :issuer_id, :issued_at, :level


Expand All @@ -16,7 +16,14 @@ def revoke id, params = {}
request "post", "#{Badgeapi.api_url}/#{collection_path}/#{id}/revoke", params
end

end
def add_badge_requirement id, required_id
request "post", "#{Badgeapi.api_url}/#{collection_path}/#{id}/requires", {required_badge: {id: required_id}}
end

def remove_badge_requirement id, required_id
request "delete", "#{Badgeapi.api_url}/#{collection_path}/#{id}/requires", {required_badge: {id: required_id}}
end

end
end
end
5 changes: 4 additions & 1 deletion lib/badgeapi/badgeapi_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def from_response response
def object_classes
@object_classes ||= {
'collection' => Collection,
'badge'=> Badge
'badge'=> Badge,
}
end

Expand All @@ -49,6 +49,9 @@ def map_json_to_object attributes
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
elsif name == "required_badges"
child = map_related_object object_classes.fetch("badge"), value
record.instance_variable_set "@#{name}", child
else
record.instance_variable_set "@#{name}", value
end
Expand Down
19 changes: 19 additions & 0 deletions test/badge/badge_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,23 @@ def test_should_error_when_revoking_badge_not_owned
end
end

def test_badge_requirements
VCR.use_cassette('badge_requirements', :record => :all) do
Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'

badge = Badgeapi::Badge.find("book-worm")

required_count = badge.required_badges.count

badge = Badgeapi::Badge.add_badge_requirement("marathon-man", "you-trim-trailed")

assert_equal required_count + 1, badge.required_badges.count
assert_equal Badgeapi::Badge, badge.required_badges.first.class

badge = Badgeapi::Badge.remove_badge_requirement("marathon-man", "you-trim-trailed")

assert_equal required_count, badge.required_badges.count
end
end

end
8 changes: 4 additions & 4 deletions test/fixtures/all_badges.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions test/fixtures/all_badges_bad_user.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions test/fixtures/all_badges_expanded.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions test/fixtures/all_badges_issued.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/fixtures/all_badges_limited.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/fixtures/all_collection.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/fixtures/all_collection_expanded.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/fixtures/all_collection_limit.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 84a3e26

Please sign in to comment.