class RAWG::Client
Constants
- BASE_URL
- DEFAULT_USER_AGENT
Attributes
user_agent[R]
Public Class Methods
new(user_agent: nil)
click to toggle source
# File lib/rawg/client.rb, line 13 def initialize(user_agent: nil) @user_agent = combine_user_agents(user_agent, DEFAULT_USER_AGENT) @http_client = default_http_client end
Public Instance Methods
all_games(options = {})
click to toggle source
# File lib/rawg/client.rb, line 26 def all_games(options = {}) get('/api/games', options) end
all_users(options = {})
click to toggle source
# File lib/rawg/client.rb, line 30 def all_users(options = {}) get('/api/users', options) end
game(game)
click to toggle source
# File lib/rawg/client.rb, line 66 def game(game) response = game_info(game) RAWG::Game.new(client: self).from_api_response(response) end
game_info(game)
click to toggle source
# File lib/rawg/client.rb, line 42 def game_info(game) get("/api/games/#{game}") end
game_reviews(game, options = {})
click to toggle source
# File lib/rawg/client.rb, line 50 def game_reviews(game, options = {}) get("/api/games/#{game}/reviews", options) end
game_suggest(game, options = {})
click to toggle source
# File lib/rawg/client.rb, line 46 def game_suggest(game, options = {}) get("/api/games/#{game}/suggested", options) end
games(options = {})
click to toggle source
# File lib/rawg/client.rb, line 71 def games(options = {}) response = all_games(options) RAWG::Collection.new(RAWG::Game, client: self).from_api_response(response) end
get(path, query = {})
click to toggle source
# File lib/rawg/client.rb, line 18 def get(path, query = {}) response = @http_client.get(path, format_query(query)).body return nil unless response.is_a?(Hash) return nil if response[:detail] == 'Not found.' response end
search_games(query, options = {})
click to toggle source
# File lib/rawg/client.rb, line 34 def search_games(query, options = {}) all_games(search: query, **options) end
search_users(query, options = {})
click to toggle source
# File lib/rawg/client.rb, line 38 def search_users(query, options = {}) all_users(search: query, **options) end
user_games(user, options = {})
click to toggle source
# File lib/rawg/client.rb, line 58 def user_games(user, options = {}) get("/api/users/#{user}/games", options) end
user_info(user)
click to toggle source
# File lib/rawg/client.rb, line 54 def user_info(user) get("/api/users/#{user}") end
user_reviews(user, options = {})
click to toggle source
# File lib/rawg/client.rb, line 62 def user_reviews(user, options = {}) get("/api/users/#{user}/reviews", options) end
Private Instance Methods
combine_user_agents(*user_agents)
click to toggle source
# File lib/rawg/client.rb, line 78 def combine_user_agents(*user_agents) user_agents.join(' ').squeeze(' ').strip end
default_http_client()
click to toggle source
# File lib/rawg/client.rb, line 82 def default_http_client Faraday.new( url: BASE_URL, headers: { user_agent: @user_agent, content_type: 'application/json' } ) do |conn| conn.response :json, content_type: /\bjson$/, parser_options: { symbolize_names: true } conn.adapter Faraday.default_adapter end end
format_query(query)
click to toggle source
# File lib/rawg/client.rb, line 93 def format_query(query) query .map { |field, value| [field, serialize_query_value(value)] } .to_h .compact end
serialize_query_value(value)
click to toggle source
# File lib/rawg/client.rb, line 100 def serialize_query_value(value) return value unless value.is_a? Array value.join(',') end