Class: SteamID::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/steam-id/parser.rb

Overview

Create SteamID objects based on numeric or string-based Steam ID inputs.

See Also:

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

Constructor Details

#initialize(api_key: nil) ⇒ Parser

Initialize parser

Parameters:

  • api_key (String) (defaults to: nil)

    Key for Steam web API. ‘nil` to disable API functionality.



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.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    If the supplied string was not a valid 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.

Parameters:

  • id (String)

    Steam ID, either of:

    • Steam ID: STEAM_0:0:24110655

    • Steam ID 3: U:1:48221310

    • Steam ID 64: 76561198008487038

    • Account ID: 48221310

Returns:

Raises:

  • (ArgumentError)

    If the supplied string could not be converted to an account 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.

Parameters:

  • s (String)

    : Any form of known Steam ID, profile URL, or ‘vanity’/ custom URL.

Returns:

Raises:

  • (ArgumentError)

    If the supplied string was not a valid custom URL.

  • (WebApiError)

    If the Steam API returned an error.

See Also:



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)
   = nil

  # Todo: Refactor
  begin
    # Checking for Steam ID first. Most restrictive check, and also does
    # not require a call to Steam Web API.
     = from_steam_id(s)
  rescue ArgumentError
    begin
      # Community URL afterwards, does not require an API call either.
       = from_community_url(s)
    rescue ArgumentError
      begin
        # Trying to resolve as custom/vanity URL now.
         = from_vanity_url(s)
      rescue ArgumentError
      end
    end
  end

  if .nil?
    raise ArgumentError, "Could not convert #{ s } to account id."
  else
    
  end
end

#from_vanity_url(url) ⇒ SteamID

Create SteamID object based on custom URL. Note that this requires the API key to be set.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    If the supplied string was not a valid custom URL.

  • (WebApiError)

    If the Steam API returned an error.



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/steam-id/parser.rb', line 133

def from_vanity_url(url)
  raise ArgumentError, "Vanity URL must be ASCII." unless url.ascii_only?
  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