Class: Warcraft::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/warcraft/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, region) ⇒ Client

Returns a new instance of Client.

Parameters:

  • token (String)

    Access Token

  • region (Symbol)

    Region to query for, e.g. :eu



13
14
15
16
# File 'lib/warcraft/client.rb', line 13

def initialize(token, region)
  self.token = token
  self.region = region
end

Instance Attribute Details

#regionObject

Returns the value of attribute region.



9
10
11
# File 'lib/warcraft/client.rb', line 9

def region
  @region
end

#tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/warcraft/client.rb', line 9

def token
  @token
end

Instance Method Details

#account(token: self.token, region: self.region) ⇒ Object



40
41
42
# File 'lib/warcraft/client.rb', line 40

def (token: self.token, region: self.region)
  AccountRequest.new(self, region, token)
end

#character(realm:, character_name:, region: self.region) ⇒ Object



44
45
46
# File 'lib/warcraft/client.rb', line 44

def character(realm:, character_name:, region: self.region)
  CharacterRequest.new(self, region, realm.slugify, character_name.slugify)
end

#get(url, region = nil, token = self.token) ⇒ Hash

Parameters:

  • url (String)
  • region (Symbol) (defaults to: nil)

Returns:

  • (Hash)


21
22
23
24
25
26
27
28
# File 'lib/warcraft/client.rb', line 21

def get(url, region = nil, token = self.token)
  token = self.token if token.nil?
  response = HTTParty.get(region.nil? ? url : "https://#{region}.api.blizzard.com#{url}",
                          headers: {
                            'Authorization': "Bearer #{token}"
                          }, format: :plain)
  JSON.parse(response.body, symbolize_names: true)
end

#userinfo(token = self.token, region = nil) ⇒ Object

Parameters:

  • region (Symbol) (defaults to: nil)

    Region to fetch data for (optional)



31
32
33
34
35
36
37
38
# File 'lib/warcraft/client.rb', line 31

def userinfo(token = self.token, region = nil)
  domain = region == :cn ? "www.battlenet.com.cn" : "#{region}.api.blizzard.com"
  response = HTTParty.get("https://#{domain}/oauth/userinfo?access_token",
                          headers: {
                            'Authorization': "Bearer #{token}"
                          }, format: :plain)
  JSON.parse(response.body, symbolize_names: true)
end