giant_bomb_api 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +6 -0
  5. data/.ruby-gemset +1 -0
  6. data/.ruby-version +1 -0
  7. data/Gemfile +2 -0
  8. data/Gemfile.lock +81 -0
  9. data/LICENSE +22 -0
  10. data/README.md +155 -0
  11. data/Rakefile +7 -0
  12. data/circle.yml +6 -0
  13. data/giant_bomb_api.gemspec +31 -0
  14. data/lib/giant_bomb_api.rb +38 -0
  15. data/lib/giant_bomb_api/client.rb +54 -0
  16. data/lib/giant_bomb_api/collection_resource.rb +40 -0
  17. data/lib/giant_bomb_api/exception.rb +5 -0
  18. data/lib/giant_bomb_api/exception/api_error.rb +14 -0
  19. data/lib/giant_bomb_api/request.rb +10 -0
  20. data/lib/giant_bomb_api/request/collection.rb +36 -0
  21. data/lib/giant_bomb_api/request/detail.rb +9 -0
  22. data/lib/giant_bomb_api/request/search.rb +18 -0
  23. data/lib/giant_bomb_api/resource.rb +34 -0
  24. data/lib/giant_bomb_api/resource/accessory.rb +10 -0
  25. data/lib/giant_bomb_api/resource/character.rb +24 -0
  26. data/lib/giant_bomb_api/resource/chat.rb +11 -0
  27. data/lib/giant_bomb_api/resource/company.rb +32 -0
  28. data/lib/giant_bomb_api/resource/concept.rb +21 -0
  29. data/lib/giant_bomb_api/resource/factory.rb +36 -0
  30. data/lib/giant_bomb_api/resource/franchise.rb +17 -0
  31. data/lib/giant_bomb_api/resource/game.rb +40 -0
  32. data/lib/giant_bomb_api/resource/game_rating.rb +8 -0
  33. data/lib/giant_bomb_api/resource/genre.rb +10 -0
  34. data/lib/giant_bomb_api/resource/image.rb +12 -0
  35. data/lib/giant_bomb_api/resource/location.rb +12 -0
  36. data/lib/giant_bomb_api/resource/object.rb +20 -0
  37. data/lib/giant_bomb_api/resource/person.rb +24 -0
  38. data/lib/giant_bomb_api/resource/platform.rb +16 -0
  39. data/lib/giant_bomb_api/resource/promo.rb +11 -0
  40. data/lib/giant_bomb_api/resource/rating_board.rb +11 -0
  41. data/lib/giant_bomb_api/resource/region.rb +11 -0
  42. data/lib/giant_bomb_api/resource/release.rb +24 -0
  43. data/lib/giant_bomb_api/resource/review.rb +15 -0
  44. data/lib/giant_bomb_api/resource/theme.rb +7 -0
  45. data/lib/giant_bomb_api/resource/user_review.rb +11 -0
  46. data/lib/giant_bomb_api/resource/video.rb +17 -0
  47. data/lib/giant_bomb_api/resource/video_type.rb +8 -0
  48. data/lib/giant_bomb_api/resource_value_setter.rb +29 -0
  49. data/lib/giant_bomb_api/response.rb +17 -0
  50. data/spec/fixtures/detail-response.json +740 -0
  51. data/spec/fixtures/search-response.json +59 -0
  52. data/spec/lib/collection_resource_spec.rb +76 -0
  53. data/spec/lib/giant_bomb_api_spec.rb +56 -0
  54. data/spec/lib/request/collection_spec.rb +32 -0
  55. data/spec/lib/request/search_spec.rb +38 -0
  56. data/spec/lib/resource/factory_spec.rb +35 -0
  57. data/spec/lib/resource/game_spec.rb +28 -0
  58. data/spec/lib/resource_spec.rb +37 -0
  59. data/spec/lib/response_spec.rb +102 -0
  60. data/spec/spec_helper.rb +15 -0
  61. metadata +241 -0
