Sha256: 872744ab727586fed40a334c260a367300e173e40c4e02516c49aa4f8960be67

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require 'faraday'

module Afterpay
  module HTTPService
    class Request
      # @attribute middleware
      # @return [Proc] A proc containing Faraday configuration
      attr_reader :middleware

      # @attribute server
      # @return [String] an API endpoint
      attr_reader :server

      # Creates Afterpay::HTTPService::Request instance
      # @param  [Hash] params hash of parameters
      # @return [Afterpay::HTTPService::Request] Request instance
      def initialize(params = {})
        @middleware = params[:middleware]
        @server     = params[:server]
      end

      # Performs an HTTP request to the specified endpoint with given body
      # @param  [Hash] params hash of parameters.
      # @return [Farday::Response] Faraday::Response instance
      def perform(params = {})
        adapter.send(*params.values_at(:action, :endpoint, :body))
      end

      private

      # Creates memoized Faraday::Connection instance
      # @return [Faraday::Connection] Faraday::Connection instance
      def adapter
        @adapter ||= Faraday.new(server, faraday_options, &middleware)
      end

      def faraday_options
        { "headers" => headers }
      end

      def headers
        { 'Content-Type' => 'application/json', 'User-Agent' => ::Afterpay.user_agent }.compact
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
afterpay-0.6.0 lib/afterpay/http_service/request.rb
afterpay-0.5.0 lib/afterpay/http_service/request.rb
afterpay-0.4.0 lib/afterpay/http_service/request.rb
afterpay-0.3.0 lib/afterpay/http_service/request.rb