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
207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/steam/community/steam_id.rb', line 207 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 = self.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.
32 33 34 |
# File 'lib/steam/community/steam_id.rb', line 32 def custom_url @custom_url end |
#groups ⇒ Array<SteamGroup> (readonly)
Returns the groups this user is a member of
37 38 39 |
# File 'lib/steam/community/steam_id.rb', line 37 def groups @groups end |
#head_line ⇒ String (readonly)
Returns the headline specified by the user
42 43 44 |
# File 'lib/steam/community/steam_id.rb', line 42 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
48 49 50 |
# File 'lib/steam/community/steam_id.rb', line 48 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.
56 57 58 |
# File 'lib/steam/community/steam_id.rb', line 56 def links @links end |
#location ⇒ String (readonly)
Returns the location of the user
61 62 63 |
# File 'lib/steam/community/steam_id.rb', line 61 def location @location end |
#member_since ⇒ Time (readonly)
Returns the date of registration for the Steam account belonging to this SteamID
67 68 69 |
# File 'lib/steam/community/steam_id.rb', line 67 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.
77 78 79 |
# File 'lib/steam/community/steam_id.rb', line 77 def most_played_games @most_played_games end |
#nickname ⇒ String (readonly)
Returns the Steam nickname of the user
82 83 84 |
# File 'lib/steam/community/steam_id.rb', line 82 def nickname @nickname end |
#privacy_state ⇒ String (readonly)
Returns the privacy state of this Steam ID
87 88 89 |
# File 'lib/steam/community/steam_id.rb', line 87 def privacy_state @privacy_state end |
#real_name ⇒ String (readonly)
Returns the real name of this user
92 93 94 |
# File 'lib/steam/community/steam_id.rb', line 92 def real_name @real_name end |
#state_message ⇒ String (readonly)
Returns the message corresponding to this user’s online state
99 100 101 |
# File 'lib/steam/community/steam_id.rb', line 99 def @state_message end |
#steam_id64 ⇒ Fixnum (readonly)
Returns this user’s 64bit SteamID
104 105 106 |
# File 'lib/steam/community/steam_id.rb', line 104 def steam_id64 @steam_id64 end |
#steam_rating ⇒ Float (readonly)
Returns the Steam rating calculated over the last two weeks’ activity
109 110 111 |
# File 'lib/steam/community/steam_id.rb', line 109 def @steam_rating end |
#summary ⇒ String (readonly)
Returns the summary this user has provided
114 115 116 |
# File 'lib/steam/community/steam_id.rb', line 114 def summary @summary end |
#trade_ban_state ⇒ String (readonly)
Returns this user’s ban state in Steam’s trading system
119 120 121 |
# File 'lib/steam/community/steam_id.rb', line 119 def trade_ban_state @trade_ban_state end |
#visibility_state ⇒ Fixnum (readonly)
Returns the visibility state of this Steam ID
124 125 126 |
# File 'lib/steam/community/steam_id.rb', line 124 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
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/steam/community/steam_id.rb', line 133 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.
198 199 200 |
# File 'lib/steam/community/steam_id.rb', line 198 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
151 152 153 154 155 156 157 158 159 |
# File 'lib/steam/community/steam_id.rb', line 151 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
169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/steam/community/steam_id.rb', line 169 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 = steam_id[8..-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 = steam_id[3..-2].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
222 223 224 |
# File 'lib/steam/community/steam_id.rb', line 222 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.
232 233 234 235 236 237 238 |
# File 'lib/steam/community/steam_id.rb', line 232 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
246 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 299 300 301 302 303 304 305 |
# File 'lib/steam/community/steam_id.rb', line 246 def fetch begin 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 rescue '' @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 = {} unless profile['mostPlayedGames'].to_s.strip.empty? [profile['mostPlayedGames']['mostPlayedGame']].flatten.each do |most_played_game| @most_played_games[most_played_game['gameName']] = most_played_game['hoursPlayed'].to_f end end @groups = [] unless profile['groups'].to_s.strip.empty? [profile['groups']['group']].flatten.each do |group| @groups << SteamGroup.new(group['groupID64'].to_i, false) end end @links = {} unless profile['weblinks'].to_s.strip.empty? [profile['weblinks']['weblink']].flatten.each do |link| @links[CGI.unescapeHTML link['title']] = link['link'] end end end rescue raise $! if $!.is_a? SteamCondenserError raise SteamCondenserError, 'XML data could not be parsed.', $!.backtrace end 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.
314 315 316 317 318 319 320 |
# File 'lib/steam/community/steam_id.rb', line 314 def fetch_friends friends_data = parse "#{base_url}/friends?xml=1" @friends = [] friends_data['friends']['friend'].each do |friend| @friends << 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”.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/steam/community/steam_id.rb', line 329 def fetch_games games_data = parse "#{base_url}/games?xml=1" @games = {} @playtimes = {} games_data['games']['game'].each do |game_data| app_id = game_data['appID'].to_i game = SteamGame.new app_id, game_data @games[app_id] = game recent = game_data['hoursLast2Weeks'].to_f total = (game_data['hoursOnRecord'] || '').delete(',').to_f @playtimes[app_id] = [(recent * 60).to_i, (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.
379 380 381 382 |
# File 'lib/steam/community/steam_id.rb', line 379 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
350 351 352 |
# File 'lib/steam/community/steam_id.rb', line 350 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
363 364 365 366 367 368 369 370 371 |
# File 'lib/steam/community/steam_id.rb', line 363 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.
393 394 395 396 |
# File 'lib/steam/community/steam_id.rb', line 393 def games fetch_games if @games.nil? @games end |
#icon_url ⇒ String
Returns the URL of the icon version of this user’s avatar
401 402 403 |
# File 'lib/steam/community/steam_id.rb', line 401 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
410 411 412 |
# File 'lib/steam/community/steam_id.rb', line 410 def id @custom_url.nil? ? @steam_id64 : @custom_url end |
#in_game? ⇒ Boolean Also known as: is_in_game?
Returns whether the owner of this SteamId is playing a game
417 418 419 |
# File 'lib/steam/community/steam_id.rb', line 417 def in_game? @online_state == 'in-game' end |
#limited? ⇒ Boolean
Returns whether this Steam account is limited
425 426 427 |
# File 'lib/steam/community/steam_id.rb', line 425 def limited? @limited end |
#medium_avatar_url ⇒ String
Returns the URL of the medium-sized version of this user’s avatar
432 433 434 |
# File 'lib/steam/community/steam_id.rb', line 432 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
439 440 441 |
# File 'lib/steam/community/steam_id.rb', line 439 def online? @online_state != 'offline' end |
#public? ⇒ Boolean
Returns whether this Steam ID is publicly accessible
447 448 449 |
# File 'lib/steam/community/steam_id.rb', line 447 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
458 459 460 461 |
# File 'lib/steam/community/steam_id.rb', line 458 def recent_playtime(id) game = find_game id @playtimes[game.app_id][0] end |
#total_playtime(id) ⇒ Fixnum
Returns the total time in minutes this user has played this game
469 470 471 472 |
# File 'lib/steam/community/steam_id.rb', line 469 def total_playtime(id) game = find_game id @playtimes[game.app_id][1] end |