Module: TriviaCrack::API::Question

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

Instance Method Summary collapse

Methods included from Common

#get, #post

Instance Method Details

#answer_question(game_id, question, answer) ⇒ Object

Public: Uses the Trivia Crack API to answer the given question.

game - The ID of the TriviaCrack::Game. question - The TriviaCrack::Question. answer - The index of the answer to be submitted.

Examples

answer_question game.id, question, 1

Returns a boolean indicating whether or not the question was answered correctly, and the updated TriviaCrack::Game object. Raises TriviaCrack::Errors::RequestError if the request fails



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/triviacrack/api/question.rb', line 25

def answer_question(game_id, question, answer)
  response =
    post "/api/users/#{@session.user_id}/games/#{game_id}/answers",
          parameters: { type: question.type.upcase,
                        answers: [{
                          id: question.id,
                          answer: answer,
                          category: question.category.upcase
                          }]
                      }.to_json

  game = TriviaCrack::Parsers::GameParser.parse response.body

  [game, answer == question.correct_answer]
end