class HQTrivia::User
Public Class Methods
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
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
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
@return [Badges] the badges for this user.
# File lib/hqtrivia/user.rb, line 66 def badges HQTrivia::Badges.new(id, @key) end
@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
@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
@return [true, false] whether this person is blocking the authed user
# File lib/hqtrivia/user.rb, line 81 def blocking? @data['blocksMe'] end
@return [Time] the creation date of this user
# File lib/hqtrivia/user.rb, line 34 def created Time.parse(@data['created']) end
@return [Integer] the amount of games this user has played
# File lib/hqtrivia/user.rb, line 50 def games_played @data['gamesPlayed'] end
@return [Integer] the user's high score.
# File lib/hqtrivia/user.rb, line 45 def high_score @data['highScore'] end
@return [Integer] their id
# File lib/hqtrivia/user.rb, line 18 def id @data['userId'] end
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
@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
@return [String] their username
# File lib/hqtrivia/user.rb, line 23 def username @data['username'] end
@return [Integer] the amount of games this user has won
# File lib/hqtrivia/user.rb, line 55 def win_count @data['winCount'] end