Sha256: 1427ef76b2e85352fd2f7d2c34424a9c20591dae70ca3f0d17056fa0ce627a01

Contents?: true

Size: 1.13 KB

Versions: 2

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(
        '/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

2 entries across 2 versions & 1 rubygems

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