Sha256: ad89bbaf0f0c4b717c3697d8f384286c82f95a8669c2cde537b9f199734017bf

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

require "triviacrack/api/common"
require "triviacrack/parsers/game_parser"

# Public: All methods in this module make requests to the Trivia Crack answers
# API.
module TriviaCrack
  module API
    module Question

      include TriviaCrack::API::Common

      # 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
      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

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
triviacrack-0.3.2 lib/triviacrack/api/question.rb
triviacrack-0.3.0 lib/triviacrack/api/question.rb
triviacrack-0.2.0 lib/triviacrack/api/question.rb
triviacrack-0.1.0 lib/triviacrack/api/question.rb