Class: SteamID::Parser
- Inherits:
-
Object
- Object
- SteamID::Parser
- Defined in:
- lib/steam-id/parser.rb
Overview
Create SteamID objects based on numeric or string-based Steam ID inputs.
Constant Summary collapse
- PATTERN_STEAM_ID =
Pattern to match classical Steam IDs (STEAM_0:…)
/^STEAM_[0-9]:([0-9]):([0-9]+)$/i
- PATTERN_STEAM_ID_3 =
Pattern to match Steam ID 3 format ([U:1:…])
/^\[?U:([0-9]{1,2}):([0-9]+)\]?$/i
- PATTERN_STEAM_ID_64 =
Pattern to match Steam ID 64 aka profile ID format (765…): Must have at least 14 digits, else ID_64 - OFFSET would be < 0.
/^(765[0-9]{11,})$/
- PATTERN_ACCOUNT_ID =
Pattern to match plain account IDs. Not sure what its valid range is - exists with at least 7 and 8 numbers.
/^[0-9]+$/
- PATTERN_COMMUNITY_URL =
Pattern to match ID64-based profile URLs.
/^https?:\/\/steamcommunity\.com\/profiles\/(765[0-9]{11,})\/?$/
- PATTERN_COMMUNITY_URL_STEAM_ID_3 =
Pattern to match ID3-based profile URLs.
/^https?:\/\/steamcommunity\.com\/profiles\/\[U:([0-9]{1,2}):([0-9]+)\]$/i
- PATTERN_CUSTOM_URL =
Pattern to match custom-URL-based profile URLs.
/^https?:\/\/steamcommunity\.com\/id\/([^\/]+)\/?$/
Instance Method Summary collapse
-
#from_community_url(url) ⇒ SteamID
Create SteamID object based on community URL.
-
#from_steam_id(id) ⇒ SteamID
Create SteamID object based on Steam ID.
-
#from_string(s) ⇒ SteamID
Create SteamID object based on any kind of recognized Steam ID.
-
#from_vanity_url(url) ⇒ SteamID
Create SteamID object based on custom URL.
-
#initialize(api_key: nil) ⇒ Parser
constructor
Initialize parser.
Constructor Details
#initialize(api_key: nil) ⇒ Parser
Initialize parser
30 31 32 33 34 35 |
# File 'lib/steam-id/parser.rb', line 30 def initialize(api_key: nil) @api_key = api_key if api_key ::WebApi.api_key = @api_key.to_s end end |
Instance Method Details
#from_community_url(url) ⇒ SteamID
Create SteamID object based on community URL.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/steam-id/parser.rb', line 113 def from_community_url(url) PATTERN_COMMUNITY_URL.match(url) do |m| return self.from_steam_id(m[1]) end PATTERN_COMMUNITY_URL_STEAM_ID_3.match(url) do |m| return self.from_steam_id(m[2]) end raise ArgumentError, "#{ url.inspect } is not a supported community URL." end |
#from_steam_id(id) ⇒ SteamID
Create SteamID object based on Steam ID.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/steam-id/parser.rb', line 83 def from_steam_id(id) # In case we get a fixnum. id = id.to_s # https://developer.valvesoftware.com/wiki/SteamID#Format PATTERN_STEAM_ID.match(id) do |m| return SteamID.new(m[1].to_i + m[2].to_i * 2) end PATTERN_STEAM_ID_3.match(id) do |m| return SteamID.new(m[2].to_i) end PATTERN_STEAM_ID_64.match(id) do |m| return SteamID.new(m[1].to_i - SteamID::ID_64_OFFSET) end # Matching this one last, as not to catch an ID 64 on accident. PATTERN_ACCOUNT_ID.match(id) do |m| return SteamID.new(id.to_i) end # If we get until here, we did not match any regex. raise ArgumentError, "#{ id.inspect } is not a supported SteamID." end |
#from_string(s) ⇒ SteamID
Create SteamID object based on any kind of recognized Steam ID.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/steam-id/parser.rb', line 46 def from_string(s) account_id = nil # Todo: Refactor begin # Checking for Steam ID first. Most restrictive check, and also does # not require a call to Steam Web API. account_id = from_steam_id(s) rescue ArgumentError begin # Community URL afterwards, does not require an API call either. account_id = from_community_url(s) rescue ArgumentError begin # Trying to resolve as custom/vanity URL now. account_id = from_vanity_url(s, steam_api_key: steam_api_key) rescue ArgumentError end end end if account_id.nil? raise ArgumentError, "Could not convert #{ s } to account id." else account_id end end |
#from_vanity_url(url) ⇒ SteamID
Create SteamID object based on custom URL. Note that this requires the API key to be set.
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/steam-id/parser.rb', line 133 def from_vanity_url(url) PATTERN_CUSTOM_URL.match(url) do |m| url = m[1] end steam_id = SteamId.resolve_vanity_url(url) if steam_id.nil? raise ArgumentError, "#{ url } was not a valid custom URL." else from_steam_id(steam_id) end end |