Sha256: 4efde9621e2f0814a92dcc2cba7ce4aca0bdcf082c596774f583faa627375d42

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module LedgerSync
  module Adaptors
    class Request
      attr_reader :body,
                  :headers,
                  :method,
                  :response,
                  :url

      def initialize(body: nil, headers: {}, method: nil, url: nil)
        @body = body
        @headers = headers
        @method = method
        @url = url
      end

      def perform
        raise 'Request already performed' if performed?

        faraday_response = Faraday.send(method, url) do |req|
          req.headers = headers
          req.body = body.to_json unless body.nil?
        end

        @response = Response.new_from_faraday_response(faraday_response: faraday_response, request: self)
        @performed = true
        @response
      end

      def performed?
        @performed == true
      end

      def self.delete(**keywords)
        new(keywords.merge(method: :delete))
      end

      def self.get(**keywords)
        new(keywords.merge(method: :get))
      end

      def self.post(**keywords)
        new(keywords.merge(method: :post))
      end

      def self.put(**keywords)
        new(keywords.merge(method: :put))
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ledger_sync-1.3.5 lib/ledger_sync/adaptors/request.rb
ledger_sync-1.3.4 lib/ledger_sync/adaptors/request.rb
ledger_sync-1.3.3 lib/ledger_sync/adaptors/request.rb
ledger_sync-1.3.2 lib/ledger_sync/adaptors/request.rb
ledger_sync-1.3.1 lib/ledger_sync/adaptors/request.rb