Module: ArtirixDataModels::DataGateway::ConnectionLoader

Defined in:
lib/artirix_data_models/gateways/data_gateway.rb

Defined Under Namespace

Classes: InvalidConnectionError

Class Method Summary collapse

Class Method Details

.connection(config: {}, url: nil, login: nil, password: nil, bearer_token: nil, token_hash: nil) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 251

def connection(config: {}, url: nil, login: nil, password: nil, bearer_token: nil, token_hash: nil)
  url          ||= config.try :url
          ||= config.try :login
  password     ||= config.try :password
  bearer_token ||= config.try :bearer_token
  token_hash   ||= config.try :token_hash

  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|
    faraday.request :url_encoded # form-encode POST params
    faraday.response :logger # log requests to STDOUT

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

    faraday.adapter Faraday.default_adapter
  end
end

.connection_by_config_key(config_key, **others) ⇒ Object



247
248
249
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 247

def connection_by_config_key(config_key, **others)
  connection config: ArtirixDataModels.configuration.send(config_key), **others
end

.default_connection(**others) ⇒ Object



243
244
245
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 243

def default_connection(**others)
  connection_by_config_key :data_gateway, **others
end