Module: TriviaCrack::API::Game

Includes:
Common
Included in:
Client
Defined in:
lib/triviacrack/api/game.rb

Instance Method Summary collapse

Methods included from Common

#get, #post

Instance Method Details

#get_game(game_id) ⇒ Object

Public: Uses the Trivia Crack API to fetch the information for the given game.

game_id - The id of a Trivia Crack game.

Examples

get_game 123

Returns the TriviaCrack::Game for the given game_id. Raises TriviaCrack:Errors::RequestError if the request fails.



48
49
50
51
52
# File 'lib/triviacrack/api/game.rb', line 48

def get_game(game_id)
  response = get "/api/users/#{@session.user_id}/games/#{game_id}"

  TriviaCrack::Parsers::GameParser.parse response.body
end

#get_gamesObject

Public: Uses the Trivia Crack API to fetch the list of games for the current user.

Examples

get_games

Returns a list of TriviaCrack::Game. Raises TriviaCrack:Errors::RequestError if the request fails.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/triviacrack/api/game.rb', line 21

def get_games
  response = get "/api/users/#{@session.user_id}/dashboard"

  games_data = response.body["list"]

  games = []
  if games_data
    games_data.each do |game_data|
      game = TriviaCrack::Parsers::GameParser.parse game_data
      games << game
    end
  end

  games
end

#start_new_gameObject

Public: Uses the Trivia Crack API to start a new game for the current user.

Examples

client.start_new_game

Returns the TriviaCrack::Game that was started. Raises TriviaCrack::Errors::RequestError if the request fails



63
64
65
66
67
68
# File 'lib/triviacrack/api/game.rb', line 63

def start_new_game
  response = post "/api/users/#{@session.user_id}/games",
                  parameters: { language: "EN" }.to_json

  TriviaCrack::Parsers::GameParser.parse response.body
end