thegamesdb 1.1.2 → 2.1.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,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gamesdb
4
+ # Developers related API Endpoints
5
+ module Developers
6
+ # Fetches Developers list
7
+ #
8
+ # @see https://api.thegamesdb.net/#/Developers/Developers
9
+ #
10
+ # @return Array of Hashes with id and name as keys
11
+ def developers
12
+ url = 'Developers'
13
+ data = perform_request(url)
14
+ data['data']['developers'].map { |_id, developer| developer }
15
+ end
16
+ end
17
+ end
@@ -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
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gamesdb
4
+ # Games related API Endpoints
5
+ module Games
6
+ # Method for listing platform's games
7
+ #
8
+ # @see https://api.thegamesdb.net/#/Games/GamesByPlatformID
9
+ #
10
+ # @param id [Integer]
11
+ # @param page [Integer]
12
+ #
13
+ # @return [Array] Array of Hashes with games info
14
+ #
15
+ def games_by_platform_id(platform_id, page = 1)
16
+ url = 'Games/ByPlatformID'
17
+ params = { id: platform_id, page: page, include: 'boxart' }
18
+ data = perform_request(url, params)
19
+ process_platform_games(data)
20
+ end
21
+
22
+ # Method for getting game info
23
+ #
24
+ # @see https://api.thegamesdb.net/#/Games/GamesByGameID
25
+ #
26
+ # @param id [Integer|String] Game id or string of ',' delimited list
27
+ #
28
+ # @return [Array|Hash] Hash with game info
29
+ #
30
+ # rubocop:disable Metrics/MethodLength
31
+ def games_by_id(id)
32
+ url = 'Games/ByGameID'
33
+ params = {
34
+ id: id,
35
+ fields:
36
+ 'players,publishers,genres,overview,last_updated,rating,platform,coop,youtube,os,processor,ram,hdd,'\
37
+ 'video,sound,alternates',
38
+ include: 'boxart,platform'
39
+ }
40
+ data = perform_request(url, params)
41
+ return [] if (data['data']['count']).zero?
42
+
43
+ games = data['data']['games']
44
+ return Gamesdb::Utils.symbolize_keys(games.first) if games.count == 1
45
+
46
+ games.map { |game| Gamesdb::Utils.symbolize_keys(game) }
47
+ end
48
+ # rubocop:enable Metrics/MethodLength
49
+
50
+ # The GetGamesList API search returns a listing of games matched up with loose search terms.
51
+ #
52
+ # @see https://api.thegamesdb.net/#/Games/GamesByGameName
53
+ #
54
+ # @param name [String] game name (required)
55
+ # @param platform [Integer] (optional - platform id)
56
+ # @param page [Integer] (optional)
57
+ #
58
+ # @return [Hash] Hash with game info: id, name (not-unique), release_date, platform, etc.
59
+ #
60
+ # rubocop:disable Metrics/MethodLength
61
+ def games_by_name(name, platform: nil, page: 1)
62
+ url = 'Games/ByGameName'
63
+ params = {
64
+ fields:
65
+ 'players,publishers,genres,overview,last_updated,rating,platform,coop,youtube,os,processor,ram,hdd'\
66
+ ',video,sound,alternates',
67
+ include: 'boxart',
68
+ name: name,
69
+ page: page
70
+ }
71
+ params.merge!('filter[platform]' => platform) unless platform.nil?
72
+
73
+ data = perform_request(url, params)
74
+ process_platform_games(data)
75
+ end
76
+ # rubocop:enable Metrics/MethodLength
77
+
78
+ # This API feature returns a list of available artwork types and
79
+ # locations specific to the requested game id in the database. It
80
+ # also lists the resolution of any images available. Scrapers can be
81
+ # set to use a minimum or maximum resolution for specific images
82
+ #
83
+ # @see https://api.thegamesdb.net/#/Games/GamesImages
84
+ #
85
+ # @param id [Integer] The numeric ID of the game in Gamesdb that you like to fetch artwork details for
86
+ #
87
+ # @return [Hash] Hash with game art info: fanart (array), boxart (Hash,
88
+ # :front, :back), screenshots (array), fanart (array)
89
+ #
90
+ # rubocop:disable Metrics/AbcSize
91
+ def games_images(id)
92
+ url = 'Games/Images'
93
+ data = perform_request(url, games_id: id)
94
+ return [] if data.dig('data', 'count') == (0 || nil)
95
+
96
+ response = {}
97
+ response[:base_url] = data['data']['base_url']['original']
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
+ response
103
+ end
104
+
105
+ # Fetch games update
106
+ #
107
+ # @see https://api.thegamesdb.net/#/Games/GamesUpdates
108
+ #
109
+ # @param last_edit_id [Integer] Required
110
+ # @param time [Integer] (optional)
111
+ # @param page [Integer] results page offset to return (optional)
112
+ # rubocop:disable Metrics/CyclomaticComplexity
113
+ # rubocop:disable Metrics/PerceivedComplexity
114
+ def games_update(last_edit_id, arguments = {})
115
+ url = 'Games/Updates'
116
+ params = arguments.merge({ last_edit_id: last_edit_id })
117
+ data = perform_request(url, params)
118
+
119
+ regexp = /page=([0-9]+)/
120
+ response = {}
121
+ response[:updates] = data['data']['updates']
122
+ response[:previous_page] = data.dig('pages', 'previous')&.match(regexp)&.captures&.first&.to_i
123
+ response[:next_page] = data.dig('pages', 'next')&.match(regexp)&.captures&.first&.to_i
124
+ response
125
+ end
126
+ # rubocop:enable Metrics/AbcSize
127
+ # rubocop:enable Metrics/CyclomaticComplexity
128
+ # rubocop:enable Metrics/PerceivedComplexity
129
+ end
130
+ end
@@ -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
+ Gamesdb::Utils.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| Gamesdb::Utils.symbolize_keys(platform) }
97
+ when Array
98
+ platforms.map { |platform| Gamesdb::Utils.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
@@ -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
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Gem version
2
4
  module Gamesdb
3
- VERSION = '1.1.2'.freeze
5
+ VERSION = '2.1.0'
4
6
  end