# 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