Class: Simpal::Middleware::Headers

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/simpal/middleware/headers.rb

Overview

Allows the use of convenience header names which are then mapped into the real header name.

Constant Summary collapse

PAYPAL_HEADERS =

Returns The request headers which must have the ‘PayPal-’ prefix applied to them.

Returns:

  • (Array)

    The request headers which must have the ‘PayPal-’ prefix applied to them.

%w[
  Request-Id
  Client-Metadata-Id
  Partner-Attribution-Id
  Auth-Assertion
].freeze

Instance Method Summary collapse

Instance Method Details

#on_request(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/simpal/middleware/headers.rb', line 17

def on_request(env)
  headers = env[:request_headers]
  return unless headers.is_a?(Hash)

  # Prefer full representations, instead of the minimal response by default.
  headers['Prefer'] = 'return=representation' unless headers.key?('Prefer')

  PAYPAL_HEADERS.each do |header|
    headers["PayPal-#{header}"] = headers.delete(header) if headers.key?(header)
  end

  env[:request_headers] = headers
end