Module: TriviaCrack::API::User

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

Instance Method Summary collapse

Methods included from Common

#get, #post

Instance Method Details

#get_userObject

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



45
46
47
48
49
# File 'lib/triviacrack/api/user.rb', line 45

def get_user # rubocop:disable Naming/AccessorMethodName
  response = get "/api/users/#{@session.user_id}"

  TriviaCrack::Parsers::UserParser.parse response
end

#get_user_id(username) ⇒ Object

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.



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

def get_user_id(username)
  # Trim the @ character from the start of the username
  username = username[1..-1] if username.start_with? '@'

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

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

  user_id
end