module BnetApi

The base module namespace for the BnetApi library.

Constants

VERSION

Public Instance Methods

config() click to toggle source

Sets up the configuration of the API.

# File lib/bnet_api.rb, line 22
def config
  @config ||= OpenStruct.new(region: :eu, locale: :en_GB)
end
configure() { |config| ... } click to toggle source

Yields the API configuration object.

# File lib/bnet_api.rb, line 17
def configure
  yield config
end
make_request(request, *optional_fields) click to toggle source

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

@param request [String] The API endpoint to request. @param *

# 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) click to toggle source

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.

@param request [String] The API endpoint to request. @param access_token [String] The user's OAuth access token. @return [Hash] A hash of the data returned from the API.

# 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) click to toggle source

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

@param request [String] The API endpoint to request. @param params [Hash] A hash containing key/value pairs of URL parameters. @return [Hash] A hash of the data returned from the API.

# 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

Private Instance Methods

build_url_params(*optional_fields) click to toggle source

Builds the URL parameters for the make_request method.

@params *optional_fields [Array<Symbol>] Any optional fields to put in the URL. @return [String] The URL parameters for the request.

# File lib/bnet_api.rb, line 66
def build_url_params(*optional_fields)
  url_params = "?locale=#{@config.locale}&apikey=#{@config.api_key}"
  if optional_fields != nil
    url_params += "&fields=#{optional_fields.join(',')}"
  end
  return url_params
end