thegamesdb 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f764717699d2e66db9ff42a82e6077ca5499fb2e2e27215276492c135bad22ad
4
- data.tar.gz: ef3c70003d768e26fcb5f6f21a1ca42279a8671b83c12072624a2881da026207
3
+ metadata.gz: 4dff06dfb7c9c5737a8f737a1dcf365cdd3c13ac9f7af2efef21d52548d91f2b
4
+ data.tar.gz: a6aa0de586fddb22974910bd6937a18e228209adc9555ee795e254e3bcbc6f84
5
5
  SHA512:
6
- metadata.gz: 0467cf3be3a2e1838f04c50536a985776ff96cb4e13616ac9560cacb02f298cc952972ea59322563d3ade61dc6847e4fb7facf2b8fc57f87aaa60fa6a3c1f15d
7
- data.tar.gz: 8d6d965a417ca7a64b85a8c41932ea683ce879acb01eb515c91c5e52d28454c265fa2d179fdb6e74bb3aeafb3dbf4f5c553c5fceaf93fc8285081158ad57590a
6
+ metadata.gz: d37e33b44631f031287f6047b79f15fa7d0c112ee1d021724693020e796e9936c2d5b95b2cf0933b4fe0092467bf4c19ca85ca10bf3196382213d6f2f7d21475
7
+ data.tar.gz: 154aebc525bdec5ae98b1209d42ad1edc1101f354c7a47607405870888d37fd18bc0303da4cb9053cc0ecd8aa02025b5102509d609e8c298859d5669a64f62e0
@@ -2,10 +2,10 @@ name: Tests
2
2
  on:
3
3
  push:
4
4
  branches:
5
- - master
5
+ - main
6
6
  pull_request:
7
7
  branches:
8
- - master
8
+ - main
9
9
  jobs:
10
10
  build:
11
11
  env:
@@ -14,7 +14,7 @@ jobs:
14
14
  strategy:
15
15
  fail-fast: false
16
16
  matrix:
17
- ruby: [ '2.5', '2.6', '2.7', 'jruby', 'truffleruby' ]
17
+ ruby: [ '3.0', '3.1', '3.2', 'jruby', 'truffleruby' ]
18
18
  name: Ruby ${{ matrix.ruby }}
19
19
  steps:
20
20
  - uses: actions/checkout@v2
@@ -2,10 +2,10 @@ name: Rubocop
2
2
  on:
3
3
  push:
4
4
  branches:
5
- - master
5
+ - main
6
6
  pull_request:
7
7
  branches:
8
- - master
8
+ - main
9
9
 
10
10
  jobs:
11
11
  build:
@@ -14,7 +14,7 @@ jobs:
14
14
  - uses: actions/checkout@v2
15
15
  - uses: ruby/setup-ruby@v1
16
16
  with:
17
- ruby-version: 2.7
17
+ ruby-version: 3.0
18
18
  - run: |
19
19
  bundle install
20
20
  bundle exec rubocop
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.5
2
+ TargetRubyVersion: 2.7
3
3
  Lint/DuplicateBranch: # (new in 1.3)
4
4
  Enabled: true
5
5
  Lint/DuplicateRegexpCharacterClassElement: # (new in 1.1)
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.1.0
4
+
5
+ Now testing Ruby versions 3.0, 3.1, 3.2, JRuby and TruffleRuby.
6
+
7
+ - Added some better error handling. Now you can catch from `Gamesdb::Error` if there's any response from the server with status >= 300 (e.g. 403 for wrong API Key). I'll improve on this in future releases.
8
+ - Refactored main client class and extracted utilitary functions into `Gamesdb::Utils`.
9
+
3
10
  ## 2.0.0
4
11
 
5
12
  Refactored code and functionality. The library was refactored in a way you need to instantiate a client with the API key to use it (more info in "Breaking changes"). 100% of the documented API is now supported and implemented.
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gamesdb
4
+ # Gamesdb errors
5
+ class Error < StandardError
6
+ def initialize(code, status)
7
+ @code = code
8
+ @status = status
9
+ super("Gamesdb: #{code} - #{status}")
10
+ end
11
+ end
12
+ end
@@ -41,9 +41,9 @@ module Gamesdb
41
41
  return [] if (data['data']['count']).zero?
42
42
 
43
43
  games = data['data']['games']
