class EveOnline::ESI::Base

Constants

API_HOST

Attributes

_open_timeout[R]
_read_timeout[R]
_write_timeout[R]
adapter[R]
language[R]
middlewares[R]
token[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/eve_online/esi/base.rb, line 17
def initialize(options = {})
  @token = options.fetch(:token, nil)
  @_read_timeout = options.fetch(:read_timeout, 60)
  @_open_timeout = options.fetch(:open_timeout, 60)
  @_write_timeout = options.fetch(:write_timeout, 60)
  @language = options.fetch(:language, "en-us")
  @adapter = options.fetch(:adapter, Faraday.default_adapter)
  @middlewares = options.fetch(:middlewares, [])
end

Public Instance Methods

add_middleware(middleware) click to toggle source
# File lib/eve_online/esi/base.rb, line 86
def add_middleware(middleware)
  @middlewares << middleware
end
additional_query_params() click to toggle source
# File lib/eve_online/esi/base.rb, line 115
def additional_query_params
  []
end
base_query_params() click to toggle source
# File lib/eve_online/esi/base.rb, line 119
def base_query_params
  []
end
connection() click to toggle source
# File lib/eve_online/esi/base.rb, line 90
def connection
  @connection ||= Faraday.new do |f|
    f.headers["User-Agent"] = user_agent
    f.headers["Accept"] = "application/json"
    f.request :authorization, "Bearer", token if token
    f.options.read_timeout = _read_timeout
    f.options.open_timeout = _open_timeout
    f.options.write_timeout = _write_timeout
    f.use FaradayMiddlewares::RaiseErrors
    middlewares.each do |middleware|
      f.use middleware[:class], esi: self
    end
    f.response :json, content_type: "application/json"
    f.adapter adapter
  end
end
error_limit_remain() click to toggle source
# File lib/eve_online/esi/base.rb, line 74
def error_limit_remain
  resource.headers["x-esi-error-limit-remain"]&.to_i
end
error_limit_reset() click to toggle source
# File lib/eve_online/esi/base.rb, line 78
def error_limit_reset
  resource.headers["x-esi-error-limit-reset"]&.to_i
end
http_method() click to toggle source
# File lib/eve_online/esi/base.rb, line 39
def http_method
  :get
end
open_timeout() click to toggle source
# File lib/eve_online/esi/base.rb, line 51
def open_timeout
  connection.options.open_timeout
end
open_timeout=(value) click to toggle source
# File lib/eve_online/esi/base.rb, line 55
def open_timeout=(value)
  connection.options.open_timeout = value
end
page() click to toggle source
# File lib/eve_online/esi/base.rb, line 67
def page
end
path() click to toggle source
# File lib/eve_online/esi/base.rb, line 123
def path
  raise NotImplementedError
end
query() click to toggle source
# File lib/eve_online/esi/base.rb, line 127
def query
  hash = {}

  (base_query_params + additional_query_params).each do |param|
    hash[param] = public_send(param)
  end

  hash.reject { |_, v| v.nil? || v == "" }
end
read_timeout() click to toggle source
# File lib/eve_online/esi/base.rb, line 43
def read_timeout
  connection.options.read_timeout
end
read_timeout=(value) click to toggle source
# File lib/eve_online/esi/base.rb, line 47
def read_timeout=(value)
  connection.options.read_timeout = value
end
request_id() click to toggle source
# File lib/eve_online/esi/base.rb, line 82
def request_id
  resource.headers["x-esi-request-id"]
end
resource() click to toggle source
# File lib/eve_online/esi/base.rb, line 137
def resource
  @resource ||= connection.public_send(http_method, uri)
rescue Faraday::ConnectionFailed, Faraday::TimeoutError
  raise EveOnline::Exceptions::Timeout
end
response() click to toggle source
# File lib/eve_online/esi/base.rb, line 143
def response
  @response ||= resource.body
end
scope() click to toggle source
# File lib/eve_online/esi/base.rb, line 31
def scope
  raise NotImplementedError
end
total_pages() click to toggle source
# File lib/eve_online/esi/base.rb, line 70
def total_pages
  resource.headers["x-pages"]&.to_i
end
uri() click to toggle source
# File lib/eve_online/esi/base.rb, line 107
def uri
  @uri ||= begin
    params = {host: API_HOST, path: path}
    params[:query] = query.to_query if query.presence
    URI::HTTPS.build(params)
  end
end
url() click to toggle source
# File lib/eve_online/esi/base.rb, line 27
def url
  uri.to_s
end
user_agent() click to toggle source
# File lib/eve_online/esi/base.rb, line 35
def user_agent
  "EveOnline API (https://github.com/evemonk/eve_online) v#{VERSION}"
end
write_timeout() click to toggle source
# File lib/eve_online/esi/base.rb, line 59
def write_timeout
  connection.options.write_timeout
end
write_timeout=(value) click to toggle source
# File lib/eve_online/esi/base.rb, line 63
def write_timeout=(value)
  connection.options.write_timeout = value
end

Private Instance Methods

parse_datetime_with_timezone(value) click to toggle source
# File lib/eve_online/esi/base.rb, line 149
def parse_datetime_with_timezone(value)
  ActiveSupport::TimeZone["UTC"].parse(value)
end