thegamesdb 0.2.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gamesdb
4
+ # Genres related API Endpoints
5
+ module Genres
6
+ # Fetches genres list
7
+ def genres
8
+ url = 'Genres'
9
+ data = perform_request(url)
10
+ data['data']['genres'].map { |_id, genre| genre }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gamesdb
4
+ # Client for TheGamesDB API (thegamesdb.net)
5
+ module Images
6
+ end
7
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gamesdb
4
+ # Platforms related API Endpoints
5
+ module Platforms
6
+ # Method for listing platforms
7
+ #
8
+ # @see https://api.thegamesdb.net/#/Platforms/Platforms
9
+ #
10
+ # @return [Array] Array of Hashes with platforms info
11
+ #
12
+ def platforms
13
+ url = 'Platforms'
14
+ params = { fields:
15
+ 'icon,console,controller,developer,manufacturer,media,cpu,memory,graphics,sound,maxcontrollers,'\
16
+ 'display,overview,youtube' }
17
+ data = perform_request(url, params)
18
+
19
+ data['data']['platforms'].map do |p|
20
+ symbolize_keys(p.last)
21
+ end
22
+ end
23
+
24
+ # This API feature returns a set of metadata and artwork data for a specified Platform ID.
25
+ #
26
+ # @see https://api.thegamesdb.net/#/Platforms/PlatformsByPlatformID
27
+ #
28
+ # @param id [Integer|String] The numeric ID of the platform in the GamesDB
29
+ # database or a String with comma delimited list of Ids.
30
+ #
31
+ # @return [Hash|Array] Returns a Hash when there's one result or an Array of
32
+ # Hashes when there's more than one
33
+ #
34
+ def platforms_by_id(id)
35
+ url = 'Platforms/ByPlatformID'
36
+ params = {
37
+ id: id,
38
+ fields:
39
+ 'icon,console,controller,developer,manufacturer,media,cpu,memory,graphics,sound,maxcontrollers,' \
40
+ 'display,overview,youtube'
41
+ }
42
+ data = perform_request(url, params)
43
+
44
+ platform_api_response(data)
45
+ end
46
+
47
+ # Fetches platforms by name
48
+ #
49
+ # @see https://api.thegamesdb.net/#/Platforms/PlatformsByPlatformName
50
+ #
51
+ # @param name [String] Platform name (Required)
52
+ #
53
+ # @return [Hash|Array] Returns a Hash when there's one result or an Array of
54
+ # Hashes when there's more than one
55
+ def platforms_by_name(name)
56
+ url = 'Platforms/ByPlatformName'
57
+ params = {
58
+ name: name,
59
+ fields:
60
+ 'icon,console,controller,developer,manufacturer,media,cpu,memory,graphics,sound,maxcontrollers,display,' \
61
+ 'overview,youtube'
62
+ }
63
+ data = perform_request(url, params)
64
+
65
+ platform_api_response(data)
66
+ end
67
+
68
+ # Fetch platform(s) images by platform(s) id
69
+ #
70
+ # @see https://api.thegamesdb.net/#/Platforms/PlatformsImages
71
+ #
72
+ # @param platforms_id [Integer|String] The numeric ID of the platform in the GamesDB
73
+ # database or a String with comma delimited list of Ids. (Required)
74
+ # @param filter [String] options: 'fanart', 'banner', 'boxart' (supports
75
+ # comma delimited list)
76
+ # @param page [Integer]
77
+ #
78
+ # @return Array of Hashes
79
+ def platform_images(platforms_id, args = {})
80
+ url = 'Platforms/Images'
81
+ args['filter[type]'] = args.delete(:type) if args[:type]
82
+ args = args.merge({ platforms_id: platforms_id })
83
+ data = perform_request(url, args)
84
+ data['data']['images'].values.flatten
85
+ end
86
+
87
+ # Auxiliary method to return either one hash when there's only one result or
88
+ # an Array of Hashes for several results
89
+ def platform_api_response(data)
90
+ return [] if data['data']['count'].zero?
91
+
92
+ platforms = data['data']['platforms']
93
+
94
+ response = case platforms
95
+ when Hash
96
+ platforms.map { |_k, platform| symbolize_keys(platform) }
97
+ when Array
98
+ platforms.map { |platform| symbolize_keys(platform) }
99
+ end
100
+
101
+ return response.first if response.count == 1
102
+
103
+ response
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gamesdb
4
+ # Publishers related API Endpoints
5
+ module Publishers
6
+ def publishers
7
+ url = 'Publishers'
8
+
9
+ data = perform_request(url)
10
+ data['data']['publishers'].map do |_id, publisher|
11
+ publisher
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Gem version
2
4
  module Gamesdb
