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
- @@secure =
true
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 = {}) ⇒ 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 = {}) ⇒ String
Fetches JSON data from Steam Web API using the specified interface, method and version.
-
.json!(interface, method, version = 1, params = {}) ⇒ Hash<Symbol, Object>
Fetches JSON data from Steam Web API using the specified interface, method and version.
-
.secure=(secure) ⇒ Object
Sets whether HTTPS should be used for the communication with the Web API.
Class Method Details
.api_key ⇒ String
Returns the Steam Web API key currently used by Steam Condenser
26 27 28 |
# File 'lib/steam/community/web_api.rb', line 26 def self.api_key @@api_key end |
.api_key=(api_key) ⇒ Object
Sets the Steam Web API key
36 37 38 39 40 41 42 |
# File 'lib/steam/community/web_api.rb', line 36 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 = {}) ⇒ 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.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/steam/community/web_api.rb', line 117 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 $DEBUG debug_url = @@api_key.nil? ? url : url.gsub(@@api_key, 'SECRET') puts "Querying Steam Web API: #{debug_url}" if $DEBUG 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 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.
58 59 60 61 |
# File 'lib/steam/community/web_api.rb', line 58 def self.interfaces data = json 'ISteamWebAPIUtil', 'GetSupportedAPIList' MultiJson.load(data, { :symbolize_keys => true })[:apilist][:interfaces] end |
.json(interface, method, version = 1, params = {}) ⇒ 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.
75 76 77 |
# File 'lib/steam/community/web_api.rb', line 75 def self.json(interface, method, version = 1, params = {}) get :json, interface, method, version, params 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.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/steam/community/web_api.rb', line 91 def self.json!(interface, method, version = 1, params = {}) 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 |
.secure=(secure) ⇒ Object
Sets whether HTTPS should be used for the communication with the Web API
47 48 49 |
# File 'lib/steam/community/web_api.rb', line 47 def self.secure=(secure) @@secure = !!secure end |