class Wowapi

Constants

VERSION

Attributes

fail_silently[RW]

Raise exceptions on error responses from API endpoint?

region[RW]

Wowapi.region class variable that holds customer region (:eu, :us, …) DEFAULTS TO :eu

public_key[RW]

Instance variables we keep public_key and secret_key in

secret_key[RW]

Instance variables we keep public_key and secret_key in

Public Class Methods

new() { |self| ... } click to toggle source

Creating an instance of Wowapi class

# File lib/wowapi/wowapi.rb, line 16
def initialize
  yield self if block_given?
end

Public Instance Methods

get(path, params = {}) click to toggle source

Method to handle all requests, properly interpreting HTTP answer & returning it's response

# File lib/wowapi/wowapi.rb, line 22
def get(path, params = {})
  res = make_request(path, params)
  if !(res.status.include?('200') or res.status.include?(200)) && ! Wowapi.fail_silently
    raise Wowapi::ApiException.new, "Did not receive status 200, but #{res.try(:status).try(:first)} (#{res.class})."
  end
  res.read
end
region() click to toggle source
# File lib/wowapi/region.rb, line 14
def region
  Wowapi.region
end
region=(region) click to toggle source
# File lib/wowapi/region.rb, line 10
def region=(region)
  Wowapi.region = region if region.is_a?(Symbol)
end

Private Instance Methods

base_url() click to toggle source

Returns URL for particular region or throws an exception if region is nonexistent

# File lib/wowapi/wowapi.rb, line 46
def base_url
  case Wowapi.region
  when :eu
    'eu.api.battle.net/wow/'
  when :us
    'us.api.battle.net/wow/'
  else
    raise Wowapi::RegionException
  end
end
make_request(path, params) click to toggle source

Making open-uri request, catching exceptions and returning data back to get method

# File lib/wowapi/wowapi.rb, line 34
def make_request(path, params)
  begin
    raise Wowapi::NoCredentialsException unless public_key
    req_uri = URI.encode("https://#{base_url}#{path}#{params}&apikey=#{self.public_key}")
    open(req_uri)
  rescue OpenURI::HTTPError => e
    raise Wowapi::ApiException.new, "Could not make a request:\n#{e.inspect}" unless Wowapi.fail_silently
  end
end