Sha256: c8800fd47847dacaeb91721193a0bfad3c599ae809bf3780056d26b3b83bfaf5

Contents?: true

Size: 950 Bytes

Versions: 2

Compression:

Stored size: 950 Bytes

Contents

# frozen_string_literal: true

require 'credit_gateway/errors'
require 'credit_gateway/client'

module CreditGateway
  class BaseRepository
    def initialize(client: nil)
      @client = client || Client.new
    end

    private

    attr_reader :client

    def get(path, params: {}, identity: nil)
      headers = identity ? identity.to_headers : {}
      result = client.get(path.strip, headers: headers, params: params)

      raise APIKeyError, result.body[:error] if result.status == 401

      result
    end

    def post(path, params: {}, identity: nil)
      headers = identity ? identity.to_headers : {}
      result = client.post(path.strip, headers: headers, params: params)

      raise APIKeyError, result.body[:error] if result.status == 401

      result
    end

    def format_url(url, params)
      formatted = url.dup.strip

      params.each { |key, value| formatted.sub!(":#{key}", value) }

      formatted
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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