3
- VERSION = '0.2.0'.freeze
5
+ VERSION = '2.0.0'
4
6
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './test_helper'
4
+
5
+ describe 'Gamesdb - client', :vcr do
6
+ let(:client) { Gamesdb::Client.new(ENV['GAMESDB_API_KEY']) }
7
+
8
+ describe 'client' do
9
+ it 'should update allowances' do
10
+ client.games_by_id(1904)
11
+ monthly = client.remaining_monthly_allowance
12
+ client.games_by_id(1527)
13
+ expect(client.remaining_monthly_allowance).must_equal(monthly - 1)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './test_helper'
4
+
5
+ describe 'Gamesdb - developers', :vcr do
6
+ let(:client) { Gamesdb::Client.new(ENV['GAMESDB_API_KEY']) }
7
+
8
+ describe 'developers' do
9
+ it 'should return the developers' do
10
+ @developers = client.developers
11
+
12
+ expect(@developers.count).must_equal 8168
13
+ expect(@developers.first.keys).must_equal(['id', 'name'])
14
+ end
15
+ end
16
+ end
@@ -1,63 +1,167 @@
1
- require_relative './test_helper.rb'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './test_helper'
4
+
5
+ describe 'Gamesdb - games', :vcr do
6
+ let(:client) { Gamesdb::Client.new(ENV['GAMESDB_API_KEY']) }
2
7
 
3
- describe "Gamesdb - games", :vcr do
4
8
  describe 'game' do
5
9
  before do
6
- @game = Gamesdb.game(109)
10
+ @game = client.games_by_id(109)
7
11
  end
8
12
 
9
13
  it 'should have a valid id' do
10
- @game[:id].must_be_kind_of Integer
11
- @game[:id].must_equal 109
14
+ expect(@game[:id]).must_be_kind_of Integer
15
+ expect(@game[:id]).must_equal 109
12
16
  end
13
17
 
14
18
  it 'should have valid fields' do
15
- @game[:title].must_be_kind_of String
16
- @game[:title].length.wont_be :<, 0
19
+ expect(@game[:game_title]).must_be_kind_of String
20
+ expect(@game[:game_title].length).wont_be :<, 0
17
21
  end
18
22
  end
19
23
 
20
- describe 'games_list' do
24
+ describe 'game by several ids' do
25
+ it 'should return games for a String of ids' do
26
+ @games = client.games_by_id('109,108').sort_by { |g| g[:id] }
27
+
28
+ expect(@games.count).must_equal 2
29
+ expect(@games.first[:id]).must_equal 108
30
+ expect(@games.last[:id]).must_equal 109
31
+ end
32
+ end
33
+
34
+ describe 'empty game' do
21
35
  before do
22
- @games_list = Gamesdb.games_list('turrican')
36
+ @game = client.games_by_id(3)
37
+ end
38
+
39
+ it 'should return an empty array' do
40
+ expect(@game).must_equal []
41
+ end
42
+ end
43
+
44
+ describe 'games by name' do
45
+ before do
46
+ @games_list = client.games_by_name('turrican')
23
47
  end
24
48
 
25
49
  it 'should return a list' do
