Module: WebApi
- Defined in:
- lib/steam/community/web_api.rb
Overview
This adds support for Steam Web API to classes needing this functionality. 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.
Constant Summary collapse
- @@api_key =
nil
Class Method Summary collapse
-
.api_key ⇒ Object
Returns the Steam Web API key.
-
.api_key=(api_key) ⇒ Object
Sets the Steam Web API key.
-
.json(interface, method, version = 1, params = nil) ⇒ Object
Fetches JSON data from Steam Web API using the specified interface, method and version.
-
.json!(interface, method, version = 1, params = nil) ⇒ Object
Fetches JSON data from Steam Web API using the specified interface, method and version.
-
.load(format, interface, method, version = 1, params = nil) ⇒ Object
Fetches data from Steam Web API using the specified interface, method and version.
Class Method Details
.api_key ⇒ Object
Returns the Steam Web API key
19 20 21 |
# File 'lib/steam/community/web_api.rb', line 19 def self.api_key @@api_key end |
.api_key=(api_key) ⇒ Object
Sets the Steam Web API key.
api_key
-
The 128bit API key that has to be requested from steamcommunity.com/dev
27 28 29 30 31 32 33 |
# File 'lib/steam/community/web_api.rb', line 27 def self.api_key=(api_key) unless api_key.nil? || api_key.match(/^[0-9A-F]{32}$/) raise WebApiException.new(:invalid_key) end @@api_key = api_key end |
.json(interface, method, version = 1, params = nil) ⇒ 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 JSON-encoded string.
interface
-
The Web API interface to call, e.g.
ISteamUser
method
-
The Web API method to call, e.g.
GetPlayerSummaries
version
-
The API method version to use (default: 1)
params
-
A Hash of additional parameters to supply via HTTP GET
43 44 45 |
# File 'lib/steam/community/web_api.rb', line 43 def self.json(interface, method, version = 1, params = nil) load(:json, interface, method, version, params) end |
.json!(interface, method, version = 1, params = nil) ⇒ 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.
interface
-
The Web API interface to call, e.g.
ISteamUser
method
-
The Web API method to call, e.g.
GetPlayerSummaries
version
-
The API method version to use (default: 1)
params
-
A Hash of additional parameters to supply via HTTP GET
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/steam/community/web_api.rb', line 55 def self.json!(interface, method, version = 1, params = nil) data = json(interface, method, version, params) result = JSON.parse(data, { :symbolize_names => true })[:result] status = result[:status] if status != 1 raise WebApiException.new(:status_bad, status, result[:statusDetail]) end result end |
.load(format, interface, method, version = 1, params = nil) ⇒ Object
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.
format
-
The format to load from the API (‘json’, ‘vdf’, or ‘xml’)
interface
-
The Web API interface to call, e.g.
ISteamUser
method
-
The Web API method to call, e.g.
GetPlayerSummaries
version
-
The API method version to use (default: 1)
params
-
A Hash of additional parameters to supply via HTTP GET
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/steam/community/web_api.rb', line 76 def self.load(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 open(url, { :proxy => true }).read rescue OpenURI::HTTPError status = $!.io.status[0] status = [status, ''] unless status.is_a? Array raise WebApiException.new(:unauthorized) if status[0].to_i == 401 raise WebApiException.new(:http_error, status[0].to_i, status[1]) end end |