44
- return symbolize_keys(games.first) if games.count == 1
44
+ return Gamesdb::Utils.symbolize_keys(games.first) if games.count == 1
45
45
 
46
- games.map { |game| symbolize_keys(game) }
46
+ games.map { |game| Gamesdb::Utils.symbolize_keys(game) }
47
47
  end
48
48
  # rubocop:enable Metrics/MethodLength
49
49
 
@@ -95,10 +95,10 @@ module Gamesdb
95
95
 
96
96
  response = {}
97
97
  response[:base_url] = data['data']['base_url']['original']
98
- response[:logo] = process_logo(data['data'], id)
99
- response[:boxart] = process_covers(data['data'], id)
100
- response[:screenshot] = process_screenshots(data['data'], id)
101
- response[:fanart] = process_fanart(data['data'], id)
98
+ response[:logo] = Gamesdb::Utils.process_logo(data['data'], id)
99
+ response[:boxart] = Gamesdb::Utils.process_covers(data['data'], id)
100
+ response[:screenshot] = Gamesdb::Utils.process_screenshots(data['data'], id)
101
+ response[:fanart] = Gamesdb::Utils.process_fanart(data['data'], id)
102
102
  response
103
103
  end
104
104
 
@@ -17,7 +17,7 @@ module Gamesdb
17
17
  data = perform_request(url, params)
18
18
 
19
19
  data['data']['platforms'].map do |p|
20
- symbolize_keys(p.last)
20
+ Gamesdb::Utils.symbolize_keys(p.last)
21
21
  end
22
22
  end
23
23
 
@@ -93,9 +93,9 @@ module Gamesdb
93
93
 
94
94
  response = case platforms
95
95
  when Hash
96
- platforms.map { |_k, platform| symbolize_keys(platform) }
96
+ platforms.map { |_k, platform| Gamesdb::Utils.symbolize_keys(platform) }
97
97
  when Array
98
- platforms.map { |platform| symbolize_keys(platform) }
98
+ platforms.map { |platform| Gamesdb::Utils.symbolize_keys(platform) }
99
99
  end
100
100
 
101
101
  return response.first if response.count == 1
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gamesdb
4
+ # Several reusable functions
5
+ module Utils
6
+ class << self
7
+ def process_logo(data, id)
8
+ logo = data['images'][id.to_s].select { |a| a['type'] == 'clearlogo' }
9
+ logo.empty? ? '' : logo.first['filename']
10
+ end
11
+
12
+ def process_fanart(data, id)
13
+ fanart = select_images(data, id, 'fanart')
14
+ return [] if fanart.empty?
15
+
16
+ fanart.map { |art| build_individual_fanart(art) }
17
+ end
18
+
19
+ def process_covers(data, id)
20
+ covers = {}
21
+ boxart = select_images(data, id, 'boxart')
22
+ return [] if boxart.empty?
23
+
24
+ boxart.each do |art|
25
+ width, height = art['resolution'].split('x') unless art['resolution'].nil?
26
+ covers[art['side'].to_sym] = art_structure(art, width, height)
27
+ end
28
+ covers
29
+ end
30
+
31
+ def build_individual_fanart(art)
32
+ width, height = art['resolution'].split('x') unless art['resolution'].nil?
33
+ art_structure(art, width, height)
34
+ end
35
+
36
+ def art_structure(art, width, height)
37
+ {
38
+ url: art['filename'],
39
+ resolution: art['resolution'],
40
+ width: width,
41
+ height: height
42
+ }
43
+ end
44
+
45
+ def process_screenshots(data, id)
46
+ select_images(data, id, 'screenshot').map do |b|
47
+ Gamesdb::Utils.symbolize_keys(b)
48
+ end
49
+ end
50
+
51
+ def select_images(data, id, image_type)
52
+ data['images'][id.to_s].select do |a|
53
+ a['type'] == image_type
54
+ end
55
+ end
56
+
57
+ def symbolize_keys(hash)
58
+ new_hash = {}
59
+ hash.each_key do |key|
60
+ new_hash[key.to_sym] = hash.delete(key)
61
+ end
62
+ new_hash
63
+ end
64
+ end
65
+ end
66
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Gem version
4
4
  module Gamesdb
5
- VERSION = '2.0.0'
5
+ VERSION = '2.1.0'
6
6
  end