26
50
  game = @games_list.first
27
- game[:id].must_be_kind_of Integer
28
- game[:name].must_be_kind_of String
29
- game[:platform].must_be_kind_of String
51
+ expect(game[:id]).must_be_kind_of Integer
52
+ expect(game[:name]).must_be_kind_of String
53
+ expect(game[:platform]).must_be_kind_of Integer
54
+ expect(game[:release_date]).must_be_kind_of String
30
55
  end
31
56
  end
32
57
 
33
- describe 'games art', :vcr do
58
+ describe 'games by name pages' do
34
59
  before do
35
- @images = Gamesdb.art('216')
60
+ @first_page = client.games_by_name('mario', page: 1)
61
+ @second_page = client.games_by_name('mario', page: 2)
36
62
  end
37
63
 
38
- it 'should return logo and boxart' do
39
- @images[:boxart].count.wont_be :<, 0
40
- @images[:logo].must_be_kind_of String
41
- @images[:boxart][:front][:url].must_be_kind_of String
42
- @images[:boxart][:front][:width].must_be_kind_of String
43
- @images[:boxart][:front][:height].must_be_kind_of String
44
- @images[:boxart][:front][:thumb].must_be_kind_of String
64
+ it 'should return games in platform by id' do
65
+ expect(@first_page.count).wont_be :<, 0
66
+ expect(@first_page.count).must_equal 20
45
67
  end
46
68
 
47
- it 'should return screenshots' do
48
- @images[:screenshot].count.wont_be :<, 0
49
- @images[:screenshot].first[:original][1].must_be_kind_of String
50
- @images[:screenshot].first[:original].first[:width].must_be_kind_of String
51
- @images[:screenshot].first[:original].first[:height].must_be_kind_of String
52
- @images[:screenshot].first[:thumb].must_be_kind_of String
69
+ it 'should return games in the platform for the second page' do
70
+ expect(@second_page.count).wont_be :<, 0
71
+ expect(@second_page.count).must_equal 20
72
+ expect(@first_page & @second_page).must_equal []
73
+ end
74
+ end
75
+
76
+ describe 'games by name and platform' do
77
+ before do
78
+ @games_list = client.games_by_name('mario', platform: 7)
53
79
  end
54
80
 
