class EveBadger::EveAPI
Attributes
Public Class Methods
# File lib/eve_badger/eve_api.rb, line 11 def initialize(args={}) @domain = args[:sisi] ? EveBadger.default_sisi_domain : EveBadger.default_tq_domain @user_agent = EveBadger.default_user_agent @key_id = args[:key_id].to_s if args[:key_id] @vcode = args[:vcode].to_s if args[:vcode] @character_id = args[:character_id].to_s if args[:character_id] @access_mask = args[:access_mask].to_i if args[:access_mask] @key_type = args[:key_type].to_sym if args[:key_type] end
Public Instance Methods
access or retrieve access_mask
, will also set key_type
if an automatic fetch is triggered
# File lib/eve_badger/eve_api.rb, line 47 def access_mask @access_mask ||= get_access_mask end
sets access_mask
, coerces to integer
# File lib/eve_badger/eve_api.rb, line 37 def access_mask=(mask) @access_mask = mask ? mask.to_i : nil end
takes an account endpoint name and returns a response object, raises an APIKeyError
if the request would fail
# File lib/eve_badger/eve_api.rb, line 57 def account(endpoint_name) raise EveBadger::APIKeyError, 'missing required key_id or vcode' unless @key_id && @vcode endpoint = EveBadger::Endpoints.account(endpoint_name.to_sym) api_request(endpoint) end
takes a character endpoint name and returns a response object, raises an APIKeyError
if the request would fail
# File lib/eve_badger/eve_api.rb, line 64 def character(endpoint_name) raise EveBadger::APIKeyError, 'missing required character_id key_id or_vcode' unless @character_id && @key_id && @vcode raise EveBadger::APIKeyError, 'wrong key type' unless [:Character, :Account].include?(key_type) endpoint = EveBadger::Endpoints.character(endpoint_name.to_sym) api_request(endpoint) end
sets character_id
, coerces to string
# File lib/eve_badger/eve_api.rb, line 32 def character_id=(id) @character_id = id ? id.to_s : nil end
takes a corporation endpoint name and returns a response object, raises an APIKeyError
if the request would fail
# File lib/eve_badger/eve_api.rb, line 72 def corporation(endpoint_name) raise EveBadger::APIKeyError, 'missing required character_id key_id or_vcode' unless @character_id && @key_id && @vcode raise EveBadger::APIKeyError, 'wrong key type' unless key_type == :Corporation endpoint = EveBadger::Endpoints.corporation(endpoint_name.to_sym) api_request(endpoint) end
takes a detail endpoint name and id of interest then returns a response from the given endpoint name, raises an APIKeyError
if the request would fail
# File lib/eve_badger/eve_api.rb, line 80 def details(endpoint_name, id_of_interest, fromid=nil, rowcount=nil) raise EveBadger::APIKeyError, 'wrong key type' unless [:Character, :Corporation, :Account].include?(key_type) endpoint = EveBadger::Endpoints.detail(endpoint_name.to_sym) if endpoint.permitted?(access_mask) uri = build_uri(endpoint) uri << "&#{endpoint.detail_id}=#{id_of_interest}" uri << "&fromID=#{fromid}" if fromid uri << "&rowCount=#{rowcount}" if rowcount get_response(uri) else raise EveBadger::APIKeyError, "#{endpoint.path} not permitted by access mask" end end
sets key_it, coerces to string
# File lib/eve_badger/eve_api.rb, line 22 def key_id=(id) @key_id = id ? id.to_s : nil end
access or retrieve key_type
, will also set access_mask
if an automatic fetch is triggered
# File lib/eve_badger/eve_api.rb, line 52 def key_type @key_type ||= get_key_type end
sets key_type
, coerces to symbol
# File lib/eve_badger/eve_api.rb, line 42 def key_type=(type) @key_type = type ? type.to_sym : nil end
sets vcode, coerces to string
# File lib/eve_badger/eve_api.rb, line 27 def vcode=(code) @vcode = code ? code.to_s : nil end
Private Instance Methods
takes an endpoint hash and makes an api request to it, raises an APIKeyError
if the request would fail
# File lib/eve_badger/eve_api.rb, line 96 def api_request(endpoint) if endpoint.access_mask.zero? or endpoint.permitted?(access_mask) get_response(build_uri(endpoint)) else raise EveBadger::APIKeyError, "#{endpoint.path} not permitted by access mask" end end
builds a uri string for a given endpoint
# File lib/eve_badger/eve_api.rb, line 124 def build_uri(endpoint) "#{@domain}#{endpoint.path}.xml.aspx#{params}" end
get an http response from the cache if is enabled, returns nil if expired or not found
# File lib/eve_badger/eve_api.rb, line 140 def cache_get(uri) if EveBadger::Cache.enabled? EveBadger::Cache.get(hash_of(uri)) end end
returns the number of seconds until the cachedUntil value in the xml response from the the API
# File lib/eve_badger/eve_api.rb, line 170 def cached_until(xml) noko = Nokogiri::XML xml seconds_until_expire = Time.parse(noko.xpath('//cachedUntil').text) seconds_until_expire.to_i - Time.now.to_i end
sets @access_mask and @key_type from the public :api_key_info endpoint
# File lib/eve_badger/eve_api.rb, line 117 def fetch_key_info info = account(:api_key_info).result_as_json @access_mask = info['key']['@accessMask'].to_i @key_type = info['key']['@type'].to_sym end
returns the access mask for a key and will retrieve it if not present
# File lib/eve_badger/eve_api.rb, line 105 def get_access_mask fetch_key_info unless @access_mask @access_mask end
returns api key type as a symbol example: :Account, :Character, :Corporation
# File lib/eve_badger/eve_api.rb, line 111 def get_key_type fetch_key_info unless @key_type @key_type end
attempts to get a http response from the request cache first, then makes an http request if not found
# File lib/eve_badger/eve_api.rb, line 134 def get_response(uri) response = cache_get(uri) || http_get(uri) EveBadger::Response.new(response) end
Hash URI’s before use as a cache key so that API key/vcode combinations don’t into log files of dependencies.
# File lib/eve_badger/eve_api.rb, line 165 def hash_of(uri) Digest::SHA1.hexdigest(uri) end
get a uri via http
# File lib/eve_badger/eve_api.rb, line 147 def http_get(uri) begin response = open(uri) { |res| res.read } rescue OpenURI::HTTPError => error response = error.io.string end store_response(uri, response) response || cache_get(uri) end
builds the default params string for most requests
# File lib/eve_badger/eve_api.rb, line 129 def params "?keyID=#{@key_id}&vCode=#{@vcode}#{"&characterID=#{@character_id}" if @character_id}" end
store an http response in the cache if it is enabled
# File lib/eve_badger/eve_api.rb, line 158 def store_response(uri, response) if EveBadger::Cache.enabled? EveBadger::Cache.store(hash_of(uri), response, expires: cached_until(response)) end end