From 4525ff687c0aa74a663df4f6439dd89a9cbc705f Mon Sep 17 00:00:00 2001 From: Fernando Briano Date: Tue, 17 Jan 2023 18:42:11 +0000 Subject: [PATCH] Refactors utility functions into own module --- lib/thegamesdb.rb | 57 +------------------------------- lib/thegamesdb/games.rb | 12 +++---- lib/thegamesdb/platforms.rb | 6 ++-- lib/thegamesdb/utils.rb | 66 +++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 65 deletions(-) create mode 100644 lib/thegamesdb/utils.rb diff --git a/lib/thegamesdb.rb b/lib/thegamesdb.rb index c54342f..2c1e87e 100644 --- a/lib/thegamesdb.rb +++ b/lib/thegamesdb.rb @@ -7,6 +7,7 @@ require 'thegamesdb/genres' require 'thegamesdb/platforms' require 'thegamesdb/publishers' +require 'thegamesdb/utils' require 'net/http' require 'json' @@ -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 @@ -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 diff --git a/lib/thegamesdb/games.rb b/lib/thegamesdb/games.rb index e33523d..8896816 100644 --- a/lib/thegamesdb/games.rb +++ b/lib/thegamesdb/games.rb @@ -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 @@ -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 diff --git a/lib/thegamesdb/platforms.rb b/lib/thegamesdb/platforms.rb index 7b18a43..73ed2ca 100644 --- a/lib/thegamesdb/platforms.rb +++ b/lib/thegamesdb/platforms.rb @@ -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 @@ -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 diff --git a/lib/thegamesdb/utils.rb b/lib/thegamesdb/utils.rb new file mode 100644 index 0000000..3ee9375 --- /dev/null +++ b/lib/thegamesdb/utils.rb @@ -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