Sha256: 841ffc3a747f5f596418568baf298ead1d0fd7dfa3b2a6127b4ca5330d505669

Contents?: true

Size: 798 Bytes

Versions: 2

Compression:

Stored size: 798 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, options = {})
      super(app)
      @client_id = client_id
      @client_secret = client_secret
      @options = options
      @token = nil
    end

    def call(env)
      @token ||= authenticate

      env[:request_headers]['Authorization'] = "Bearer #{@token}"
      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

2 entries across 2 versions & 1 rubygems

Version Path
credit_gateway-0.1.1 lib/credit_gateway/faraday_auth.rb
credit_gateway-0.1.0 lib/credit_gateway/faraday_auth.rb