@@ -0,0 +1,59 @@
1
+ {
2
+ "error": "OK",
3
+ "limit": 100,
4
+ "offset": 0,
5
+ "number_of_page_results": 1,
6
+ "number_of_total_results": 1,
7
+ "status_code": 1,
8
+ "results": [
9
+ {
10
+ "aliases": "Drive Club\r\n#DRIVECLUB",
11
+ "api_detail_url": "http://www.giantbomb.com/api/game/3030-41693/",
12
+ "date_added": "2013-02-20 15:57:29",
13
+ "date_last_updated": "2014-11-30 06:30:47",
14
+ "deck": "Driveclub is a team-based racing game from Evolution Studios for PlayStation 4.",
15
+ "description": "<p style=\"\"> </p>",
16
+ "expected_release_day": null,
17
+ "expected_release_month": null,
18
+ "expected_release_quarter": null,
19
+ "expected_release_year": null,
20
+ "id": 41693,
21
+ "image": {
22
+ "icon_url": "http://static.giantbomb.com/uploads/square_avatar/8/82063/2560905-driveclub.jpg",
23
+ "medium_url": "http://static.giantbomb.com/uploads/scale_medium/8/82063/2560905-driveclub.jpg",
24
+ "screen_url": "http://static.giantbomb.com/uploads/screen_medium/8/82063/2560905-driveclub.jpg",
25
+ "small_url": "http://static.giantbomb.com/uploads/scale_small/8/82063/2560905-driveclub.jpg",
26
+ "super_url": "http://static.giantbomb.com/uploads/scale_large/8/82063/2560905-driveclub.jpg",
27
+ "thumb_url": "http://static.giantbomb.com/uploads/scale_avatar/8/82063/2560905-driveclub.jpg",
28
+ "tiny_url": "http://static.giantbomb.com/uploads/square_mini/8/82063/2560905-driveclub.jpg"
29
+ },
30
+ "name": "Driveclub",
31
+ "number_of_user_reviews": 0,
32
+ "original_game_rating": [
33
+ {
34
+ "api_detail_url": "http://www.giantbomb.com/api/game_rating/3065-7/",
35
+ "id": 7,
36
+ "name": "PEGI: 3+"
37
+ },
38
+ {
39
+ "api_detail_url": "http://www.giantbomb.com/api/game_rating/3065-25/",
40
+ "id": 25,
41
+ "name": "CERO: A"
42
+ }
43
+ ],
44
+ "original_release_date": "2014-10-07 00:00:00",
45
+ "platforms": [
46
+ {
47
+ "api_detail_url": "http://www.giantbomb.com/api/platform/3045-146/",
48
+ "id": 146,
49
+ "name": "PlayStation 4",
50
+ "site_detail_url": "http://www.giantbomb.com/playstation-4/3045-146/",
51
+ "abbreviation": "PS4"
52
+ }
53
+ ],
54
+ "site_detail_url": "http://www.giantbomb.com/driveclub/3030-41693/",
55
+ "resource_type": "game"
56
+ }
57
+ ],
58
+ "version": "1.0"
59
+ }
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe GiantBombApi::CollectionResource do
4
+
5
+ class DummyCollection
6
+ extend GiantBombApi::CollectionResource
7
+ end
8
+
9
+ class AnotherDummyCollection
10
+ extend GiantBombApi::CollectionResource
11
+
12
+ collection_resource_name "some_dummy_collection"
13
+ end
14
+
15
+ describe 'extension' do
16
+ it 'will also extend from GiantBombApi::Resource' do
17
+ expect(DummyCollection).to be_a GiantBombApi::Resource
18
+ end
19
+ end
20
+
21
+ describe '#collection_resource_name' do
22
+
23
+ context "when in a class the collection_resource_name has been defined" do
24
+ it 'returns the collection_resource_name of that class' do
25
+ expect(AnotherDummyCollection.collection_resource_name).to eq "some_dummy_collection"
26
+ end
27
+ end
28
+
29
+ context "when in a class the collection_resource_name has NOT been defined" do
30
+ it 'returns the pluralized form of the resource_name' do
31
+ expect(DummyCollection.collection_resource_name).to eq "dummy_collections"
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ describe '#where' do
38
+ let(:client) { double("GiantBombApi::Client") }
39
+ before do
40
+ allow(GiantBombApi).to receive(:client).and_return(client)
41
+ end
42
+
43
+ context "when given NO additional attributes" do
44
+ let(:dummy_collection_request) { double("DummyCollectionRequest") }
45
+ let(:response) { double("GiantBombApi::Response") }
46
+
47
+ it 'will send a blank collection-request for itself' do
48
+ expect(GiantBombApi::Request::Collection).to receive(:new).with(DummyCollection).and_return(dummy_collection_request)
49
+ expect(client).to receive(:send_request).with(dummy_collection_request).and_return(response)
50
+ expect(DummyCollection.where).to eq response
51
+ end
52
+ end
53
+ context "when given a hash that does not contain :sort, :offset or :limit" do
54
+ let(:dummy_collection_request) { double("DummyCollectionRequest") }
55
+ let(:response) { double("GiantBombApi::Response") }
56
+
57
+ it 'will send a collection-request with the given has as a filter' do
58
+ expect(GiantBombApi::Request::Collection).to receive(:new).with(DummyCollection, filter: { name: "something", aliases: "other" }).and_return(dummy_collection_request)
59
+ expect(client).to receive(:send_request).with(dummy_collection_request).and_return(response)
60
+ expect(DummyCollection.where(name: "something", aliases: "other")).to eq response
61
+ end
62
+ end
63
+
64
+ context "when given a hash that contains :sort, :offset and :limit" do
65
+ let(:dummy_collection_request) { double("DummyCollectionRequest") }
66
+ let(:response) { double("GiantBombApi::Response") }
67
+
68
+ it 'will send a collection-request with the the parameters assorted correctly' do
69
+ expect(GiantBombApi::Request::Collection).to receive(:new).with(DummyCollection, filter: { name: "something", aliases: "other" }, sort: {something: :desc}, limit: 10, offset: 1).and_return(dummy_collection_request)
70
+ expect(client).to receive(:send_request).with(dummy_collection_request).and_return(response)
71
+ expect(DummyCollection.where(name: "something", aliases: "other", sort: {something: :desc}, limit: 10, offset: 1)).to eq response
72
+ end
73
+ end
74
+ end
75
+
76
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe GiantBombApi do
4
+
5
+ before do
6
+ GiantBombApi.configure do |config|
7
+ config.api_key = "12345"
8
+ end
9
+ end
10
+
11
+ after do
12
+ GiantBombApi.configuration = nil
13
+ end
14
+
15
+ describe "#configure" do
16
+
17
+ it "will correctly retain it's configuration" do
18
+ expect(GiantBombApi.configuration.api_key).to eq "12345"
19
+ end
20
+ end
21
+
22
+ describe '#search' do
23
+ let(:search_request) { double() }
24
+ let(:result) { double() }
25
+
26
+ it 'uses the modules default client to send a Request::Search at returns the result' do
27
+ expect(GiantBombApi::Request::Search).to receive(:new).with("query").and_return(search_request)
28
+ expect(GiantBombApi.client).to receive(:send_request).with(search_request).and_return(result)
29
+
30
+ expect(GiantBombApi.search("query")).to eq result
31
+ end
32
+ end
33
+
34
+ describe "#client" do
35
+
36
+ context "when correctly configured first" do
37
+ let(:client) { double("GiantBombApi::Client") }
38
+
39
+ it "will use the module-config to configure a client and returns it" do
40
+ expect(GiantBombApi::Client).to receive(:new).with(api_key: "12345").and_return(client)
41
+ expect(GiantBombApi.client).to eq client
42
+ end
43
+ end
44
+
45
+ context "when not configured first" do
46
+ before do
47
+ GiantBombApi.configuration = nil
48
+ end
49
+
50
+ it "will raise an error and asks to configure first" do
51
+ expect{GiantBombApi.client}.to raise_error(GiantBombApi::Exception)
52
+ end
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe GiantBombApi::Request::Collection do
4
+ describe "#initialize" do
5
+ context "when only a resource is given" do
6
+ it "sets query and the correct request_params" do
7
+ collection_request = GiantBombApi::Request::Collection.new GiantBombApi::Resource::Game
8
+
9
+ expect(collection_request.end_point).to eq "games"
10
+ expect(collection_request.params).to eq({limit: 100, offset: 0})
11
+ end
12
+ end
13
+
14
+ context "when a resource with filter is given" do
15
+ it "sets query and the correct request_params" do
16
+ collection_request = GiantBombApi::Request::Collection.new GiantBombApi::Resource::Game, filter: {name: "other", aliases: "something"}
17
+
18
+ expect(collection_request.end_point).to eq "games"
19
+ expect(collection_request.params).to eq(filter: "name:other,aliases:something", limit: 100, offset: 0)
20
+ end
21
+ end
22
+
23
+ context "when a resource with filter and sort is given" do
24
+ it "sets query and the correct request_params" do
25
+ collection_request = GiantBombApi::Request::Collection.new GiantBombApi::Resource::Game, filter: {name: "other", aliases: "something"}, sort: {name: :desc}
26
+
27
+ expect(collection_request.end_point).to eq "games"
28
+ expect(collection_request.params).to eq(filter: "name:other,aliases:something", sort: "name:desc", limit: 100, offset: 0)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe GiantBombApi::Request::Search do
4
+ describe "#initialize" do
5
+ context "when only a query is given" do
6
+ it "sets query and the correct defaults as request_params" do
7
+ search_request = GiantBombApi::Request::Search.new "query"
8
+
9
+ expect(search_request.params).to eq({ query: "query",
10
+ resources: "game",
11
+ limit: 100,
12
+ page: 1 })
13
+ end
14
+ end
15
+
16
+ context "when a query and some resources are given" do
17
+ it "maps the resources-class to resource-names and sets the correct request_params" do
18
+ search_request = GiantBombApi::Request::Search.new "query", resources: [GiantBombApi::Resource::Game, GiantBombApi::Resource::Company, GiantBombApi::Resource::Character]
19
+
20
+ expect(search_request.params).to eq({ query: "query",
21
+ resources: "game,company,character",
22
+ limit: 100,
23
+ page: 1 })
24
+ end
25
+ end
26
+
27
+ context "when a query, some resources and parameters are given" do
28
+ it "sets the correct request_params" do
29
+ search_request = GiantBombApi::Request::Search.new "query", resources: [GiantBombApi::Resource::Game, GiantBombApi::Resource::Company], limit: 50, page: 2
30
+
31
+ expect(search_request.params).to eq({ query: "query",
32
+ resources: "game,company",
33
+ limit: 50,
34
+ page: 2 })
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe GiantBombApi::Resource::Factory do
4
+
5
+ let(:game_json) {{'api_detail_url' => 'http://www.giantbomb.com/api/game/3030-41693/', 'some' => 'data' }}
6
+ let(:unknown_json) {{'api_detail_url' => 'http://www.giantbomb.com/api/unknown/1234-567/', 'some' => 'data' }}
7
+ let(:image_json) {{"icon_url": "http://url","medium_url": "http://url2","screen_url": "http://url3"}}
8
+
9
+ describe '#init_resource_from' do
10
+ context "when a json without resource_name is given" do
11
+ context 'and a api_detail_url is present' do
12
+ let(:game) { double('Game') }
13
+
14
+ it 'initializes the object based on the found resource_name in the api_detail_url' do
15
+ expect(GiantBombApi::Resource::Game).to receive(:new).with(game_json).and_return(game)
16
+ expect(GiantBombApi::Resource::Factory.init_resource_from(game_json)).to eq game
17
+ end
18
+
19
+ context 'and an unknown-resource-name is discovered' do
20
+ it 'will not fail and return nil' do
21
+ expect(GiantBombApi::Resource::Factory.init_resource_from(unknown_json)).to be_nil
22
+ end
23
+ end
24
+ end
25
+ end
26
+ context "when a json with resource_name is given" do
27
+ let(:image) { double('Image') }
28
+ it 'initializes the object based on the given resource_name' do
29
+ expect(GiantBombApi::Resource::Image).to receive(:new).with(image_json).and_return(image)
30
+ expect(GiantBombApi::Resource::Factory.init_resource_from(image_json, resource_name: :image)).to eq image
31
+ end
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe GiantBombApi::Resource::Game do
4
+
5
+ let(:games_detail_response) { JSON.parse File.read(File.join('spec', 'fixtures', 'detail-response.json')) }
6
+
7
+ describe '#initialize' do
8
+ it 'correctly builds an object hierachy for a detail-json' do
9
+ game = GiantBombApi::Resource::Game.new games_detail_response["results"]
10
+
11
+ expect(game.name).to eq "Driveclub"
12
+ expect(game.image).to be_present
13
+ expect(game.original_game_rating.count).to eq 2
14
+ expect(game.platforms.count).to eq 1
15
+ expect(game.images.count).to eq 30
16
+ expect(game.videos.count).to eq 5
17
+ expect(game.developers.count).to eq 1
18
+ expect(game.genres.count).to eq 1
19
+ expect(game.franchises).to be_nil
20
+ expect(game.publishers.count).to eq 2
21
+ expect(game.releases.count).to eq 4
22
+ expect(game.similar_games.count).to eq 1
23
+
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe GiantBombApi::Resource do
4
+
5
+ class DummyResource
6
+ extend GiantBombApi::Resource
7
+ end
8
+
9
+ class AnotherDummyResource
10
+ extend GiantBombApi::Resource
11
+
12
+ resource_name "some_dummy_resource"
13
+ end
14
+
15
+ describe 'inclusion' do
16
+ it 'will also include from GiantBombApi::Resource' do
17
+ expect(DummyCollection).to include(GiantBombApi::ResourceValueSetter)
18
+ end
19
+ end
20
+
21
+ describe '#resource_name' do
22
+
23
+ context "when in a class the resource_name has been defined" do
24
+ it 'returns the resource_name of that class' do
25
+ expect(AnotherDummyResource.resource_name).to eq "some_dummy_resource"
26
+ end
27
+ end
28
+
29
+ context "when in a class the resource_name has NOT been defined" do
30
+ it 'returns the underscored class_name' do
31
+ expect(DummyResource.resource_name).to eq "dummy_resource"
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ describe GiantBombApi::Response do
4
+
5
+ let(:search_response) { JSON.parse File.read(File.join('spec', 'fixtures', 'search-response.json')) }
6
+ let(:attributes_json) { { "limit" => "123", "offset" => "12", "number_of_page_results" => "1234", "number_of_total_results" => "4000"} }
7
+
8
+ describe '#has_more_results?' do
9
+ context "when total-results is bigger than offset plus page-results" do
10
+ it 'returns true' do
11
+ response = GiantBombApi::Response.new({ "offset" => 100, "number_of_page_results" => 100, "number_of_total_results" => 300 })
12
+ expect(response.has_more_results?).to eq true
13
+ end
14
+ end
15
+
16
+ context "when total-results is the same as offset + number_of_page_results" do
17
+ it 'returns false' do
18
+ response = GiantBombApi::Response.new({ "offset" => 0, "number_of_page_results" => 50, "number_of_total_results" => 50 })
19
+ expect(response.has_more_results?).to eq false
20
+ end
21
+ end
22
+ end
23
+
24
+ describe '#initialize' do
25
+ context "when handed a search-result" do
26
+ let(:response) { GiantBombApi::Response.new(search_response) }
27
+
28
+ it 'sets the correct values on itself' do
29
+ expect(response.limit).to eq 100
30
+ expect(response.offset).to eq 0
31
+ expect(response.number_of_page_results).to eq 1
32
+ expect(response.number_of_total_results).to eq 1
33
+ end
34
+
35
+ it 'creates the correct results of objects' do
36
+ expect(response.results.count).to eq 1
37
+
38
+ result = response.results.first
39
+
40
+ expect(result).to be_a GiantBombApi::Resource::Game
41
+ expect(result.aliases).to eq "Drive Club\r\n#DRIVECLUB"
42
+ expect(result.api_detail_url).to eq "http://www.giantbomb.com/api/game/3030-41693/"
43
+ expect(result.date_added).to eq "2013-02-20 15:57:29"
44
+ expect(result.date_last_updated).to eq "2014-11-30 06:30:47"
45
+ expect(result.deck).to eq "Driveclub is a team-based racing game from Evolution Studios for PlayStation 4."
46
+ expect(result.description).to eq "<p style=\"\"> </p>"
47
+ expect(result.expected_release_day).to be_nil
48
+ expect(result.expected_release_month).to be_nil
49
+ expect(result.expected_release_quarter).to be_nil
50
+ expect(result.expected_release_year).to be_nil
51
+ expect(result.id).to eq 41693
52
+ expect(result.name).to eq "Driveclub"
53
+ expect(result.number_of_user_reviews).to eq 0
54
+ expect(result.original_release_date).to eq "2014-10-07 00:00:00"
55
+ end
56
+
57
+ it 'sets the correct platforms' do
58
+ result = response.results.first
59
+
60
+ expect(result.image).to be_present
61
+ expect(result.image.icon_url).to eq "http://static.giantbomb.com/uploads/square_avatar/8/82063/2560905-driveclub.jpg"
62
+ expect(result.image.medium_url).to eq "http://static.giantbomb.com/uploads/scale_medium/8/82063/2560905-driveclub.jpg"
63
+ expect(result.image.screen_url).to eq "http://static.giantbomb.com/uploads/screen_medium/8/82063/2560905-driveclub.jpg"
64
+ expect(result.image.small_url).to eq "http://static.giantbomb.com/uploads/scale_small/8/82063/2560905-driveclub.jpg"
65
+ expect(result.image.super_url).to eq "http://static.giantbomb.com/uploads/scale_large/8/82063/2560905-driveclub.jpg"
66
+ expect(result.image.thumb_url).to eq "http://static.giantbomb.com/uploads/scale_avatar/8/82063/2560905-driveclub.jpg"
67
+ expect(result.image.tiny_url).to eq "http://static.giantbomb.com/uploads/square_mini/8/82063/2560905-driveclub.jpg"
68
+ end
69
+
70
+ it 'sets the correct platforms' do
71
+ result = response.results.first
72
+
73
+ expect(result.platforms.count).to eq 1
74
+ platform = result.platforms.first
75
+
76
+ expect(platform).to be_a GiantBombApi::Resource::Platform
77
+ expect(platform.api_detail_url).to eq "http://www.giantbomb.com/api/platform/3045-146/"
78
+ expect(platform.id).to eq 146
79
+ expect(platform.name).to eq "PlayStation 4"
80
+ expect(platform.abbreviation).to eq "PS4"
81
+ end
82
+
83
+ it 'sets the correct game_ratings' do
84
+ result = response.results.first
85
+
86
+ expect(result.original_game_rating.count).to eq 2
87
+
88
+ rating1 = result.original_game_rating[0]
89
+ rating2 = result.original_game_rating[1]
90
+
91
+ expect(rating1.api_detail_url).to eq "http://www.giantbomb.com/api/game_rating/3065-7/"
92
+ expect(rating1.id).to eq 7
93
+ expect(rating1.name).to eq "PEGI: 3+"
94
+
95
+ expect(rating2.api_detail_url).to eq "http://www.giantbomb.com/api/game_rating/3065-25/"
96
+ expect(rating2.id).to eq 25
97
+ expect(rating2.name).to eq "CERO: A"
98
+ end
99
+ end
100
+ end
101
+
102
+ end