Class: BungieClient::Client
- Inherits:
-
Object
- Object
- BungieClient::Client
- Defined in:
- lib/bungie_client/client.rb
Overview
Class Client for GET/POST requests to Bungie. For specific HTTP operations you can use @conn [Faraday].
Constant Summary collapse
- BUNGIE_URI =
'https://www.bungie.net/Platform'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Class Method Summary collapse
-
.parse(response) ⇒ Mash
Format answer from Bungie.
Instance Method Summary collapse
-
#get(url, parameters = {}) ⇒ Mash
Get request to bungie service.
-
#initialize(options) ⇒ Client
constructor
Init client.
-
#post(url, query = {}) ⇒ Mash
Post data to Bungie services.
Constructor Details
#initialize(options) ⇒ Client
Init client
Initialize client for bungie api throw hash:
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bungie_client/client.rb', line 30 def initialize() # Checking options and @api_key raise 'Wrong options: It must be Hash.' unless .is_a? Hash if [:api_key].nil? raise "The API-key required for every request to bungie." else @api_key = [:api_key].to_s end # Set token @token = [:token].to_s unless [:token].nil? # Init connection @conn = Faraday.new :url => BUNGIE_URI do |builder| builder.headers['Content-Type'] = 'application/json' builder.headers['Accept'] = 'application/json' builder.headers['X-API-Key'] = @api_key builder.headers['Authorization'] = "Bearer #{@token}" unless @token.nil? builder..timeout = 5 builder..open_timeout = 2 builder.use FaradayMiddleware::FollowRedirects, :limit => 5 builder.adapter :httpclient end end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
17 18 19 |
# File 'lib/bungie_client/client.rb', line 17 def api_key @api_key end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
19 20 21 |
# File 'lib/bungie_client/client.rb', line 19 def conn @conn end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
18 19 20 |
# File 'lib/bungie_client/client.rb', line 18 def token @token end |
Class Method Details
.parse(response) ⇒ Mash
Format answer from Bungie
11 12 13 14 15 |
# File 'lib/bungie_client/client.rb', line 11 def self.parse(response) response = MultiJson.load response rescue {} Hashie::Mash.new response end |
Instance Method Details
#get(url, parameters = {}) ⇒ Mash
Get request to bungie service
67 68 69 |
# File 'lib/bungie_client/client.rb', line 67 def get(url, parameters = {}) self.class.parse @conn.get(url, parameters).body rescue Hashie::Mash.new end |
#post(url, query = {}) ⇒ Mash
Post data to Bungie services
77 78 79 |
# File 'lib/bungie_client/client.rb', line 77 def post(url, query = {}) self.class.parse @conn.post(url, query).body rescue Hashie::Mash.new end |