55
- it 'should return fanart' do
56
- @images[:fanart].count.wont_be :<, 0
57
- @images[:fanart].first[:original][1].must_be_kind_of String
58
- @images[:fanart].first[:original].first[:width].must_be_kind_of String
59
- @images[:fanart].first[:original].first[:height].must_be_kind_of String
60
- @images[:fanart].first[:thumb].must_be_kind_of String
81
+ it 'should return a list' do
82
+ @games_list.each do |game|
83
+ expect(game[:id]).must_be_kind_of Integer
84
+ expect(game[:id]).must_be_kind_of Integer
85
+ expect(game[:name]).must_be_kind_of String
86
+ expect(game[:platform]).must_equal 7
87
+ end
88
+ end
89
+ end
90
+
91
+ describe 'games art', :vcr do
92
+ describe 'when most of the art is available' do
93
+ before do
94
+ @images = client.games_images('218')
95
+ end
96
+
97
+ it 'should return logo and boxart' do
98
+ expect(@images[:boxart].count).wont_be :<, 0
99
+ expect(@images[:logo]).must_be_kind_of String
100
+ expect(@images[:boxart][:front][:url]).must_be_kind_of String
101
+ expect(@images[:boxart][:front][:width]).must_be_kind_of String
102
+ expect(@images[:boxart][:front][:height]).must_be_kind_of String
103
+ expect(@images[:boxart][:front][:resolution]).must_be_kind_of String
104
+ end
105
+
106
+ it 'should return screenshots' do
107
+ expect(@images[:screenshot].count).wont_be :<, 0
108
+ expect(@images[:screenshot].first).must_be_kind_of Hash
109
+ expect(@images[:screenshot].first[:filename]).must_be_kind_of String
110
+ end
111
+
112
+ it 'should return fanart' do
113
+ expect(@images[:fanart].count).wont_be :<, 0
114
+ expect(@images[:fanart].first[:url]).must_be_kind_of String
115
+ expect(@images[:fanart].first[:width]).must_be_kind_of String
116
+ expect(@images[:fanart].first[:height]).must_be_kind_of String
117
+ expect(@images[:fanart].first[:resolution]).must_be_kind_of String
118
+ end
119
+ end
120
+
121
+ describe 'when some art is missing' do
122
+ before do
123
+ @images = client.games_images(65_238)
124
+ end
125
+
126
+ it 'should return an empty array' do
127
+ expect(@images).must_be_kind_of Hash
128
+ end
129
+ end
130
+
131
+ describe 'update games' do
132
+ it 'should return updates' do
133
+ updates = client.games_update(1)
134
+
135
+ expect(updates[:updates].count).must_equal 100
136
+ expect(updates[:updates].first).must_equal(
137
+ {
138
+ 'edit_id' => 2,
139
+ 'game_id' => 38_113,
140
+ 'timestamp' => '2018-06-28 16:20:54',
141
+ 'type' => 'boxart',
142
+ 'value' => 'boxart/front/38113-1.jpg'
143
+ }
144
+ )
145
+ expect(updates[:updates].last).must_equal(
146
+ {
147
+ 'edit_id' => 101,
148
+ 'game_id' => 22_208,
149
+ 'timestamp' => '2018-06-29 02:36:38',
150
+ 'type' => 'rating',
151
+ 'value' => 'E - Everyone'
152
+ }
153
+ )
154
+ expect(updates[:previous_page]).must_be_nil
155
+ expect(updates[:next_page]).must_equal 2
156
+ end
157
+
158
+ it 'should return updates when using a page parameter' do
159
+ updates = client.games_update(1, { page: 1_000 })
160
+
161
+ expect(updates[:updates].count).must_equal 100
162
+ expect(updates[:previous_page]).must_equal 999
163
+ expect(updates[:next_page]).must_equal 1001
164
+ end
61
165
  end
62
166
  end
63
167
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './test_helper'
4
+
5
+ describe 'Gamesdb - genres', :vcr do
6
+ let(:client) { Gamesdb::Client.new(ENV['GAMESDB_API_KEY']) }
7
+
8
+ describe 'genres' do
9
+ it 'should return the genres' do
10
+ @genres = client.genres
11
+
12
+ expect(@genres.count).must_equal 29
13
+ expect(@genres.min_by { |g| g['id'] }).must_equal({ 'id' => 1, 'name' => 'Action' })
14
+ end
15
+ end
16
+ end
@@ -1,94 +1,147 @@
1
- describe "GamesDB - platforms", :vcr do
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './test_helper'
4
+
5
+ describe 'GamesDB - platforms', :vcr do
6
+ let(:client) { Gamesdb::Client.new(ENV['GAMESDB_API_KEY']) }
7
+
2
8
  describe 'platforms' do
3
9
  before do
4
- @platforms = Gamesdb.platforms
10
+ @platforms = client.platforms
5
11
  end
6
12
 
7
13
  it 'should get gaming platforms' do
8
- @platforms.count.wont_be :<, 0
14
+ expect(@platforms.count).wont_be :<, 0
15
+ expect(@platforms.count).must_equal 114
9
16
  end
10
17
 
11
18
  it 'should have a valid name' do
12
- @platforms[0][:name].must_be_kind_of String
19
+ expect(@platforms[0][:name]).must_be_kind_of String
13
20
  end
14
21
 
15
22
  it 'should have a valid id' do
16
- @platforms[0][:id].must_be_kind_of Integer
23
+ expect(@platforms[0][:id]).must_be_kind_of Integer
17
24
  end
18
25
 
