Class: Battlenet
- Inherits:
-
Object
- Object
- Battlenet
- Includes:
- Modules::Achievement, Modules::Arena, Modules::Auction, Modules::Character, Modules::Data, Modules::Guild, Modules::Item, Modules::Pvp, Modules::Quest, Modules::Realm, Modules::Recipe, HTTParty
- Defined in:
- lib/battlenet/battlenet.rb,
lib/battlenet/version.rb,
lib/battlenet/modules/pvp.rb,
lib/battlenet/modules/data.rb,
lib/battlenet/modules/item.rb,
lib/battlenet/modules/arena.rb,
lib/battlenet/modules/guild.rb,
lib/battlenet/modules/quest.rb,
lib/battlenet/modules/realm.rb,
lib/battlenet/authentication.rb,
lib/battlenet/modules/recipe.rb,
lib/battlenet/modules/auction.rb,
lib/battlenet/modules/character.rb,
lib/battlenet/modules/achievement.rb,
lib/battlenet/exceptions/api_exception.rb
Overview
Battlenet exposes the Blizzard Battle.net Community Platform API via an easy-to-use interface.
The main API class includes several Modules that define methods for collecting
specific API data. See the documentation for Battlenet::Modules
for a list of
these modules.
Specific details about the information returned from the API can be found at Blizzard's official Community Platform API documentation.
Defined Under Namespace
Modules: Modules Classes: ApiException, Authentication
Constant Summary collapse
- VERSION =
"1.4.0"
Class Attribute Summary collapse
-
.fail_silently ⇒ boolean
Whether or not to raise exceptions on error responses from the API endpoint.
-
.locale ⇒ String|nil
The locale to use for API calls.
Instance Method Summary collapse
-
#fullpath(path) ⇒ String
private
Returns the full URI for the given path based on the API endpoint set (varies by region).
-
#get(path, params = {}) ⇒ Object
Signs and performs an HTTP GET request.
-
#initialize(region = :us, public = nil, private = nil) ⇒ Battlenet
constructor
Creates a new instance of the Battlenet API.
-
#make_request(verb, path, params = {}) ⇒ Object
private
Signs and performs an HTTP request.
- #process_response(response) ⇒ Object private
-
#sign_request(verb, path, time) ⇒ String
private
Signs an HTTP request.
Methods included from Modules::Data
#battlegroups, #character_achievements, #character_classes, #character_races, #guild_achievements, #guild_perks, #guild_rewards, #item_classes
Methods included from Modules::Pvp
#arena_ladder, #rated_bg_ladder
Methods included from Modules::Arena
Methods included from Modules::Quest
Methods included from Modules::Recipe
Methods included from Modules::Achievement
Methods included from Modules::Item
Methods included from Modules::Auction
Methods included from Modules::Realm
Methods included from Modules::Guild
Methods included from Modules::Character
Constructor Details
#initialize(region = :us, public = nil, private = nil) ⇒ Battlenet
Creates a new instance of the Battlenet API.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/battlenet/battlenet.rb', line 88 def initialize(region = :us, public = nil, private = nil) @public = public @private = private @proto = @public && @private ? "https://" : "http://" @endpoint = '/api/wow' @domain = case region when :us 'us.battle.net' when :eu 'eu.battle.net' when :kr 'kr.battle.net' when :tw 'tw.battle.net' when :cn 'battlenet.com.cn' else raise "Invalid region: #{region.to_s}" end @base_uri = "#{@proto}#{@domain}#{@endpoint}" self.class.base_uri @base_uri end |
Class Attribute Details
.fail_silently ⇒ boolean
Whether or not to raise exceptions on error responses from the API endpoint.
A value of false
causes exceptions to be raised. Defaults to false
.
71 72 73 |
# File 'lib/battlenet/battlenet.rb', line 71 def fail_silently @fail_silently end |
.locale ⇒ String|nil
The locale to use for API calls. Defaults to nil
, which makes requests with
no locale
parameter set.
77 78 79 |
# File 'lib/battlenet/battlenet.rb', line 77 def locale @locale end |
Instance Method Details
#fullpath(path) ⇒ String (private)
Returns the full URI for the given path based on the API endpoint set (varies by region).
130 131 132 |
# File 'lib/battlenet/battlenet.rb', line 130 def fullpath(path) "#{@endpoint}#{path}" end |
#get(path, params = {}) ⇒ Object
Signs and performs an HTTP GET request. The request is only signed if a public and private key were provided during object instantiation.
121 122 123 |
# File 'lib/battlenet/battlenet.rb', line 121 def get(path, params = {}) make_request :get, path, params end |
#make_request(verb, path, params = {}) ⇒ Object (private)
Signs and performs an HTTP request. The request is only signed if a public and private key were provided during object instantiation.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/battlenet/battlenet.rb', line 145 def make_request(verb, path, params = {}) = {} headers = {} if @public && @private now = Time.now signed = sign_request verb, path, now headers.merge!({ "Authorization" => "BNET #{@public}:#{signed}", "Date" => now.httpdate }) end [:headers] = headers unless headers.empty? [:query] = params unless params.empty? if Battlenet.locale [:query] ||= {} [:query].merge!({ :locale => Battlenet.locale }) end response = self.class.send(verb, path, ) process_response response end |
#process_response(response) ⇒ Object (private)
170 171 172 173 174 175 |
# File 'lib/battlenet/battlenet.rb', line 170 def process_response(response) if response.code.to_s =~ /^(4|5)/ && Battlenet.fail_silently == false raise Battlenet::ApiException.new(response) end response end |
#sign_request(verb, path, time) ⇒ String (private)
Signs an HTTP request.
184 185 186 187 |
# File 'lib/battlenet/battlenet.rb', line 184 def sign_request(verb, path, time) auth = Battlenet::Authentication.new @private auth.sign verb, fullpath(path), time end |