Module: SteamCondenser::Community::WebApi

Includes:
Logging
Defined in:
lib/steam-condenser/community/web_api.rb

Overview

This module provides functionality for accessing Steam's Web API

The Web API requires you to register a domain with your Steam account to acquire an API key. See http://steamcommunity.com/dev for further details.

Author:

  • Sebastian Staudt

Constant Summary collapse

@@api_key =
nil
@@secure =
true

Class Method Summary collapse

Methods included from Logging

formatter=, included, level=, #log, logdev=

Class Method Details

.api_keyString

Returns the Steam Web API key currently used by Steam Condenser

Returns:

  • (String)

    The currently active Steam Web API key


30
31
32
# File 'lib/steam-condenser/community/web_api.rb', line 30

def self.api_key
  @@api_key
end

.api_key=(api_key) ⇒ Object

Sets the Steam Web API key

Parameters:

Raises:

  • (Error::WebApi)

    if the given API key is not a valid 128bit hexadecimal string


40
41
42
43
44
45
46
# File 'lib/steam-condenser/community/web_api.rb', line 40

def self.api_key=(api_key)
  unless api_key.nil? || api_key.match(/^[0-9A-F]{32}$/)
    raise SteamCondenser::Error::WebApi, :invalid_key
  end

  @@api_key = api_key
end

.get(format, interface, method, version = 1, params = {}) ⇒ String

Fetches data from Steam Web API using the specified interface, method and version. Additional parameters are supplied via HTTP GET. Data is returned as a string in the given format.

Parameters:

  • format (Symbol)

    The format to load from the API (:json, :vdf, or :xml)

  • interface (String)

    The Web API interface to call, e.g. ISteamUser

  • method (String)

    The Web API method to call, e.g. GetPlayerSummaries

  • version (Fixnum) (defaults to: 1)

    The API method version to use

  • params (Hash<Symbol, Object>) (defaults to: {})

    Additional parameters to supply via HTTP GET

Returns:

  • (String)

    The data as replied by the Web API in the desired format

Raises:


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/steam-condenser/community/web_api.rb', line 123

def self.get(format, interface, method, version = 1, params = {})
  version = version.to_s.rjust(4, '0')
  params = { :key => WebApi.api_key }.merge params unless WebApi.api_key.nil?
  params = { :format => format }.merge params
  protocol = @@secure ? 'https' : 'http'
  url = "#{protocol}://api.steampowered.com/#{interface}/#{method}/v#{version}/" +
        '?' + params.map { |k,v| "#{k}=#{v}" }.join('&')

  begin
    if log.debug?
      debug_url = @@api_key.nil? ? url : url.gsub(@@api_key, 'SECRET')
      log.debug "Querying Steam Web API: #{debug_url}"
    end
    open(url, { 'Content-Type' => 'application/x-www-form-urlencoded' ,:proxy => true }).read
  rescue OpenURI::HTTPError
    status = $!.io.status[0]
    status = [status, ''] unless status.is_a? Array
    raise SteamCondenser::Error::WebApi, :unauthorized if status[0].to_i == 401
    raise SteamCondenser::Error::WebApi.new :http_error, status[0].to_i, status[1]
  end
end

.interfacesArray<Hash>

Returns a raw list of interfaces and their methods that are available in Steam's Web API

This can be used for reference when accessing interfaces and methods that have not yet been implemented by Steam Condenser.

Returns:

  • (Array<Hash>)

    The list of interfaces and methods


62
63
64
# File 'lib/steam-condenser/community/web_api.rb', line 62

def self.interfaces
  json('ISteamWebAPIUtil', 'GetSupportedAPIList')[:apilist][:interfaces]
end

.json(interface, method, version = 1, params = {}) ⇒ Hash<Symbol, Object>

Fetches JSON data from Steam Web API using the specified interface, method and version. Additional parameters are supplied via HTTP GET. Data is returned as a Hash containing the JSON data.

Parameters:

  • interface (String)

    The Web API interface to call, e.g. ISteamUser

  • method (String)

    The Web API method to call, e.g. GetPlayerSummaries

  • version (Fixnum) (defaults to: 1)

    The API method version to use

  • params (Hash<Symbol, Object>) (defaults to: {})

    Additional parameters to supply via HTTP GET

Returns:

  • (Hash<Symbol, Object>)

    The JSON data replied to the request

Raises:


79
80
81
82
# File 'lib/steam-condenser/community/web_api.rb', line 79

def self.json(interface, method, version = 1, params = {})
  data = get :json, interface, method, version, params
  MultiJson.load(data, { :symbolize_keys => true })
end

.json!(interface, method, version = 1, params = {}) ⇒ Hash<Symbol, Object>

Fetches JSON data from Steam Web API using the specified interface, method and version. Additional parameters are supplied via HTTP GET. Data is returned as a Hash containing the JSON data.

Parameters:

  • interface (String)

    The Web API interface to call, e.g. ISteamUser

  • method (String)

    The Web API method to call, e.g. GetPlayerSummaries

  • version (Fixnum) (defaults to: 1)

    The API method version to use

  • params (Hash<Symbol, Object>) (defaults to: {})

    Additional parameters to supply via HTTP GET

Returns:

  • (Hash<Symbol, Object>)

    The JSON data replied to the request

Raises:


97
98
99
100
101
102
103
104
105
106
# File 'lib/steam-condenser/community/web_api.rb', line 97

def self.json!(interface, method, version = 1, params = {})
  result = json(interface, method, version, params)[:result]

  status = result[:status]
  if status != 1
    raise SteamCondenser::Error::WebApi.new :status_bad, status, result[:statusDetail]
  end

  result
end

.secure=(secure) ⇒ Object

Sets whether HTTPS should be used for the communication with the Web API

Parameters:

  • secure (Boolean)

    Whether to use HTTPS


51
52
53
# File 'lib/steam-condenser/community/web_api.rb', line 51

def self.secure=(secure)
  @@secure = !!secure
end