module ArtirixDataModels::DataGateway::ConnectionLoader

Public Class Methods

connection(config: {}, url: nil, login: nil, password: nil, bearer_token: nil, token_hash: nil, log_body_request: nil, log_body_response: nil, faraday_build_proc: nil, faraday_adapter: nil) click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 269
def connection(config: {},
               url: nil,
               login: nil,
               password: nil,
               bearer_token: nil,
               token_hash: nil,
               log_body_request: nil,
               log_body_response: nil,
               faraday_build_proc: nil,
               faraday_adapter: nil)

  url ||= config.try :url
  login ||= config.try :login
  password ||= config.try :password
  bearer_token ||= config.try :bearer_token
  token_hash ||= config.try :token_hash
  faraday_adapter ||= config.try(:faraday_adapter) || Faraday.default_adapter
  log_body_request ||= log_body_request.nil? ? config.try(:log_body_request) : log_body_request
  log_body_response ||= log_body_response.nil? ? config.try(:log_body_response) : log_body_response

  raise InvalidConnectionError, 'no url given, nor is it present in `config.url`' unless url.present?

  Faraday.new(url: url, request: { params_encoder: Faraday::FlatParamsEncoder }) do |faraday|
    if faraday_build_proc.present? && faraday_build_proc.respond_to?(:call)
      faraday_build_proc.call faraday
    end

    faraday.request :url_encoded # form-encode POST params
    # faraday.response :logger # log requests to STDOUT
    faraday.response :logger, ::Logger.new(STDOUT), bodies: { request: log_body_request, response: log_body_response }

    if login.present? || password.present?
      faraday.basic_auth(login, password)
    elsif bearer_token.present?
      faraday.authorization :Bearer, bearer_token
    elsif token_hash.present?
      faraday.authorization :Token, token_hash
    end

    faraday.adapter faraday_adapter
  end
end
connection_by_config_key(config_key, **others) click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 265
def connection_by_config_key(config_key, **others)
  connection config: ArtirixDataModels.configuration.send(config_key), **others
end
default_connection(**others) click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 261
def default_connection(**others)
  connection_by_config_key :data_gateway, **others
end