class Gamesdb::Client
Client
for TheGamesDB API (thegamesdb.net)
Constants
- BASE_URL
- IMAGES_BASE_URL
Attributes
allowance_refresh_timer[R]
extra_allowance[R]
remaining_monthly_allowance[R]
Public Class Methods
new(api_key)
click to toggle source
# File lib/thegamesdb.rb, line 26 def initialize(api_key) @api_key = api_key end
Public Instance Methods
perform_request(url, params = {})
click to toggle source
Perform request
Used by every API endpoint, but can also be used manually.
@param url [String] Required @param params [Hash] optional
@return [Hash] Parsed JSON response
# File lib/thegamesdb.rb, line 38 def perform_request(url, params = {}) raise ArgumentError, 'You need to set the API KEY to use the GamesDB API' unless @api_key params = params.merge({ apikey: @api_key }) uri = URI(BASE_URL + url) uri.query = URI.encode_www_form(params) response = JSON.parse(Net::HTTP.get_response(uri).body) refresh_allowances(response) response rescue StandardError => e # TODO: Handle errors raise e end
Private Instance Methods
art_structure(art, width, height)
click to toggle source
# File lib/thegamesdb.rb, line 89 def art_structure(art, width, height) { url: art['filename'], resolution: art['resolution'], width: width, height: height } end
build_individual_fanart(art)
click to toggle source
# File lib/thegamesdb.rb, line 84 def build_individual_fanart(art) width, height = art['resolution'].split('x') unless art['resolution'].nil? art_structure(art, width, height) end
process_covers(data, id)
click to toggle source
# File lib/thegamesdb.rb, line 72 def process_covers(data, id) covers = {} boxart = select_images(data, id, 'boxart') return [] if boxart.empty? boxart.each do |art| width, height = art['resolution'].split('x') unless art['resolution'].nil? covers[art['side'].to_sym] = art_structure(art, width, height) end covers end
process_fanart(data, id)
click to toggle source
# File lib/thegamesdb.rb, line 65 def process_fanart(data, id) fanart = select_images(data, id, 'fanart') return [] if fanart.empty? fanart.map { |art| build_individual_fanart(art) } end
process_logo(data, id)
click to toggle source
# File lib/thegamesdb.rb, line 60 def process_logo(data, id) logo = data['images'][id.to_s].select { |a| a['type'] == 'clearlogo' } logo.empty? ? '' : logo.first['filename'] end
process_platform_games(data)
click to toggle source
Process games for platform_games rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
# File lib/thegamesdb.rb, line 111 def process_platform_games(data) data['data']['games'].map do |elem| { name: elem['game_title'], id: elem['id'], release_date: elem['release_date'], platform: elem['platform'], developers: elem['developers'], players: elem['players'], publishers: elem['publishers'], genres: elem['genres'], overview: elem['overview'], last_updated: elem['last_updated'], rating: elem['rating'], coop: elem['coop'], youtube: elem['youtube'], alternates: elem['alternates'], image: if (boxart = data.dig('include', 'boxart', 'data', elem['id'].to_s)) data['include']['boxart']['base_url']['original'] + boxart.select { |a| a['side'] == 'front' }.first['filename'] || '' end } end end
process_screenshots(data, id)
click to toggle source
# File lib/thegamesdb.rb, line 98 def process_screenshots(data, id) select_images(data, id, 'screenshot').map { |b| symbolize_keys(b) } end
refresh_allowances(response)
click to toggle source
# File lib/thegamesdb.rb, line 54 def refresh_allowances(response) @remaining_monthly_allowance = response['remaining_monthly_allowance'] @extra_allowance = response['extra_allowance'] @allowance_refresh_timer = response['allowance_refresh_timer'] end
select_images(data, id, image_type)
click to toggle source
# File lib/thegamesdb.rb, line 102 def select_images(data, id, image_type) data['images'][id.to_s].select do |a| a['type'] == image_type end end
symbolize_keys(hash)
click to toggle source
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength
# File lib/thegamesdb.rb, line 138 def symbolize_keys(hash) new_hash = {} hash.each_key do |key| new_hash[key.to_sym] = hash.delete(key) end new_hash end