data/lib/thegamesdb.rb CHANGED
@@ -2,10 +2,12 @@
2
2
 
3
3
  require 'thegamesdb/version'
4
4
  require 'thegamesdb/developers'
5
+ require 'thegamesdb/error'
5
6
  require 'thegamesdb/games'
6
7
  require 'thegamesdb/genres'
7
8
  require 'thegamesdb/platforms'
8
9
  require 'thegamesdb/publishers'
10
+ require 'thegamesdb/utils'
9
11
  require 'net/http'
10
12
  require 'json'
11
13
 
@@ -42,10 +44,11 @@ module Gamesdb
42
44
  uri = URI(BASE_URL + url)
43
45
  uri.query = URI.encode_www_form(params)
44
46
  response = JSON.parse(Net::HTTP.get_response(uri).body)
47
+ http_error(response) if response['code'] >= 300
48
+
45
49
  refresh_allowances(response)
46
50
  response
47
51
  rescue StandardError => e
48
- # TODO: Handle errors
49
52
  raise e
50
53
  end
51
54
 
@@ -57,52 +60,9 @@ module Gamesdb
57
60
  @allowance_refresh_timer = response['allowance_refresh_timer']
58
61
  end
59
62
 
60
- def process_logo(data, id)
61
- logo = data['images'][id.to_s].select { |a| a['type'] == 'clearlogo' }
62
- logo.empty? ? '' : logo.first['filename']
63
- end
64
-
65
- def process_fanart(data, id)
66
- fanart = select_images(data, id, 'fanart')
67
- return [] if fanart.empty?
68
-
69
- fanart.map { |art| build_individual_fanart(art) }
70
- end
71
-
72
- def process_covers(data, id)
73
- covers = {}
74
- boxart = select_images(data, id, 'boxart')
75
- return [] if boxart.empty?
76
-
77
- boxart.each do |art|
78
- width, height = art['resolution'].split('x') unless art['resolution'].nil?
79
- covers[art['side'].to_sym] = art_structure(art, width, height)
80
- end
81
- covers
82
- end
83
-
84
- def build_individual_fanart(art)
85
- width, height = art['resolution'].split('x') unless art['resolution'].nil?
86
- art_structure(art, width, height)
87
- end
88
-
89
- def art_structure(art, width, height)
90
- {
91
- url: art['filename'],
92
- resolution: art['resolution'],
93
- width: width,
94
- height: height
95
- }
96
- end
97
-
98
- def process_screenshots(data, id)
99
- select_images(data, id, 'screenshot').map { |b| symbolize_keys(b) }
100
- end
101
-
102
- def select_images(data, id, image_type)
103
- data['images'][id.to_s].select do |a|
104
- a['type'] == image_type
105
- end
63
+ # TODO: More granular errors
64
+ def http_error(response)
65
+ raise Gamesdb::Error.new(response['code'], response['status'])
106
66
  end
107
67
 
108
68
  # Process games for platform_games
@@ -134,13 +94,5 @@ module Gamesdb
134
94
  end
135
95
  # rubocop:enable Metrics/AbcSize
136
96
  # rubocop:enable Metrics/MethodLength
137
-
138
- def symbolize_keys(hash)
139
- new_hash = {}
140
- hash.each_key do |key|
141
- new_hash[key.to_sym] = hash.delete(key)
142
- end
143
- new_hash
144
- end
145
97
  end
146
98
  end
data/test/client_test.rb CHANGED
@@ -12,5 +12,15 @@ describe 'Gamesdb - client', :vcr do
12
12
  client.games_by_id(1527)
13
13
  expect(client.remaining_monthly_allowance).must_equal(monthly - 1)
14
14
  end
15
+
16
+ describe 'errors' do
17
+ let(:client) { Gamesdb::Client.new('invalid_api_key') }
18
+
19
+ it 'should raise http error' do
20
+ assert_raises Gamesdb::Error do
21
+ client.games_by_id(1904)
22
+ end
23
+ end
24
+ end
15
25
  end
16
26
  end
@@ -9,7 +9,7 @@ describe 'Gamesdb - developers', :vcr do
9
9
  it 'should return the developers' do
10
10
  @developers = client.developers
11
11
 
12
- expect(@developers.count).must_equal 8168
12
+ expect(@developers.count.positive?)
13
13
  expect(@developers.first.keys).must_equal(['id', 'name'])
