Class: SteamId
- Inherits:
-
Object
- Object
- SteamId
- Defined in:
- lib/steam/community/steam_id.rb
Overview
The SteamId class represents a Steam Community profile (also called Steam ID)
Instance Attribute Summary collapse
-
#custom_url ⇒ String
readonly
Returns the custom URL of this Steam ID.
-
#groups ⇒ Array<SteamGroup>
readonly
Returns the groups this user is a member of.
-
#head_line ⇒ String
readonly
Returns the headline specified by the user.
-
#hours_played ⇒ Float
readonly
Returns the number of hours that this user played a game in the last two weeks.
-
#links ⇒ Hash<String, String>
readonly
Returns the links that this user has added to his/her Steam ID.
-
#location ⇒ String
readonly
Returns the location of the user.
-
#member_since ⇒ Time
readonly
Returns the date of registration for the Steam account belonging to this SteamID.
-
#most_played_games ⇒ Hash<String, Float>
readonly
Returns the games this user has played the most in the last two weeks.
-
#nickname ⇒ String
readonly
Returns the Steam nickname of the user.
-
#privacy_state ⇒ String
readonly
Returns the privacy state of this Steam ID.
-
#real_name ⇒ String
readonly
Returns the real name of this user.
-
#state_message ⇒ String
readonly
Returns the message corresponding to this user’s online state.
-
#steam_id64 ⇒ Fixnum
readonly
Returns this user’s 64bit SteamID.
-
#steam_rating ⇒ Float
readonly
Returns the Steam rating calculated over the last two weeks’ activity.
-
#summary ⇒ String
readonly
Returns the summary this user has provided.
-
#trade_ban_state ⇒ String
readonly
Returns this user’s ban state in Steam’s trading system.
-
#visibility_state ⇒ Fixnum
readonly
Returns the visibility state of this Steam ID.
Attributes included from Cacheable
Class Method Summary collapse
-
.community_id_to_steam_id(community_id) ⇒ String
(also: convert_community_id_to_steam_id)
Converts a 64bit numeric SteamID as used by the Steam Community to a SteamID as reported by game servers.
-
.from_steam_id(steam_id) ⇒ SteamId
Creates a new ‘SteamId` instance using a SteamID as used on servers.
-
.resolve_vanity_url(vanity_url) ⇒ Fixnum
Resolves a vanity URL of a Steam Community profile to a 64bit numeric SteamID.
-
.steam_id_to_community_id(steam_id) ⇒ Fixnum
(also: convert_steam_id_to_community_id)
Converts a SteamID as reported by game servers to a 64bit numeric SteamID as used by the Steam Community.
Instance Method Summary collapse
-
#banned? ⇒ Boolean
(also: #is_banned?)
Returns whether the owner of this SteamID is VAC banned.
-
#base_url ⇒ String
Returns the base URL for this Steam ID.
-
#fetch ⇒ Object
Fetchs data from the Steam Community by querying the XML version of the profile specified by the ID of this Steam ID.
-
#fetch_friends ⇒ Object
Fetches the friends of this user.
-
#fetch_games ⇒ Object
Fetches the games this user owns.
-
#friends ⇒ Array<SteamId>
Returns the Steam Community friends of this user.
-
#full_avatar_url ⇒ String
Returns the URL of the full-sized version of this user’s avatar.
-
#game_stats(id) ⇒ GameStats
Returns the stats for the given game for the owner of this SteamID.
-
#games ⇒ Hash<Fixnum, SteamGame>
Returns the games this user owns.
-
#icon_url ⇒ String
Returns the URL of the icon version of this user’s avatar.
-
#id ⇒ Fixnum, String
Returns a unique identifier for this Steam ID.
-
#in_game? ⇒ Boolean
(also: #is_in_game?)
Returns whether the owner of this SteamId is playing a game.
-
#initialize(id) ⇒ SteamId
constructor
Creates a new ‘SteamId` instance for the given Steam ID.
-
#limited? ⇒ Boolean
Returns whether this Steam account is limited.
-
#medium_avatar_url ⇒ String
Returns the URL of the medium-sized version of this user’s avatar.
-
#online? ⇒ Boolean
(also: #is_online?)
Returns whether the owner of this SteamID is currently logged into Steam.
-
#public? ⇒ Boolean
Returns whether this Steam ID is publicly accessible.
-
#recent_playtime(id) ⇒ Fixnum
Returns the time in minutes this user has played this game in the last two weeks.
-
#total_playtime(id) ⇒ Fixnum
Returns the total time in minutes this user has played this game.
Methods included from XMLData
Methods included from Cacheable
Constructor Details
#initialize(id) ⇒ SteamId
Creates a new ‘SteamId` instance for the given Steam ID
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/steam/community/steam_id.rb', line 208 def initialize(id) if id.is_a? Numeric @steam_id64 = id else if id =~ /^STEAM_[0-1]:[0-1]:[0-9]+$/ || id =~ /\[U:[0-1]:[0-9]+\]/ @steam_id64 = SteamId.steam_id_to_community_id id else @custom_url = id.downcase end end end |
Instance Attribute Details
#custom_url ⇒ String (readonly)
The custom URL is not necessarily the same as the user’s nickname.
Returns the custom URL of this Steam ID
The custom URL is a user specified unique string that can be used instead of the 64bit SteamID as an identifier for a Steam ID.
33 34 35 |
# File 'lib/steam/community/steam_id.rb', line 33 def custom_url @custom_url end |
#groups ⇒ Array<SteamGroup> (readonly)
Returns the groups this user is a member of
38 39 40 |
# File 'lib/steam/community/steam_id.rb', line 38 def groups @groups end |
#head_line ⇒ String (readonly)
Returns the headline specified by the user
43 44 45 |
# File 'lib/steam/community/steam_id.rb', line 43 def head_line @head_line end |
#hours_played ⇒ Float (readonly)
Returns the number of hours that this user played a game in the last two weeks
49 50 51 |
# File 'lib/steam/community/steam_id.rb', line 49 def hours_played @hours_played end |
#links ⇒ Hash<String, String> (readonly)
Returns the links that this user has added to his/her Steam ID
The keys of the hash contain the titles of the links while the values contain the corresponding URLs.
57 58 59 |
# File 'lib/steam/community/steam_id.rb', line 57 def links @links end |
#location ⇒ String (readonly)
Returns the location of the user
62 63 64 |
# File 'lib/steam/community/steam_id.rb', line 62 def location @location end |
#member_since ⇒ Time (readonly)
Returns the date of registration for the Steam account belonging to this SteamID
68 69 70 |
# File 'lib/steam/community/steam_id.rb', line 68 def member_since @member_since end |
#most_played_games ⇒ Hash<String, Float> (readonly)
Returns the games this user has played the most in the last two weeks
The keys of the hash contain the names of the games while the values contain the number of hours the corresponding game has been played by the user in the last two weeks.
78 79 80 |
# File 'lib/steam/community/steam_id.rb', line 78 def most_played_games @most_played_games end |
#nickname ⇒ String (readonly)
Returns the Steam nickname of the user
83 84 85 |
# File 'lib/steam/community/steam_id.rb', line 83 def nickname @nickname end |
#privacy_state ⇒ String (readonly)
Returns the privacy state of this Steam ID
88 89 90 |
# File 'lib/steam/community/steam_id.rb', line 88 def privacy_state @privacy_state end |
#real_name ⇒ String (readonly)
Returns the real name of this user
93 94 95 |
# File 'lib/steam/community/steam_id.rb', line 93 def real_name @real_name end |
#state_message ⇒ String (readonly)
Returns the message corresponding to this user’s online state
100 101 102 |
# File 'lib/steam/community/steam_id.rb', line 100 def @state_message end |
#steam_id64 ⇒ Fixnum (readonly)
Returns this user’s 64bit SteamID
105 106 107 |
# File 'lib/steam/community/steam_id.rb', line 105 def steam_id64 @steam_id64 end |
#steam_rating ⇒ Float (readonly)
Returns the Steam rating calculated over the last two weeks’ activity
110 111 112 |
# File 'lib/steam/community/steam_id.rb', line 110 def @steam_rating end |
#summary ⇒ String (readonly)
Returns the summary this user has provided
115 116 117 |
# File 'lib/steam/community/steam_id.rb', line 115 def summary @summary end |
#trade_ban_state ⇒ String (readonly)
Returns this user’s ban state in Steam’s trading system
120 121 122 |
# File 'lib/steam/community/steam_id.rb', line 120 def trade_ban_state @trade_ban_state end |
#visibility_state ⇒ Fixnum (readonly)
Returns the visibility state of this Steam ID
125 126 127 |
# File 'lib/steam/community/steam_id.rb', line 125 def visibility_state @visibility_state end |
Class Method Details
.community_id_to_steam_id(community_id) ⇒ String Also known as: convert_community_id_to_steam_id
Converts a 64bit numeric SteamID as used by the Steam Community to a SteamID as reported by game servers
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/steam/community/steam_id.rb', line 134 def self.community_id_to_steam_id(community_id) steam_id1 = community_id % 2 steam_id2 = community_id - 76561197960265728 unless steam_id2 > 0 raise SteamCondenserError, "SteamID #{community_id} is too small." end steam_id2 = (steam_id2 - steam_id1) / 2 "STEAM_0:#{steam_id1}:#{steam_id2}" end |
.from_steam_id(steam_id) ⇒ SteamId
Creates a new ‘SteamId` instance using a SteamID as used on servers
The SteamID from the server is converted into a 64bit numeric SteamID first before this is used to retrieve the corresponding Steam Community profile.
199 200 201 |
# File 'lib/steam/community/steam_id.rb', line 199 def self.from_steam_id(steam_id) new(convert_steam_id_to_community_id(steam_id)) end |
.resolve_vanity_url(vanity_url) ⇒ Fixnum
Resolves a vanity URL of a Steam Community profile to a 64bit numeric SteamID
152 153 154 155 156 157 158 159 160 |
# File 'lib/steam/community/steam_id.rb', line 152 def self.resolve_vanity_url(vanity_url) params = { :vanityurl => vanity_url } json = WebApi.json 'ISteamUser', 'ResolveVanityURL', 1, params result = MultiJson.load(json, :symbolize_keys => true)[:response] return nil if result[:success] != 1 result[:steamid].to_i end |
.steam_id_to_community_id(steam_id) ⇒ Fixnum Also known as: convert_steam_id_to_community_id
Converts a SteamID as reported by game servers to a 64bit numeric SteamID as used by the Steam Community
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/steam/community/steam_id.rb', line 170 def self.steam_id_to_community_id(steam_id) if steam_id == 'STEAM_ID_LAN' || steam_id == 'BOT' raise SteamCondenserError, "Cannot convert SteamID \"#{steam_id}\" to a community ID." elsif steam_id =~ /^STEAM_[0-1]:([0-1]:[0-9]+)$/ steam_id = $1.split(':').map! { |s| s.to_i } steam_id[0] + steam_id[1] * 2 + 76561197960265728 elsif steam_id =~ /^\[U:([0-1]:[0-9]+)\]$/ steam_id = $1.split(':').map { |s| s.to_i } steam_id[0] + steam_id[1] + 76561197960265727 else raise SteamCondenserError, "SteamID \"#{steam_id}\" doesn't have the correct format." end end |
Instance Method Details
#banned? ⇒ Boolean Also known as: is_banned?
Returns whether the owner of this SteamID is VAC banned
223 224 225 |
# File 'lib/steam/community/steam_id.rb', line 223 def banned? @vac_banned end |
#base_url ⇒ String
Returns the base URL for this Steam ID
This URL is different for Steam IDs having a custom URL.
233 234 235 236 237 238 239 |
# File 'lib/steam/community/steam_id.rb', line 233 def base_url if @custom_url.nil? "http://steamcommunity.com/profiles/#@steam_id64" else "http://steamcommunity.com/id/#@custom_url" end end |
#fetch ⇒ Object
Fetchs data from the Steam Community by querying the XML version of the profile specified by the ID of this Steam ID
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/steam/community/steam_id.rb', line 247 def fetch profile = parse "#{base_url}?xml=1" raise SteamCondenserError, profile['error'] unless profile['error'].nil? unless profile['privacyMessage'].nil? raise SteamCondenserError, profile['privacyMessage'] end @nickname = CGI.unescapeHTML profile['steamID'] @steam_id64 = profile['steamID64'].to_i @limited = (profile['isLimitedAccount'].to_i == 1) @trade_ban_state = profile['tradeBanState'] @vac_banned = (profile['vacBanned'].to_i == 1) @image_url = profile['avatarIcon'][0..-5] @online_state = profile['onlineState'] @privacy_state = profile['privacyState'] @state_message = profile['stateMessage'] @visibility_state = profile['visibilityState'].to_i if public? @custom_url = (profile['customURL'] || '').downcase @custom_url = nil if @custom_url.empty? @head_line = CGI.unescapeHTML profile['headline'] || '' @hours_played = profile['hoursPlayed2Wk'].to_f @location = profile['location'] @member_since = Time.parse profile['memberSince'] @real_name = CGI.unescapeHTML profile['realname'] || '' @steam_rating = profile['steamRating'].to_f @summary = CGI.unescapeHTML profile['summary'] || '' @most_played_games = {} [(profile['mostPlayedGames'] || {})['mostPlayedGame']].compact.flatten.each do |most_played_game| @most_played_games[most_played_game['gameName']] = most_played_game['hoursPlayed'].to_f end @groups = [] [(profile['groups'] || {})['group']].compact.flatten.each do |group| @groups << SteamGroup.new(group['groupID64'].to_i, false) end @links = {} [(profile['weblinks'] || {})['weblink']].compact.flatten.each do |link| @links[CGI.unescapeHTML link['title']] = link['link'] end end rescue raise $! if $!.is_a? SteamCondenserError raise SteamCondenserError, 'XML data could not be parsed.' end |
#fetch_friends ⇒ Object
Fetches the friends of this user
This creates a new ‘SteamId` instance for each of the friends without fetching their data.
307 308 309 310 311 312 |
# File 'lib/steam/community/steam_id.rb', line 307 def fetch_friends friends_data = parse "#{base_url}/friends?xml=1" @friends = friends_data['friends']['friend'].map do |friend| SteamId.new friend.to_i, false end end |
#fetch_games ⇒ Object
Fetches the games this user owns
This fills the game hash with the names of the games as keys. The values will either be ‘false` if the game does not have stats or the game’s “friendly name”.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/steam/community/steam_id.rb', line 321 def fetch_games games_data = parse "#{base_url}/games?xml=1" @games = {} @recent_playtimes = {} @total_playtimes = {} games_data['games']['game'].each do |game_data| app_id = game_data['appID'].to_i @games[app_id] = SteamGame.new app_id, game_data recent = game_data['hoursLast2Weeks'].to_f total = (game_data['hoursOnRecord'] || '').delete(',').to_f @recent_playtimes[app_id] = (recent * 60).to_i @total_playtimes[app_id] = (total * 60).to_i end true end |
#friends ⇒ Array<SteamId>
Returns the Steam Community friends of this user
If the friends haven’t been fetched yet, this is done now.
372 373 374 375 |
# File 'lib/steam/community/steam_id.rb', line 372 def friends fetch_friends if @friends.nil? @friends end |
#full_avatar_url ⇒ String
Returns the URL of the full-sized version of this user’s avatar
343 344 345 |
# File 'lib/steam/community/steam_id.rb', line 343 def full_avatar_url "#{@image_url}_full.jpg" end |
#game_stats(id) ⇒ GameStats
Returns the stats for the given game for the owner of this SteamID
356 357 358 359 360 361 362 363 364 |
# File 'lib/steam/community/steam_id.rb', line 356 def game_stats(id) game = find_game id unless game.has_stats? raise SteamCondenserError, "\"#{game.name}\" does not have stats." end GameStats.create_game_stats(@custom_url || @steam_id64, game.short_name) end |
#games ⇒ Hash<Fixnum, SteamGame>
Returns the games this user owns
The keys of the hash are the games’ application IDs and the values are the corresponding game instances.
If the friends haven’t been fetched yet, this is done now.
386 387 388 389 |
# File 'lib/steam/community/steam_id.rb', line 386 def games fetch_games if @games.nil? @games end |
#icon_url ⇒ String
Returns the URL of the icon version of this user’s avatar
394 395 396 |
# File 'lib/steam/community/steam_id.rb', line 394 def icon_url "#@image_url.jpg" end |
#id ⇒ Fixnum, String
Returns a unique identifier for this Steam ID
This is either the 64bit numeric SteamID or custom URL
403 404 405 |
# File 'lib/steam/community/steam_id.rb', line 403 def id @custom_url || @steam_id64 end |
#in_game? ⇒ Boolean Also known as: is_in_game?
Returns whether the owner of this SteamId is playing a game
410 411 412 |
# File 'lib/steam/community/steam_id.rb', line 410 def in_game? @online_state == 'in-game' end |
#limited? ⇒ Boolean
Returns whether this Steam account is limited
418 419 420 |
# File 'lib/steam/community/steam_id.rb', line 418 def limited? @limited end |
#medium_avatar_url ⇒ String
Returns the URL of the medium-sized version of this user’s avatar
425 426 427 |
# File 'lib/steam/community/steam_id.rb', line 425 def medium_avatar_url "#{@image_url}_medium.jpg" end |
#online? ⇒ Boolean Also known as: is_online?
Returns whether the owner of this SteamID is currently logged into Steam
432 433 434 |
# File 'lib/steam/community/steam_id.rb', line 432 def online? @online_state != 'offline' end |
#public? ⇒ Boolean
Returns whether this Steam ID is publicly accessible
440 441 442 |
# File 'lib/steam/community/steam_id.rb', line 440 def public? @privacy_state == 'public' end |
#recent_playtime(id) ⇒ Fixnum
Returns the time in minutes this user has played this game in the last two weeks
451 452 453 |
# File 'lib/steam/community/steam_id.rb', line 451 def recent_playtime(id) @recent_playtimes[find_game(id).app_id] end |
#total_playtime(id) ⇒ Fixnum
Returns the total time in minutes this user has played this game
461 462 463 |
# File 'lib/steam/community/steam_id.rb', line 461 def total_playtime(id) @total_playtimes[find_game(id).app_id] end |