Module: WebApi

Defined in:
lib/steam/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 steamcommunity.com/dev for further details.

Author:

  • Sebastian

Constant Summary collapse

@@api_key =
nil

Class Method Summary collapse

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



24
25
26
# File 'lib/steam/community/web_api.rb', line 24

def self.api_key
  @@api_key
end

.api_key=(api_key) ⇒ Object

Sets the Steam Web API key

Parameters:

  • api_key (String)

    The 128bit API key as a hexadecimal string that has to be requested from steamcommunity.com/dev

Raises:

  • (WebApiError)

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



34
35
36
37
38
39
40
# File 'lib/steam/community/web_api.rb', line 34

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

  @@api_key = api_key
end

.get(format, interface, method, version = 1, params = nil) ⇒ 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: nil)

    Additional parameters to supply via HTTP GET

Returns:

  • (String)

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

Raises:

  • (WebApiError)

    if the request to Steam’s Web API fails



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/steam/community/web_api.rb', line 108

def self.get(format, interface, method, version = 1, params = nil)
  version = version.to_s.rjust(4, '0')
  url = "http://api.steampowered.com/#{interface}/#{method}/v#{version}/"
  params = {} unless params.is_a?(Hash)
  params[:format] = format
  params[:key] = WebApi.api_key

  unless params.nil? && params.empty?
    url += '?' + params.map { |k,v| "#{k}=#{v}" }.join('&')
  end

  begin
    puts "Querying Steam Web API: #{url}" if $DEBUG
    open(url, { :proxy => true }).read
  rescue OpenURI::HTTPError
    status = $!.io.status[0]
    status = [status, ''] unless status.is_a? Array
    raise WebApiError, :unauthorized if status[0].to_i == 401
    raise WebApiError.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



49
50
51
52
# File 'lib/steam/community/web_api.rb', line 49

def self.interfaces
  data = json 'ISteamWebAPIUtil', 'GetSupportedAPIList'
  MultiJson.load(data, { :symbolize_keys => true })[:apilist][:interfaces]
end

.json(interface, method, version = 1, params = nil) ⇒ String

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 JSON-encoded string.

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

    Additional parameters to supply via HTTP GET

Returns:

  • (String)

    The raw JSON data replied to the request

Raises:

  • (WebApiError)

    if the request to Steam’s Web API fails



66
67
68
# File 'lib/steam/community/web_api.rb', line 66

def self.json(interface, method, version = 1, params = nil)
  get(:json, interface, method, version, params)
end

.json!(interface, method, version = 1, params = nil) ⇒ 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: nil)

    Additional parameters to supply via HTTP GET

Returns:

  • (Hash<Symbol, Object>)

    The JSON data replied to the request

Raises:

  • (WebApiError)

    if the request to Steam’s Web API fails



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/steam/community/web_api.rb', line 82

def self.json!(interface, method, version = 1, params = nil)
  data = json(interface, method, version, params)
  result = MultiJson.load(data, { :symbolize_keys => true })[:result]

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

  result
end