Class: BungieClient::Client
- Inherits:
-
Object
- Object
- BungieClient::Client
- Defined in:
- lib/bungie_client/client.rb
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 = {}, headers = {}) ⇒ Mash
Get request to bungie service.
-
#initialize(options) ⇒ Client
constructor
Init client.
-
#post(url, query = {}, headers = {}) ⇒ Mash
Post data to Bungie services.
Constructor Details
#initialize(options) ⇒ Client
Init client
Initialize client for bungie api throw hash:
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/bungie_client/client.rb', line 35 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.
24 25 26 |
# File 'lib/bungie_client/client.rb', line 24 def api_key @api_key end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
24 25 26 |
# File 'lib/bungie_client/client.rb', line 24 def conn @conn end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
24 25 26 |
# File 'lib/bungie_client/client.rb', line 24 def token @token end |
Class Method Details
.parse(response) ⇒ Mash
Format answer from Bungie
14 15 16 17 18 19 20 21 22 |
# File 'lib/bungie_client/client.rb', line 14 def self.parse(response) response = begin MultiJson.load response rescue StandardError {} end Hashie::Mash.new response end |
Instance Method Details
#get(url, parameters = {}, headers = {}) ⇒ Mash
Get request to bungie service
73 74 75 76 77 |
# File 'lib/bungie_client/client.rb', line 73 def get(url, parameters = {}, headers = {}) self.class.parse @conn.get(url, parameters, headers).body rescue StandardError Hashie::Mash.new end |
#post(url, query = {}, headers = {}) ⇒ Mash
Post data to Bungie services
86 87 88 89 90 |
# File 'lib/bungie_client/client.rb', line 86 def post(url, query = {}, headers = {}) self.class.parse @conn.post(url, query, headers).body rescue StandardError Hashie::Mash.new end |