class EveBadger::EveAPI

Attributes

character_id[R]
domain[R]
key_id[R]
user_agent[RW]
vcode[R]

Public Class Methods

new(args={}) click to toggle source
# 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_mask() click to toggle source

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
access_mask=(mask) click to toggle source

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
account(endpoint_name) click to toggle source

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
character(endpoint_name) click to toggle source

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
character_id=(id) click to toggle source

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
corporation(endpoint_name) click to toggle source

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
details(endpoint_name, id_of_interest, fromid=nil, rowcount=nil) click to toggle source

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
key_id=(id) click to toggle source

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
key_type() click to toggle source

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
key_type=(type) click to toggle source

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
vcode=(code) click to toggle source

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

api_request(endpoint) click to toggle source

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
build_uri(endpoint) click to toggle source

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
cache_get(uri) click to toggle source

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
cached_until(xml) click to toggle source

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
fetch_key_info() click to toggle source

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
get_access_mask() click to toggle source

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
get_key_type() click to toggle source

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
get_response(uri) click to toggle source

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_of(uri) click to toggle source

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
http_get(uri) click to toggle source

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
params() click to toggle source

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_response(uri, response) click to toggle source

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