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.
Constant Summary collapse
- @@api_key =
nil
Class Method Summary collapse
-
.api_key ⇒ String
Returns the Steam Web API key currently used by Steam Condenser.
-
.api_key=(api_key) ⇒ Object
Sets the Steam Web API key.
-
.get(format, interface, method, version = 1, params = nil) ⇒ String
Fetches data from Steam Web API using the specified interface, method and version.
-
.interfaces ⇒ Array<Hash>
Returns a raw list of interfaces and their methods that are available in Steam’s Web API.
-
.json(interface, method, version = 1, params = nil) ⇒ String
Fetches JSON data from Steam Web API using the specified interface, method and version.
-
.json!(interface, method, version = 1, params = nil) ⇒ Hash<Symbol, Object>
Fetches JSON data from Steam Web API using the specified interface, method and version.
Class Method Details
.api_key ⇒ String
Returns the Steam Web API key currently used by Steam Condenser
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
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.
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 |
.interfaces ⇒ Array<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.
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.
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.
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 |