Skip to content

Commit

Permalink
Refactors utility functions into own module
Browse files Browse the repository at this point in the history
  • Loading branch information
picandocodigo committed Jan 17, 2023
1 parent d6c2fb6 commit 4525ff6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 65 deletions.
57 changes: 1 addition & 56 deletions lib/thegamesdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'thegamesdb/genres'
require 'thegamesdb/platforms'
require 'thegamesdb/publishers'
require 'thegamesdb/utils'
require 'net/http'
require 'json'

Expand Down Expand Up @@ -64,54 +65,6 @@ def http_error(response)
raise Gamesdb::Error.new(response['code'], response['status'])
end

def process_logo(data, id)
logo = data['images'][id.to_s].select { |a| a['type'] == 'clearlogo' }
logo.empty? ? '' : logo.first['filename']
end

def process_fanart(data, id)
fanart = select_images(data, id, 'fanart')
return [] if fanart.empty?

fanart.map { |art| build_individual_fanart(art) }
end

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

def build_individual_fanart(art)
width, height = art['resolution'].split('x') unless art['resolution'].nil?
art_structure(art, width, height)
end

def art_structure(art, width, height)
{
url: art['filename'],
resolution: art['resolution'],
width: width,
height: height
}
end

def process_screenshots(data, id)
select_images(data, id, 'screenshot').map { |b| symbolize_keys(b) }
end

def select_images(data, id, image_type)
data['images'][id.to_s].select do |a|
a['type'] == image_type
end
end

# Process games for platform_games
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
Expand Down Expand Up @@ -141,13 +94,5 @@ def process_platform_games(data)
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength

def symbolize_keys(hash)
new_hash = {}
hash.each_key do |key|
new_hash[key.to_sym] = hash.delete(key)
end
new_hash
end
end
end
12 changes: 6 additions & 6 deletions lib/thegamesdb/games.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def games_by_id(id)
return [] if (data['data']['count']).zero?

games = data['data']['games']
return symbolize_keys(games.first) if games.count == 1
return Gamesdb::Utils.symbolize_keys(games.first) if games.count == 1

games.map { |game| symbolize_keys(game) }
games.map { |game| Gamesdb::Utils.symbolize_keys(game) }
end
# rubocop:enable Metrics/MethodLength

Expand Down Expand Up @@ -95,10 +95,10 @@ def games_images(id)

response = {}
response[:base_url] = data['data']['base_url']['original']
response[:logo] = process_logo(data['data'], id)
response[:boxart] = process_covers(data['data'], id)
response[:screenshot] = process_screenshots(data['data'], id)
response[:fanart] = process_fanart(data['data'], id)
response[:logo] = Gamesdb::Utils.process_logo(data['data'], id)
response[:boxart] = Gamesdb::Utils.process_covers(data['data'], id)
response[:screenshot] = Gamesdb::Utils.process_screenshots(data['data'], id)
response[:fanart] = Gamesdb::Utils.process_fanart(data['data'], id)
response
end

Expand Down
6 changes: 3 additions & 3 deletions lib/thegamesdb/platforms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def platforms
data = perform_request(url, params)

data['data']['platforms'].map do |p|
symbolize_keys(p.last)
Gamesdb::Utils.symbolize_keys(p.last)
end
end

Expand Down Expand Up @@ -93,9 +93,9 @@ def platform_api_response(data)

response = case platforms
when Hash
platforms.map { |_k, platform| symbolize_keys(platform) }
platforms.map { |_k, platform| Gamesdb::Utils.symbolize_keys(platform) }
when Array
platforms.map { |platform| symbolize_keys(platform) }
platforms.map { |platform| Gamesdb::Utils.symbolize_keys(platform) }
end

return response.first if response.count == 1
Expand Down
66 changes: 66 additions & 0 deletions lib/thegamesdb/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

module Gamesdb
# Several reusable functions
module Utils
class << self
def process_logo(data, id)
logo = data['images'][id.to_s].select { |a| a['type'] == 'clearlogo' }
logo.empty? ? '' : logo.first['filename']
end

def process_fanart(data, id)
fanart = select_images(data, id, 'fanart')
return [] if fanart.empty?

fanart.map { |art| build_individual_fanart(art) }
end

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

def build_individual_fanart(art)
width, height = art['resolution'].split('x') unless art['resolution'].nil?
art_structure(art, width, height)
end

def art_structure(art, width, height)
{
url: art['filename'],
resolution: art['resolution'],
width: width,
height: height
}
end

def process_screenshots(data, id)
select_images(data, id, 'screenshot').map do |b|
Gamesdb::Utils.symbolize_keys(b)
end
end

def select_images(data, id, image_type)
data['images'][id.to_s].select do |a|
a['type'] == image_type
end
end

def symbolize_keys(hash)
new_hash = {}
hash.each_key do |key|
new_hash[key.to_sym] = hash.delete(key)
end
new_hash
end
end
end
end

0 comments on commit 4525ff6

Please sign in to comment.