Module: BnetApi

Extended by:
BnetApi
Includes:
HTTParty
Included in:
BnetApi
Defined in:
lib/bnet_api.rb,
lib/bnet_api/d3.rb,
lib/bnet_api/sc2.rb,
lib/bnet_api/wow.rb,
lib/bnet_api/oauth.rb,
lib/bnet_api/version.rb,
lib/bnet_api/wow_data.rb

Overview

The base module namespace for the BnetApi library.

Defined Under Namespace

Modules: D3, OAuth, SC2, WoW

Constant Summary collapse

VERSION =
'1.0.2'

Instance Method Summary collapse

Instance Method Details

#configObject

Sets up the configuration of the API.



22
23
24
# File 'lib/bnet_api.rb', line 22

def config
  @config ||= OpenStruct.new(region: :eu, locale: :en_GB)
end

#configure {|config| ... } ⇒ Object

Yields the API configuration object.

Yields:



17
18
19
# File 'lib/bnet_api.rb', line 17

def configure
  yield config
end

#make_request(request, *optional_fields) ⇒ Hash

Sends a HTTPS GET request to the Battle.net API and returns the retrieved data in a hash.

Parameters:

  • request (String)

    The API endpoint to request.

  • *optional_fields (Array<Symbol>)

    Any optional fields to be requested from the API.

Returns:

  • (Hash)

    A hash of the data returned from the API.



31
32
33
# File 'lib/bnet_api.rb', line 31

def make_request(request, *optional_fields)
  self.get("https://#{@config.region}.api.battle.net/#{request}#{build_url_params(optional_fields)}")
end

#make_request_oauth(request, access_token) ⇒ Hash

Sends a HTTPS GET request to the Battle.net API using OAuth instead of API key authentication and returns the retrieved data in a hash.

Parameters:

  • request (String)

    The API endpoint to request.

  • access_token (String)

    The user's OAuth access token.

Returns:

  • (Hash)

    A hash of the data returned from the API.



57
58
59
# File 'lib/bnet_api.rb', line 57

def make_request_oauth(request, access_token)
  self.get("https://#{@config.region}.api.battle.net/#{request}?locale=#{@config.locale}&access_token=#{access_token}")
end

#make_request_with_params(request, params) ⇒ Hash

Sends a HTTPS GET request to the Battle.net API with some additional URL parameters and returns the retrieved data in a hash.

Parameters:

  • request (String)

    The API endpoint to request.

  • params (Hash)

    A hash containing key/value pairs of URL parameters.

Returns:

  • (Hash)

    A hash of the data returned from the API.



41
42
43
44
45
46
47
48
49
# File 'lib/bnet_api.rb', line 41

def make_request_with_params(request, params)
  url = "https://#{@config.region}.api.battle.net/#{request}?"
  params.each do |k, v|
    url += "#{k}=#{v}&"
  end
  url += "locale=#{@config.locale}&apikey=#{@config.api_key}"

  self.get(url)
end