Sha256: 8440d56ca782c12baba14b7c58fd97735c8819f63da879696a6650809b6d374d

Contents?: true

Size: 983 Bytes

Versions: 4

Compression:

Stored size: 983 Bytes

Contents

require 'httparty'

module PostmanMta
  class ApiRequest
    include ::HTTParty
    base_uri PostmanMta.api_endpoint

    attr_reader :request_type, :path, :options, :callback

    def initialize(request_type, path, options = {})
      @callback = PostmanMta.before_request_hook

      @request_type = request_type
      @path = path
      @options = options
    end

    def perform
      self.class.send(request_type.downcase, path, request_options)
    end

    private

    def request_options
      { headers: auth_headers, format: :json }.merge(merge_with_custom_options)
    end

    def auth_headers
      PostmanMta::Utils::SignedRequest.new(request_method: request_type.upcase, path: path).headers
    end

    def merge_with_custom_options
      return options unless callback

      custom_options = callback.call
      return options unless custom_options.is_a?(Hash)

      options[:body] = (options[:body] || {}).merge(custom_options)

      options
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
postman_mta-0.1.7 lib/postman_mta/api_request.rb
postman_mta-0.1.6 lib/postman_mta/api_request.rb
postman_mta-0.1.5 lib/postman_mta/api_request.rb
postman_mta-0.1.4 lib/postman_mta/api_request.rb