Sha256: ca01d20dbf3385e0c6f053cdbd85ea3431aa2139a9cafeb81cf8cf77b4fc3727
Contents?: true
Size: 1.55 KB
Versions: 2
Compression:
Stored size: 1.55 KB
Contents
require 'battlenet/adapter_manager' require 'cgi' require 'json' module Battlenet module API class APIError < Exception attr_reader :code, :body def initialize(code, body) @code = code @body = body end def to_s "Status: #{code} Body: #{body}" end end extend self @config = { :indifferent_hashes => true, :http_adapter => :net_http, :region => :us } def set_option(setting, value) @config[setting] = value end def get_option(setting, default = nil) @config[setting] || default end def adapter @adapter ||= Battlenet::AdapterManager.fetch get_option(:http_adapter) end def base_url region = get_option(:region, :us).to_s "http://#{region}.battle.net/api/wow" end def make_api_call(path, query = {}) query_string = query.empty? ? '' : make_query_string(query) url = base_url url << (path.start_with?('/') ? '' : '/') url << path url << query_string code, body = get url raise APIError.new(code, body) unless code == 200 JSON::parse body end def get(url) adapter.get(url) end def make_query_string(query) query_string = "?" query.each do |key, value| case value when String query_string << "#{key}=#{CGI.escape value}&" when Array value.each { |v| query_string << "#{key}=#{CGI.escape v}&" } end end query_string.chomp("&").chomp("?") end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
battlenet-0.1.1 | lib/battlenet/api.rb |
battlenet-0.1.0 | lib/battlenet/api.rb |