class EveOnline::ESI::Base

Constants

API_HOST

Attributes

_etag[R]
_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)
  @_etag = options.fetch(:etag, nil)
  @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 95
def add_middleware(middleware)
  @middlewares << middleware
end
additional_query_params() click to toggle source
# File lib/eve_online/esi/base.rb, line 129
def additional_query_params
  []
end
base_query_params() click to toggle source
# File lib/eve_online/esi/base.rb, line 133
def base_query_params
  []
end
connection() click to toggle source
# File lib/eve_online/esi/base.rb, line 99
def connection
  @connection ||= Faraday.new do |f|
    f.headers["User-Agent"] = user_agent
    f.headers["If-None-Match"] = _etag if _etag
    f.headers["Accept"] = "application/json"
    f.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|
      if middleware[:esi].present?
        f.use middleware[:class], esi: middleware[:esi]
      else
        f.use middleware[:class]
      end
    end
    f.response :json, content_type: "application/json"
    f.adapter adapter
  end
end
content() click to toggle source
# File lib/eve_online/esi/base.rb, line 161
def content
  if not_modified?
    raise EveOnline::Exceptions::NotModified
  else
    resource.body
  end
end
error_limit_remain() click to toggle source
# File lib/eve_online/esi/base.rb, line 83
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 87
def error_limit_reset
  resource.headers["x-esi-error-limit-reset"]&.to_i
end
etag() click to toggle source
# File lib/eve_online/esi/base.rb, line 72
def etag
  resource.headers["etag"]&.gsub("W/", "")&.gsub('"', "")
end
etag=(value) click to toggle source
# File lib/eve_online/esi/base.rb, line 68
def etag=(value)
  @_etag = value
end
http_method() click to toggle source
# File lib/eve_online/esi/base.rb, line 40
def http_method
  :get
end
not_modified?() click to toggle source
# File lib/eve_online/esi/base.rb, line 157
def not_modified?
  resource.status == 304
end
open_timeout() click to toggle source
# File lib/eve_online/esi/base.rb, line 52
def open_timeout
  connection.options.open_timeout
end
open_timeout=(value) click to toggle source
# File lib/eve_online/esi/base.rb, line 56
def open_timeout=(value)
  connection.options.open_timeout = value
end
page() click to toggle source
# File lib/eve_online/esi/base.rb, line 76
def page
end
path() click to toggle source
# File lib/eve_online/esi/base.rb, line 137
def path
  raise NotImplementedError
end
query() click to toggle source
# File lib/eve_online/esi/base.rb, line 141
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 44
def read_timeout
  connection.options.read_timeout
end
read_timeout=(value) click to toggle source
# File lib/eve_online/esi/base.rb, line 48
def read_timeout=(value)
  connection.options.read_timeout = value
end
request_id() click to toggle source
# File lib/eve_online/esi/base.rb, line 91
def request_id
  resource.headers["x-esi-request-id"]
end
resource() click to toggle source
# File lib/eve_online/esi/base.rb, line 151
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 169
def response
  @response ||= content
end
scope() click to toggle source
# File lib/eve_online/esi/base.rb, line 32
def scope
  raise NotImplementedError
end
total_pages() click to toggle source
# File lib/eve_online/esi/base.rb, line 79
def total_pages
  resource.headers["x-pages"]&.to_i
end
uri() click to toggle source
# File lib/eve_online/esi/base.rb, line 121
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 28
def url
  uri.to_s
end
user_agent() click to toggle source
# File lib/eve_online/esi/base.rb, line 36
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 60
def write_timeout
  connection.options.write_timeout
end
write_timeout=(value) click to toggle source
# File lib/eve_online/esi/base.rb, line 64
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 175
def parse_datetime_with_timezone(value)
  ActiveSupport::TimeZone["UTC"].parse(value)
end