Sha256: cf31e2381ef68d5a4ee5a15f52224bb766c0e67d9c50f9d8efab62355531324b

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

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

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

      include TriviaCrack::API::Common

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

      # 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.
      def get_game(game_id)
        response = get "/api/users/#{@session.user_id}/games/#{game_id}"

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

      # 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
      def start_new_game
        response = post "/api/users/#{@session.user_id}/games",
                        parameters: { language: "EN" }.to_json

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

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
triviacrack-0.5.0 lib/triviacrack/api/game.rb
triviacrack-0.4.0 lib/triviacrack/api/game.rb
triviacrack-0.3.2 lib/triviacrack/api/game.rb
triviacrack-0.3.0 lib/triviacrack/api/game.rb
triviacrack-0.2.0 lib/triviacrack/api/game.rb
triviacrack-0.1.0 lib/triviacrack/api/game.rb