19
- it 'should have a valid slug' do
20
- @platforms[0][:slug].must_be_kind_of String
26
+ it 'should have a valid alias' do
27
+ expect(@platforms[0][:alias]).must_be_kind_of String
28
+ end
29
+
30
+ it 'should have valid fields for other stuff' do
31
+ nes = @platforms.select { |p| p[:id] == 7 }.first
32
+ expect(nes[:icon]).must_be_kind_of String
33
+ expect(nes[:console]).must_be_kind_of String
34
+ expect(nes[:controller]).must_be_kind_of String
35
+ expect(nes[:developer]).must_be_kind_of String
36
+ expect(nes[:manufacturer]).must_be_kind_of String
37
+ expect(nes[:maxcontrollers]).must_be_kind_of String
38
+ expect(nes[:cpu]).must_be_kind_of String
39
+ expect(nes[:memory]).must_be_kind_of String
40
+ expect(nes[:sound]).must_be_kind_of String
41
+ expect(nes[:display]).must_be_kind_of String
42
+ expect(nes[:overview]).must_be_kind_of String
21
43
  end
22
44
  end
23
45
 
24
- describe 'platform_games' do
46
+ describe 'games by platform' do
25
47
  before do
26
- platforms = Gamesdb.platforms
27
- @games_by_id = Gamesdb.platform_games(platforms[0][:id])
28
- @games_by_slug = Gamesdb.platform_games(platforms[0][:slug])
48
+ platforms = client.platforms
49
+ @first_page = client.games_by_platform_id(platforms[0][:id])
50
+ @second_page = client.games_by_platform_id(platforms[0][:id], 2)
29
51
  end
30
52
 
31
53
  it 'should return games in platform by id' do
32
- @games_by_id.count.wont_be :<, 0
54
+ expect(@first_page.count).wont_be :<, 0
55
+ expect(@first_page.count).must_equal 20
56
+ end
57
+
58
+ it 'should return games in the platform for the second page' do
59
+ expect(@second_page.count).wont_be :<, 0
60
+ expect(@second_page.count).must_equal 20
61
+ expect(@first_page & @second_page).must_equal []
62
+ end
63
+ end
64
+
65
+ describe 'games by platform parameters' do
66
+ before do
67
+ @games1 = client.games_by_platform_id(4950)
68
+ @games2 = client.games_by_platform_id(4948)
69
+ @games3 = client.games_by_platform_id('4950,4948')
33
70
  end
34
71
 
35
- it 'should return games in platform' do
36
- @games_by_slug.count.wont_be :<, 0
72
+ it 'supports comma separated list' do
73
+ expect(@games1.count).must_equal 20
74
+ expect(@games2.count).must_equal 1
75
+ expect(@games3.count).must_equal 20
76
+
77
+ expect(@games3 - @games1).must_equal @games2
37
78
  end
38
79
  end
39
80
 
40
81
  describe 'platform' do
41
82
  describe 'assigning basic info' do
42
83
  before do
43
- @platform = Gamesdb.platform 6
84
+ @platform = client.platforms_by_id(6)
44
85
  end
45
86
 
46
87
  it 'should return valid platform info' do
47
- @platform[:name].must_equal 'Super Nintendo (SNES)'
48
- @platform[:overview].must_be_kind_of String
49
- @platform[:developer].must_be_kind_of String
50
- @platform[:manufacturer].must_equal 'Nintendo'
51
- @platform[:cpu].must_be_kind_of String
52
- @platform[:memory].must_be_kind_of String
53
- @platform[:sound].must_be_kind_of String
54
- @platform[:display].must_be_kind_of String
55
- end
56
-
57
- it 'should assign images if provided' do
58
- images = @platform[:Images]
59
- images[:boxart][1].must_equal 'platform/boxart/6-2.jpg'
60
- images[:boxart].first[:width].must_equal '500'
61
- images[:boxart].first[:height].must_equal '750'
62
- images[:consoleart].must_equal 'platform/consoleart/6.png'
63
- images[:controllerart].must_equal 'platform/controllerart/6.png'
88
+ expect(@platform[:name]).must_equal 'Super Nintendo (SNES)'
89
+ expect(@platform[:overview]).must_be_kind_of String
90
+ expect(@platform[:developer]).must_be_kind_of String
91
+ expect(@platform[:manufacturer]).must_equal 'Nintendo'
92
+ expect(@platform[:cpu]).must_be_kind_of String
93
+ expect(@platform[:memory]).must_be_kind_of String
94
+ expect(@platform[:sound]).must_be_kind_of String
95
+ expect(@platform[:display]).must_be_kind_of String
64
96
  end