14
14
  end
15
15
  end
data/test/games_test.rb CHANGED
@@ -137,7 +137,7 @@ describe 'Gamesdb - games', :vcr do
137
137
  {
138
138
  'edit_id' => 2,
139
139
  'game_id' => 38_113,
140
- 'timestamp' => '2018-06-28 16:20:54',
140
+ 'timestamp' => '2018-06-28 15:20:54',
141
141
  'type' => 'boxart',
142
142
  'value' => 'boxart/front/38113-1.jpg'
143
143
  }
@@ -146,7 +146,7 @@ describe 'Gamesdb - games', :vcr do
146
146
  {
147
147
  'edit_id' => 101,
148
148
  'game_id' => 22_208,
149
- 'timestamp' => '2018-06-29 02:36:38',
149
+ 'timestamp' => '2018-06-29 01:36:38',
150
150
  'type' => 'rating',
151
151
  'value' => 'E - Everyone'
152
152
  }
@@ -12,7 +12,7 @@ describe 'GamesDB - platforms', :vcr do
12
12
 
13
13
  it 'should get gaming platforms' do
14
14
  expect(@platforms.count).wont_be :<, 0
15
- expect(@platforms.count).must_equal 114
15
+ expect(@platforms.count).must_equal 147
16
16
  end
17
17
 
18
18
  it 'should have a valid name' do
@@ -71,7 +71,7 @@ describe 'GamesDB - platforms', :vcr do
71
71
 
72
72
  it 'supports comma separated list' do
73
73
  expect(@games1.count).must_equal 20
74
- expect(@games2.count).must_equal 1
74
+ expect(@games2.count).must_equal 20
75
75
  expect(@games3.count).must_equal 20
76
76
 
77
77
  expect(@games3 - @games1).must_equal @games2
@@ -9,7 +9,7 @@ describe 'GamesDB - Publishers', :vcr do
9
9
  it 'should return publishers' do
10
10
  publishers = client.publishers
11
11
 
12
- expect(publishers.count).must_equal 4371
12
+ expect(publishers.count.positive?)
13
13
  expect(publishers.first.keys).must_equal(['id', 'name'])
14
14
  end
15
15
  end
data/thegamesdb.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
21
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
22
  spec.require_paths = ['lib']
23
- spec.required_ruby_version = '> 2.5.0'
23
+ spec.required_ruby_version = '> 2.7.0'
24
24
  spec.add_development_dependency 'bundler'
25
25
  spec.add_development_dependency 'byebug' unless defined?(JRUBY_VERSION)
26
26
  spec.add_development_dependency 'minitest-reporters'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thegamesdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fernando Briano
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-30 00:00:00.000000000 Z
11
+ date: 2023-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -129,7 +129,7 @@ executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
- - ".github/workflows/master.yml"
132
+ - ".github/workflows/main.yml"
133
133
  - ".github/workflows/rubocop.yml"
134
134
  - ".gitignore"
135
135
  - ".rubocop.yml"
@@ -140,11 +140,13 @@ files:
140
140
  - Rakefile
141
141
  - lib/thegamesdb.rb
142
142
  - lib/thegamesdb/developers.rb
143
+ - lib/thegamesdb/error.rb
143
144
  - lib/thegamesdb/games.rb
144
145
  - lib/thegamesdb/genres.rb
145
146
  - lib/thegamesdb/images.rb
146
147
  - lib/thegamesdb/platforms.rb
147
148
  - lib/thegamesdb/publishers.rb
149
+ - lib/thegamesdb/utils.rb
148
150
  - lib/thegamesdb/version.rb
149
151
  - test/client_test.rb
150
152
  - test/developers_test.rb
@@ -171,14 +173,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
171
173
  requirements:
172
174
  - - ">"
173
175
  - !ruby/object:Gem::Version
174
- version: 2.5.0
176
+ version: 2.7.0
175
177
  required_rubygems_version: !ruby/object:Gem::Requirement
176
178
  requirements:
177
179
  - - ">="
178
180
  - !ruby/object:Gem::Version
179
181
  version: '0'
180
182
  requirements: []
181
- rubygems_version: 3.0.8
183
+ rubygems_version: 3.3.7
182
184
  signing_key:
183
185
  specification_version: 4
184
186
  summary: Client for TheGamesDB API (thegamesdb.net).