Sha256: 883054529e27669edf6bf00b069b3261d8f68c35fc83017c04ccf913e953de35

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

class Bnet::WOW::Data::Base < Bnet::BnetResource
  # 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
  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

  def self.collection_from_api(response_collection)
    response_collection[scopes[:collection_root]].collect do |attrs|
      from_api(attrs)
    end
  end

  def self.url(collection_scope)
    "#{Bnet::WOW.url}/data/#{collection_scope}"
  end

  def self.scopes
    self::SCOPES
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bnet-0.0.10 lib/bnet/wow/data/base.rb
bnet-0.0.5 lib/bnet/wow/data/base.rb
bnet-0.0.4 lib/bnet/wow/data/base.rb
bnet-0.0.3 lib/bnet/wow/data/base.rb
bnet-0.0.2 lib/bnet/wow/data/base.rb
bnet-0.0.1 lib/bnet/wow/data/base.rb