class Bnet::WOW::Data::Base

Public Class Methods

find_all(args) click to toggle source

Query Battlenet API for the the data based on the subclass’s scope

Hash Params:

:region          - (e.g. 'us', 'ea')
:name            - String name of the toon
:realm           - String name of the server the character is on (String)
:locale          - String locale (defaults to 'en_US')
:api_key         - String api key

Example : If a character named ‘AlexeiStukov’ is on ‘DragonMaw’ ‘US’ server

# File lib/bnet/wow/data/base.rb, line 12
def self.find_all(args)
  region = args.delete(:region)
  api_key    = args.delete(:api_key) || Bnet::configuration.api_key
  locale     = args.delete(:locale) || 'en_US'

  base_api = Bnet::WOW.new(region: region)
  call_url = base_api.url + 'data/' + scopes[:url] + "?locale=#{locale}&apikey=#{api_key}"

  begin
    data = open(call_url)
    raw_response = JSON.parse(data.read)

    if Bnet::API.valid_call?(data.status, raw_response)
      collection = collection_from_api(raw_response)
    else
      collection = []
    end


  rescue OpenURI::HTTPError => e
    collection = []
  end

  return collection
end

Private Class Methods

collection_from_api(response_collection) click to toggle source
# File lib/bnet/wow/data/base.rb, line 40
def self.collection_from_api(response_collection)
  response_collection[scopes[:collection_root]].collect do |attrs|
    from_api(attrs)
  end
end
scopes() click to toggle source
# File lib/bnet/wow/data/base.rb, line 50
def self.scopes
  self::SCOPES
end
url(collection_scope) click to toggle source
# File lib/bnet/wow/data/base.rb, line 46
def self.url(collection_scope)
  "#{Bnet::WOW.url}/data/#{collection_scope}"
end