Sha256: 9f6ab2a2e46c49153bdfbf62329e4379b05d07883d278e208813e774d6de8179

Contents?: true

Size: 884 Bytes

Versions: 3

Compression:

Stored size: 884 Bytes

Contents

# frozen_string_literal: true

require 'credit_gateway/authenticate'
require 'credit_gateway/version'
require 'credit_gateway/errors'

module CreditGateway
  class FaradayAuth < Faraday::Middleware
    def initialize(app, client_id, client_secret, country, options = {})
      super(app)
      @client_id = client_id
      @client_secret = client_secret
      @country = country
      @options = options
      @token = nil
    end

    def call(env)
      @token ||= authenticate

      env[:request_headers]['Authorization'] = "Bearer #{@token}"
      env[:request_headers]['X-Country'] = @country
      env[:request_headers]['Content-Type'] = 'application/json'
      env[:request_headers]['User-Agent'] = "finpoint/#{CreditGateway::VERSION}"

      @app.call(env)
    end

    private

    def authenticate
      Authenticate.new.call(@client_id, @client_secret)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
credit_gateway-0.3.2 lib/credit_gateway/faraday_auth.rb
credit_gateway-0.3.1 lib/credit_gateway/faraday_auth.rb
credit_gateway-0.3.0 lib/credit_gateway/faraday_auth.rb