Skip to content

Commit

Permalink
Adds platform id parameter to games_by_name
Browse files Browse the repository at this point in the history
Adds extra fields and boxart to games_by_name response
Refactors process_platform_games
  • Loading branch information
picandocodigo committed Sep 11, 2019
1 parent a16abdd commit 5635428
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
48 changes: 35 additions & 13 deletions lib/thegamesdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,25 @@ def self.game_by_id(id)
#
# Parameters:
# - name (required)
# - platform (optional - platform id)
#
# == Returns:
# Hash with game info: id, name (not-unique), release_date,
# platform
# platform, etc.
#
def self.games_by_name(name)
def self.games_by_name(name, platform: nil)
url = 'Games/ByGameName'
data = json_response(url, name: name)
data['data']['games'].map { |game| symbolize_keys(game) }
params = {
fields: 'players,publishers,genres,overview,last_updated,rating,platform,coop,youtube,os,processor,ram,hdd,video,sound,alternates',
include: 'boxart',
name: name
}
unless platform.nil?
params.merge!("filter[platform]" => platform)
end

data = json_response(url, params)
process_platform_games(data)
end

# This API feature returns a list of available artwork types and
Expand All @@ -119,7 +129,7 @@ def self.games_by_name(name)
def self.game_images(id)
url = 'Games/Images'
data = json_response(url, games_id: id)
return [] if data['data']['count'] == 0
return [] if data.dig('data', 'count') == (0 || nil)

response = {}
response[:base_url] = data['data']['base_url']['original']
Expand Down Expand Up @@ -199,15 +209,27 @@ def self.process_platform_games(data)
games = []

data['data']['games'].each do |elem|
name = elem['game_title']
id = elem['id']
date = elem['release_date']
developers = elem['developers']
if boxart = data.dig('include', 'boxart', 'data', id.to_s)
image = data['include']['boxart']['base_url']['original'] +
boxart.select { |a| a['side'] == 'front' }.first['filename'] || ''
end
games << { name: name, id: id, release_date: date, developers: developers, image: image }
games << {
name: elem['game_title'],
id: 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', id.to_s)
data['include']['boxart']['base_url']['original'] +
boxart.select { |a| a['side'] == 'front' }.first['filename'] || ''
end
}
end
games
end
Expand Down
17 changes: 16 additions & 1 deletion test/games_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,27 @@
it 'should return a list' do
game = @games_list.first
game[:id].must_be_kind_of Integer
game[:game_title].must_be_kind_of String
game[:name].must_be_kind_of String
game[:platform].must_be_kind_of Integer
game[:release_date].must_be_kind_of String
end
end

describe 'games by name and platform' do
before do
@games_list = Gamesdb.games_by_name('mario', platform: 7)
end

it 'should return a list' do
@games_list.each do |game|
game[:id].must_be_kind_of Integer
game[:id].must_be_kind_of Integer
game[:name].must_be_kind_of String
game[:platform].must_equal 7
end
end
end

describe 'games art', :vcr do
describe 'when most of the art is available' do
before do
Expand Down

0 comments on commit 5635428

Please sign in to comment.