class Emarsys::Api::Base

Public Instance Methods

client() click to toggle source
# File lib/emarsys/api/base.rb, line 9
def client
  @client ||= Faraday.new(url: base_url) do |faraday|
    faraday.use FaradayMiddleware::Escher::RequestSigner, escher_config

    faraday.response :json, content_type: /\bjson$/
    faraday.response :json_fix

    faraday.adapter Faraday.default_adapter
  end
end

Private Instance Methods

base_url() click to toggle source
# File lib/emarsys/api/base.rb, line 31
def base_url
  uri.to_s
end
error_class_by_status(status) click to toggle source
# File lib/emarsys/api/base.rb, line 53
def error_class_by_status(status)
  case status
  when 200 then nil
  when 202, 423 then RecoverableError
  when 404 then IrrecoverableError
  when 400 then BadRequestError
  when 401 then UnauthorizedError
  else Error
  end
end
escher_config() click to toggle source
# File lib/emarsys/api/base.rb, line 39
def escher_config
  {
    credential_scope: 'eu/suite/ems_request',
    host: hostname,
    options: ESCHER_AUTH_OPTIONS,
    active_key: -> { ::Escher::Keypool.new.get_active_key('suite') }
  }
end
hostname() click to toggle source
# File lib/emarsys/api/base.rb, line 35
def hostname
  uri.hostname
end
module_path() click to toggle source
# File lib/emarsys/api/base.rb, line 27
def module_path
  ''
end
parse_for_error(response) click to toggle source
# File lib/emarsys/api/base.rb, line 64
def parse_for_error(response)
  [response.body, response.status, nil]
end
uri() click to toggle source
# File lib/emarsys/api/base.rb, line 23
def uri
  URI.join(Emarsys::Api.base_url, module_path)
end
validate_response!(response) click to toggle source
# File lib/emarsys/api/base.rb, line 48
def validate_response!(response)
  error_class = error_class_by_status response.status
  raise error_class.new(*parse_for_error(response)) if error_class
end