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 @agent [Mechanzie].
Constant Summary collapse
- BUNGIE_URI =
'https://www.bungie.net/Platform'
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.parse_response(response) ⇒ Hashie::Mash
Format answer from bungie.
-
.request_uri(uri) ⇒ String
Form uri for requests.
Instance Method Summary collapse
-
#get(uri, parameters = {}) ⇒ String|nil
Get response from bungie services.
-
#get_response(uri, parameters = {}) ⇒ Hashie::Mash
Get Response field after sending GET request to bungie.
-
#initialize(options) ⇒ Client
constructor
Init client.
-
#post(uri, query = {}) ⇒ String|nil
Post data to bungie services.
-
#post_response(uri, query = {}) ⇒ Hashie::Mash
Get Response field after post request to bungie.
Constructor Details
#initialize(options) ⇒ Client
Init client
Initialize client for bungie api throw hash:
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bungie_client/client.rb', line 51 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 # init @agent @agent = Mechanize.new do |config| config.read_timeout = 5 end # merge cookies with options if BungieClient::Auth. [:cookies], true = ([:cookies].is_a? CookieJar) ? [:cookies]. : [:cookies] .each do || @agent..add end end unless [:cookies].nil? # make authentication and save new cookies in client unless [:username].nil? || [:password].nil? jar = BungieClient::Auth.auth [:username].to_s, [:password].to_s, ([:type].to_s || 'psn') if jar.nil? raise "Wrong authentication. Check your account data." else jar..each do || @agent..add end end end end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
37 38 39 |
# File 'lib/bungie_client/client.rb', line 37 def agent @agent end |
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
33 34 35 |
# File 'lib/bungie_client/client.rb', line 33 def api_key @api_key end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
35 36 37 |
# File 'lib/bungie_client/client.rb', line 35 def password @password end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
36 37 38 |
# File 'lib/bungie_client/client.rb', line 36 def type @type end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
34 35 36 |
# File 'lib/bungie_client/client.rb', line 34 def username @username end |
Class Method Details
.parse_response(response) ⇒ Hashie::Mash
Format answer from bungie
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bungie_client/client.rb', line 19 def self.parse_response(response) if !response.nil? && response != '' response = JSON.parse response if response.is_a?(Hash) && !response['Response'].nil? && response['ErrorCode'] == 1 response = Hashie::Mash.new response return response['Response'] end end Hashie::Mash.new end |
.request_uri(uri) ⇒ String
Form uri for requests
10 11 12 |
# File 'lib/bungie_client/client.rb', line 10 def self.request_uri(uri) "#{BUNGIE_URI}/#{uri.sub(/^\//, '').sub(/\/$/, '')}/" end |
Instance Method Details
#get(uri, parameters = {}) ⇒ String|nil
Get response from bungie services
97 98 99 |
# File 'lib/bungie_client/client.rb', line 97 def get(uri, parameters = {}) @agent.get(self.class.request_uri(uri), parameters, nil, headers).body rescue nil end |
#get_response(uri, parameters = {}) ⇒ Hashie::Mash
Get Response field after sending GET request to bungie
107 108 109 |
# File 'lib/bungie_client/client.rb', line 107 def get_response(uri, parameters = {}) self.class.parse_response get(uri, parameters) end |
#post(uri, query = {}) ⇒ String|nil
Post data to bungie services
117 118 119 |
# File 'lib/bungie_client/client.rb', line 117 def post(uri, query = {}) @agent.post(self.class.request_uri(uri), query, headers).body rescue nil end |
#post_response(uri, query = {}) ⇒ Hashie::Mash
Get Response field after post request to bungie
127 128 129 |
# File 'lib/bungie_client/client.rb', line 127 def post_response(uri, query = {}) self.class.parse_response post(uri, query) end |