65
97
  end
66
98
 
67
99
  describe 'without hardware or images' do
68
100
  before do
69
- @platform = Gamesdb.platform 4916
101
+ @platform = client.platforms_by_id(4916)
70
102
  end
71
103
 
72
104
  it 'should return valid platform info' do
73
- @platform[:name].must_equal 'Android'
74
- @platform[:overview].must_be_kind_of String
75
- @platform[:developer].must_be_kind_of String
76
- @platform[:manufacturer].must_be_nil
77
- @platform[:cpu].must_be_nil
78
- @platform[:memory].must_be_nil
79
- @platform[:sound].must_be_nil
80
- @platform[:display].must_be_nil
105
+ expect(@platform[:name]).must_equal 'Android'
106
+ expect(@platform[:overview]).must_be_kind_of String
107
+ expect(@platform[:developer]).must_be_kind_of String
108
+ expect(@platform[:manufacturer]).must_be_nil
109
+ expect(@platform[:cpu]).must_be_nil
110
+ expect(@platform[:memory]).must_be_nil
111
+ expect(@platform[:sound]).must_be_nil
112
+ expect(@platform[:display]).must_be_nil
81
113
  end
114
+ end
115
+ end
82
116
 
83
- it 'should not fail hard if no images are provided' do
84
- images = @platform[:Images]
117
+ describe 'platforms by name' do
118
+ it 'should return platforms' do
119
+ @platforms = client.platforms_by_name('Nintendo')
85
120
 
86
- images[:boxart][1].must_equal 'platform/boxart/4916-2.jpg'
87
- images[:boxart].first[:width].must_equal '820'
88
- images[:boxart].first[:height].must_equal '1080'
89
- images[:consoleart].must_be_nil
90
- images[:controllerart].must_be_nil
91
- end
121
+ expect(@platforms.count).must_equal 14
122
+ names = @platforms.sort_by { |p| p[:name] }.map { |p| p[:name] }
123
+ expect(names).must_equal [
124
+ 'Nintendo 3DS', 'Nintendo 64', 'Nintendo DS', 'Nintendo Entertainment System (NES)',
125
+ 'Nintendo Game Boy', 'Nintendo Game Boy Advance', 'Nintendo Game Boy Color', 'Nintendo GameCube',
126
+ 'Nintendo Pokémon Mini', 'Nintendo Switch', 'Nintendo Virtual Boy', 'Nintendo Wii', 'Nintendo Wii U',
127
+ 'Super Nintendo (SNES)'
128
+ ]
129
+ end
130
+ end
131
+
132
+ describe 'platform images' do
133
+ it 'should return all images for a platform' do
134
+ @images = client.platform_images(7)
135
+
136
+ expect(@images.count).must_equal(10)
137
+ expect(@images.first.keys).must_equal(['id', 'type', 'filename'])
138
+ end
139
+
140
+ it 'should filter image type for a platform' do
141
+ @images = client.platform_images(7, type: 'boxart')
142
+
143
+ expect(@images.count).must_equal(1)
144
+ expect(@images.first).must_equal({ 'id' => 222, 'type' => 'boxart', 'filename' => 'platform/boxart/7-2.jpg' })
92
145
  end
93
146
  end
94
147
  end