Class: RAWG::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rawg/client.rb

Constant Summary collapse

DEFAULT_USER_AGENT =
"rawg.rb/#{RAWG::VERSION}"
BASE_URL =
'https://api.rawg.io'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent: nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
# 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

Instance Attribute Details

#user_agentObject (readonly)

Returns the value of attribute user_agent.



11
12
13
# File 'lib/rawg/client.rb', line 11

def user_agent
  @user_agent
end

Instance Method Details

#all_games(options = {}) ⇒ Object



26
27
28
# File 'lib/rawg/client.rb', line 26

def all_games(options = {})
  get('/api/games', options)
end

#all_users(options = {}) ⇒ Object



30
31
32
# File 'lib/rawg/client.rb', line 30

def all_users(options = {})
  get('/api/users', options)
end

#game(game) ⇒ Object



66
67
68
69
# 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) ⇒ Object



42
43
44
# File 'lib/rawg/client.rb', line 42

def game_info(game)
  get("/api/games/#{game}")
end

#game_reviews(game, options = {}) ⇒ Object



50
51
52
# File 'lib/rawg/client.rb', line 50

def game_reviews(game, options = {})
  get("/api/games/#{game}/reviews", options)
end

#game_suggest(game, options = {}) ⇒ Object



46
47
48
# File 'lib/rawg/client.rb', line 46

def game_suggest(game, options = {})
  get("/api/games/#{game}/suggested", options)
end

#games(options = {}) ⇒ Object



71
72
73
74
# 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 = {}) ⇒ Object



18
19
20
21
22
23
24
# 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 = {}) ⇒ Object



34
35
36
# File 'lib/rawg/client.rb', line 34

def search_games(query, options = {})
  all_games(search: query, **options)
end

#search_users(query, options = {}) ⇒ Object



38
39
40
# File 'lib/rawg/client.rb', line 38

def search_users(query, options = {})
  all_users(search: query, **options)
end

#user_games(user, options = {}) ⇒ Object



58
59
60
# File 'lib/rawg/client.rb', line 58

def user_games(user, options = {})
  get("/api/users/#{user}/games", options)
end

#user_info(user) ⇒ Object



54
55
56
# File 'lib/rawg/client.rb', line 54

def (user)
  get("/api/users/#{user}")
end

#user_reviews(user, options = {}) ⇒ Object



62
63
64
# File 'lib/rawg/client.rb', line 62

def user_reviews(user, options = {})
  get("/api/users/#{user}/reviews", options)
end