Sha256: ba5d0a3c870caf3d38879cdfe13a82f7b1e746edcad1ae8aae234aaee394e7eb

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

require "triviacrack/api/common"
require "triviacrack/parsers/user_parser"

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

      include TriviaCrack::API::Common

      # Public: Uses the Trivia Crack Search API to find the numeric user id of
      # the user identified by the given username.
      #
      # username  - Trivia Crack username of a user (e.g. "@example").
      #
      # Examples
      #
      #   get_user_id "@example"
      #
      # Returns the user id of the user.
      # Raises TriviaCrack:Errors::RequestError if the request fails.
      def get_user_id(username)
        # Trim the @ character from the start of the username
        if username.start_with? "@"
          username = username[1..-1]
        end

        response = get "/api/search?username=#{username}"

        body = response.body

        user_id = false
        body["list"].each do |user|
          if user["username"] == username
            user_id = user["id"]
            break
          end
        end

        user_id
      end

      # Public: Uses the Trivia Crack API to retrieve the user data.
      #
      # Returns a TriviaCrack::User representing the current user.
      # Raises TriviaCrack::Errors::RequestError if the request fails
      def get_user
        response = get "/api/users/#{@session.user_id}"

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

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

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