Class: SteamId
- Inherits:
-
Object
- Object
- SteamId
- Includes:
- Cacheable
- 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.
-
#favorite_game ⇒ String
readonly
deprecated
Deprecated.
The favorite game is no longer listed for new users
-
#favorite_game_hours_played ⇒ String
readonly
deprecated
Deprecated.
The favorite game is no longer listed for new users
-
#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.
-
#visibility_state ⇒ Fixnum
readonly
Returns the visibility state of this Steam ID.
Attributes included from Cacheable
Class Method Summary collapse
-
.convert_community_id_to_steam_id(community_id) ⇒ String
Converts a 64bit numeric SteamID as used by the Steam Community to a SteamID as reported by game servers.
-
.convert_steam_id_to_community_id(steam_id) ⇒ Fixnum
Converts a SteamID as reported by game servers to a 64bit numeric SteamID as used by the Steam Community.
-
.from_steam_id(steam_id) ⇒ SteamId
Creates a new ‘SteamId` instance using a SteamID as used on servers.
Instance Method Summary collapse
-
#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.
-
#initialize(id, fetch = true) ⇒ SteamId
constructor
Creates a new ‘SteamId` instance for the given Steam ID.
-
#is_banned? ⇒ Boolean
Returns whether the owner of this SteamID is VAC banned.
-
#is_in_game? ⇒ Boolean
Returns whether the owner of this SteamId is playing a game.
-
#is_online? ⇒ Boolean
Returns whether the owner of this SteamID is currently logged into Steam.
-
#medium_avatar_url ⇒ String
Returns the URL of the medium-sized version of this user’s avatar.
-
#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 Cacheable
Constructor Details
#initialize(id, fetch = true) ⇒ SteamId
Creates a new ‘SteamId` instance for the given Steam ID
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/steam/community/steam_id.rb', line 194 def initialize(id, fetch = true) begin if id.is_a? Numeric @steam_id64 = id else @custom_url = id.downcase end super(fetch) rescue REXML::ParseException raise SteamCondenserError, 'SteamID could not be loaded.' 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.
31 32 33 |
# File 'lib/steam/community/steam_id.rb', line 31 def custom_url @custom_url end |
#favorite_game ⇒ String (readonly)
The favorite game is no longer listed for new users
Returns the favorite game of this user
37 38 39 |
# File 'lib/steam/community/steam_id.rb', line 37 def favorite_game @favorite_game end |
#favorite_game_hours_played ⇒ String (readonly)
The favorite game is no longer listed for new users
Returns the number of hours that this user played his/her favorite game in the last two weeks
45 46 47 |
# File 'lib/steam/community/steam_id.rb', line 45 def favorite_game_hours_played @favorite_game_hours_played end |
#groups ⇒ Array<SteamGroup> (readonly)
Returns the groups this user is a member of
50 51 52 |
# File 'lib/steam/community/steam_id.rb', line 50 def groups @groups end |
#head_line ⇒ String (readonly)
Returns the headline specified by the user
55 56 57 |
# File 'lib/steam/community/steam_id.rb', line 55 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
61 62 63 |
# File 'lib/steam/community/steam_id.rb', line 61 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.
69 70 71 |
# File 'lib/steam/community/steam_id.rb', line 69 def links @links end |
#location ⇒ String (readonly)
Returns the location of the user
74 75 76 |
# File 'lib/steam/community/steam_id.rb', line 74 def location @location end |
#member_since ⇒ Time (readonly)
Returns the date of registration for the Steam account belonging to this SteamID
80 81 82 |
# File 'lib/steam/community/steam_id.rb', line 80 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.
90 91 92 |
# File 'lib/steam/community/steam_id.rb', line 90 def most_played_games @most_played_games end |
#nickname ⇒ String (readonly)
Returns the Steam nickname of the user
95 96 97 |
# File 'lib/steam/community/steam_id.rb', line 95 def nickname @nickname end |
#privacy_state ⇒ String (readonly)
Returns the privacy state of this Steam ID
100 101 102 |
# File 'lib/steam/community/steam_id.rb', line 100 def privacy_state @privacy_state end |
#real_name ⇒ String (readonly)
Returns the real name of this user
105 106 107 |
# File 'lib/steam/community/steam_id.rb', line 105 def real_name @real_name end |
#state_message ⇒ String (readonly)
Returns the message corresponding to this user’s online state
112 113 114 |
# File 'lib/steam/community/steam_id.rb', line 112 def @state_message end |
#steam_id64 ⇒ Fixnum (readonly)
Returns this user’s 64bit SteamID
117 118 119 |
# File 'lib/steam/community/steam_id.rb', line 117 def steam_id64 @steam_id64 end |
#steam_rating ⇒ Float (readonly)
Returns the Steam rating calculated over the last two weeks’ activity
122 123 124 |
# File 'lib/steam/community/steam_id.rb', line 122 def @steam_rating end |
#summary ⇒ String (readonly)
Returns the summary this user has provided
127 128 129 |
# File 'lib/steam/community/steam_id.rb', line 127 def summary @summary end |
#visibility_state ⇒ Fixnum (readonly)
Returns the visibility state of this Steam ID
132 133 134 |
# File 'lib/steam/community/steam_id.rb', line 132 def visibility_state @visibility_state end |
Class Method Details
.convert_community_id_to_steam_id(community_id) ⇒ String
Converts a 64bit numeric SteamID as used by the Steam Community to a SteamID as reported by game servers
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/steam/community/steam_id.rb', line 141 def self.convert_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 |
.convert_steam_id_to_community_id(steam_id) ⇒ Fixnum
Converts a SteamID as reported by game servers to a 64bit numeric SteamID as used by the Steam Community
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/steam/community/steam_id.rb', line 162 def self.convert_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.match(/^STEAM_[0-1]:[0-1]:[0-9]+$/).nil? raise SteamCondenserError, "SteamID \"#{steam_id}\" doesn't have the correct format." end steam_id = steam_id[6..-1].split(':').map!{|s| s.to_i} steam_id[1] + steam_id[2] * 2 + 76561197960265728 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.
184 185 186 |
# File 'lib/steam/community/steam_id.rb', line 184 def self.from_steam_id(steam_id) new(convert_steam_id_to_community_id(steam_id)) end |
Instance Method Details
#base_url ⇒ String
Returns the base URL for this Steam ID
This URL is different for Steam IDs having a custom URL.
213 214 215 216 217 218 219 |
# File 'lib/steam/community/steam_id.rb', line 213 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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 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 |
# File 'lib/steam/community/steam_id.rb', line 227 def fetch profile_url = open(base_url + '?xml=1', {:proxy => true}) profile = REXML::Document.new(profile_url.read).root unless REXML::XPath.first(profile, 'error').nil? raise SteamCondenserError, profile.elements['error'].text end unless REXML::XPath.first(profile, 'privacyMessage').nil? raise SteamCondenserError, profile.elements['privacyMessage'].text end begin @nickname = CGI.unescapeHTML profile.elements['steamID'].text @steam_id64 = profile.elements['steamID64'].text.to_i @vac_banned = (profile.elements['vacBanned'].text == 1) @image_url = profile.elements['avatarIcon'].text[0..-5] @online_state = profile.elements['onlineState'].text @privacy_state = profile.elements['privacyState'].text @state_message = profile.elements['stateMessage'].text @visibility_state = profile.elements['visibilityState'].text.to_i if @privacy_state == 'public' @custom_url = profile.elements['customURL'].text.downcase @custom_url = nil if @custom_url.empty? unless REXML::XPath.first(profile, 'favoriteGame').nil? @favorite_game = profile.elements['favoriteGame/name'].text @favorite_game_hours_played = profile.elements['favoriteGame/hoursPlayed2wk'].text end @head_line = CGI.unescapeHTML profile.elements['headline'].text @hours_played = profile.elements['hoursPlayed2Wk'].text.to_f @location = profile.elements['location'].text @member_since = Time.parse(profile.elements['memberSince'].text) @real_name = CGI.unescapeHTML profile.elements['realname'].text @steam_rating = profile.elements['steamRating'].text.to_f @summary = CGI.unescapeHTML profile.elements['summary'].text @most_played_games = {} unless REXML::XPath.first(profile, 'mostPlayedGames').nil? profile.elements.each('mostPlayedGames/mostPlayedGame') do |most_played_game| @most_played_games[most_played_game.elements['gameName'].text] = most_played_game.elements['hoursPlayed'].text.to_f end end @groups = [] unless REXML::XPath.first(profile, 'groups').nil? profile.elements.each('groups/group') do |group| @groups << SteamGroup.new(group.elements['groupID64'].text.to_i, false) end end @links = {} unless REXML::XPath.first(profile, 'weblinks').nil? profile.elements.each('weblinks/weblink') do |link| @links[CGI.unescapeHTML link.elements['title'].text] = link.elements['link'].text end end end rescue raise SteamCondenserError, 'XML data could not be parsed.' end super 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.
302 303 304 305 306 307 308 309 310 |
# File 'lib/steam/community/steam_id.rb', line 302 def fetch_friends url = "#{base_url}/friends?xml=1" @friends = [] friends_data = REXML::Document.new(open(url, {:proxy => true}).read).root friends_data.elements.each('friends/friend') do |friend| @friends << SteamId.new(friend.text.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”.
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/steam/community/steam_id.rb', line 319 def fetch_games url = "#{base_url}/games?xml=1" @games = {} @playtimes = {} games_data = REXML::Document.new(open(url, {:proxy => true}).read).root games_data.elements.each('games/game') do |game_data| game = SteamGame.new(game_data) @games[game.app_id] = game recent = total = 0 unless game_data.elements['hoursLast2Weeks'].nil? recent = game_data.elements['hoursLast2Weeks'].text.to_f end unless game_data.elements['hoursOnRecord'].nil? total = game_data.elements['hoursOnRecord'].text.to_f end @playtimes[game.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.
373 374 375 376 |
# File 'lib/steam/community/steam_id.rb', line 373 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
344 345 346 |
# File 'lib/steam/community/steam_id.rb', line 344 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
357 358 359 360 361 362 363 364 365 |
# File 'lib/steam/community/steam_id.rb', line 357 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.
387 388 389 390 |
# File 'lib/steam/community/steam_id.rb', line 387 def games fetch_games if @games.nil? @games end |
#icon_url ⇒ String
Returns the URL of the icon version of this user’s avatar
395 396 397 |
# File 'lib/steam/community/steam_id.rb', line 395 def icon_url "#{@image_url}.jpg" end |
#is_banned? ⇒ Boolean
Returns whether the owner of this SteamID is VAC banned
402 403 404 |
# File 'lib/steam/community/steam_id.rb', line 402 def is_banned? @vac_banned end |
#is_in_game? ⇒ Boolean
Returns whether the owner of this SteamId is playing a game
409 410 411 |
# File 'lib/steam/community/steam_id.rb', line 409 def is_in_game? @online_state == 'in-game' end |
#is_online? ⇒ Boolean
Returns whether the owner of this SteamID is currently logged into Steam
416 417 418 |
# File 'lib/steam/community/steam_id.rb', line 416 def is_online? @online_state != 'offline' end |
#medium_avatar_url ⇒ String
Returns the URL of the medium-sized version of this user’s avatar
423 424 425 |
# File 'lib/steam/community/steam_id.rb', line 423 def medium_avatar_url "#{@image_url}_medium.jpg" end |
#recent_playtime(id) ⇒ Fixnum
Returns the time in minutes this user has played this game in the last two weeks
434 435 436 437 |
# File 'lib/steam/community/steam_id.rb', line 434 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
445 446 447 448 |
# File 'lib/steam/community/steam_id.rb', line 445 def total_playtime(id) game = find_game id @playtimes[game.app_id][1] end |