Sha256: 3ae470e919d983122da9b7038ce8ee4f4899a3e9ed5473b44888d6b72db29a79

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require 'credit_gateway/configuration'
require 'credit_gateway/errors'
require 'credit_gateway/version'

module CreditGateway
  class Authenticate
    def call(client_id, client_secret)
      response = post(
        '/auth/connect/token',
        client_id: client_id,
        client_secret: client_secret,
        grant_type: 'client_credentials'
      )

      if response.success?
        response.body[:access_token]
      elsif response.status == 400
        raise InvalidRequestError, response.body
      else
        raise GenericError, response.body
      end
    end

    private

    def post(path, params = {})
      connection.post(path, params)
    end

    def connection
      auth_url = CreditGateway.configuration.auth_url

      @connection ||= Faraday.new(url: auth_url) do |conn|
        conn.request :url_encoded

        conn.headers.merge!(
          user_agent: "finpoint/#{CreditGateway::VERSION}"
        )
        conn.response :multi_json, symbolize_keys: true
        conn.response :logger if CreditGateway.configuration.debug
        conn.adapter Faraday.default_adapter
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
credit_gateway-0.3.0 lib/credit_gateway/authenticate.rb