class HQTrivia::User

The User Class. A User. Your typical, everyday user.

Public Class Methods

new(id, key) click to toggle source

Initialize a new user. Get all their stats. Relax. @param id [Integer] the ID of the user. @param key [String] an API key, to get info. @raise [HQTrivia::Errors::InvalidKey] if the api key is invalid @raise [HQTrivia::Errors::InvalidUser] if the user id is invalid

# File lib/hqtrivia/user.rb, line 8
def initialize(id, key)
  @key = key
  @data = JSON.parse(RestClient.get("https://api-quiz.hype.space/users/#{id}", Authorization: key))
rescue RestClient::NotFound
  raise HQTrivia::Errors::InvalidUser, "This user doesn't exist!"
rescue RestClient::Unauthorized
  raise HQTrivia::Errors::InvalidKey, "This API Key is invalid!"
end

Public Instance Methods

authed_user?() click to toggle source

If this is true, you should consider HQTrivia#me. Otherwise, this is just a normal user. @return [true, false] whether the user is actually the owner of the key

# File lib/hqtrivia/user.rb, line 88
def authed_user?
  return true unless @data['hasPhone'].nil?

  false
end
avatar_url() click to toggle source

A user always has an avatar, whether HQ provided or not. @return [String] the user's Avatar URL.

# File lib/hqtrivia/user.rb, line 29
def avatar_url
  @data['avatarUrl']
end
badges() click to toggle source

@return [Badges] the badges for this user.

# File lib/hqtrivia/user.rb, line 66
def badges
  HQTrivia::Badges.new(id, @key)
end
badges_count() click to toggle source

@see [User#badges] @return [Integer] the amount of badges this person has

# File lib/hqtrivia/user.rb, line 61
def badges_count
  @data['achievementCount']
end
blocked?() click to toggle source

@return [true, false] whether this person has been blocked by the authed user

# File lib/hqtrivia/user.rb, line 76
def blocked?
  @data['blocked']
end
blocking?() click to toggle source

@return [true, false] whether this person is blocking the authed user

# File lib/hqtrivia/user.rb, line 81
def blocking?
  @data['blocksMe']
end
created() click to toggle source

@return [Time] the creation date of this user

# File lib/hqtrivia/user.rb, line 34
def created
  Time.parse(@data['created'])
end
games_played() click to toggle source

@return [Integer] the amount of games this user has played

# File lib/hqtrivia/user.rb, line 50
def games_played
  @data['gamesPlayed']
end
high_score() click to toggle source

@return [Integer] the user's high score.

# File lib/hqtrivia/user.rb, line 45
def high_score
  @data['highScore']
end
id() click to toggle source

@return [Integer] their id

# File lib/hqtrivia/user.rb, line 18
def id
  @data['userId']
end
leaderboard() click to toggle source

This user's leaderboard, returned as a UserLeaderboard object @return [User::Leaderboard] the user's leaderboard standings

# File lib/hqtrivia/user.rb, line 40
def leaderboard
  HQTrivia::User::Leaderboard.new(@data['leaderboard'])
end
season_xp() click to toggle source

@return [User::SeasonXP] the season XP stats of this user

# File lib/hqtrivia/user.rb, line 71
def season_xp
  HQTrivia::User::SeasonXP.new(@data['seasonXp'])
end
username() click to toggle source

@return [String] their username

# File lib/hqtrivia/user.rb, line 23
def username
  @data['username']
end
win_count() click to toggle source

@return [Integer] the amount of games this user has won

# File lib/hqtrivia/user.rb, line 55
def win_count
  @data['winCount']
end