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.
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#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
-
.request_uri(uri) ⇒ String
Form uri for requests.
Instance Method Summary collapse
-
#allow_get_cache(options = {}) ⇒ Boolean
Check options for allowing getting of cache.
-
#allow_set_cache(options = {}) ⇒ Boolean
Check options for allowing setting of cache.
-
#get(uri, parameters = {}) ⇒ String|nil
Get response from bungie services.
-
#get_response(uri, parameters = {}, options = {}) ⇒ Array|Hash|nil
Get Response field after get request to bungie.
-
#initialize(options) ⇒ Client
constructor
Init client.
-
#post(uri, query = {}) ⇒ String|nil
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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 # init @cache unless [:cache].nil? if [:cache].is_a? BungieClient::Cache @cache = [:cache] else raise 'Cache client must be inhereted from [BungieClient::Cache].' end end # init @agent @agent = Mechanize.new # make authentication if [:authentication] @username = [:username] if [:username].is_a? String @password = [:password] if [:password].is_a? String @type = [:type].to_s if ['psn', 'live'].include? [:type].to_s = BungieClient::Auth.auth @username, @password, (@type || 'psn') if .nil? raise "Wrong authentication. Check your account data." else @agent. = end end # merge cookies with options if BungieClient::Auth. [:cookies], true = ([:cookies].is_a? Array) ? : . .each do || @agent..add end end unless [:cookies].nil? end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
18 19 20 |
# File 'lib/bungie_client/client.rb', line 18 def agent @agent end |
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/bungie_client/client.rb', line 14 def api_key @api_key end |
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
19 20 21 |
# File 'lib/bungie_client/client.rb', line 19 def cache @cache end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
16 17 18 |
# File 'lib/bungie_client/client.rb', line 16 def password @password end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
17 18 19 |
# File 'lib/bungie_client/client.rb', line 17 def type @type end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
15 16 17 |
# File 'lib/bungie_client/client.rb', line 15 def username @username end |
Class Method Details
.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
#allow_get_cache(options = {}) ⇒ Boolean
Check options for allowing getting of cache
87 88 89 |
# File 'lib/bungie_client/client.rb', line 87 def allow_get_cache( = {}) allow_set_cache() && [:cache_rewrite] != true end |
#allow_set_cache(options = {}) ⇒ Boolean
Check options for allowing setting of cache
96 97 98 |
# File 'lib/bungie_client/client.rb', line 96 def allow_set_cache( = {}) !@cache.nil? && [:cache_none] != true end |
#get(uri, parameters = {}) ⇒ String|nil
Get response from bungie services
108 109 110 |
# File 'lib/bungie_client/client.rb', line 108 def get(uri, parameters = {}) @agent.get(self.class.request_uri(uri), parameters, nil, headers).body rescue nil end |
#get_response(uri, parameters = {}, options = {}) ⇒ Array|Hash|nil
Get Response field after get request to bungie
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/bungie_client/client.rb', line 122 def get_response(uri, parameters = {}, = {}) if allow_get_cache result = @cache.get "#{uri}+#{parameters}" return result unless result.nil? end result = get uri, parameters if !result.nil? && result != '' result = JSON.parse result if !result['Response'].nil? && !result['ErrorCode'].nil? && result['ErrorCode'] == 1 @cache.set "#{uri}+#{parameters}", result['Response'], [:cache_ttl] if allow_set_cache result['Response'] end end end |
#post(uri, query = {}) ⇒ String|nil
Post data to bungie services
148 149 150 |
# File 'lib/bungie_client/client.rb', line 148 def post(uri, query = {}) @agent.post(self.class.request_uri(uri), query, headers).body rescue nil end |