Class: CoreLibrary::RackRequestHelper
- Inherits:
-
Object
- Object
- CoreLibrary::RackRequestHelper
- Defined in:
- lib/apimatic-core/utilities/rack_request_helper.rb
Overview
SignatureVerifierHelper ===
Utility class for extracting headers and body from Rack::Request (or request-like objects).
All methods are class methods and can be used without instantiating.
Class Method Summary collapse
-
.extract_headers_hash(request) ⇒ Hash{String => String}
Extract headers into a downcast-key Hash from Rack::Request or request-like object.
-
.prepend_header(name) ⇒ String
Normalizes a header name to the Rack environment variable format.
-
.read_raw_body(request) ⇒ String
Read raw body from a Rack::Request (rewinds after reading).
Class Method Details
.extract_headers_hash(request) ⇒ Hash{String => String}
Extract headers into a downcast-key Hash from Rack::Request or request-like object. Supports both Rack env (‘request.env`) and objects exposing `headers` Hash.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/apimatic-core/utilities/rack_request_helper.rb', line 13 def extract_headers_hash(request) env = if request.respond_to?(:env) request.env elsif request.respond_to?(:headers) && request.headers.is_a?(Hash) headers_to_env(request.headers) else {} end env.each_with_object({}) do |(k, v), acc| next if v.nil? key = k.to_s if key.start_with?('HTTP_') || %w[CONTENT_TYPE CONTENT_LENGTH].include?(key) header_name = format_header_name(key) acc[header_name.downcase] = v.to_s end end end |
.prepend_header(name) ⇒ String
Normalizes a header name to the Rack environment variable format.
-
Converts “Content-Type” → “CONTENT_TYPE”
-
Converts “Content-Length” → “CONTENT_LENGTH”
-
Converts all other headers → “HTTP_<UPPERCASED_NAME>”
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/apimatic-core/utilities/rack_request_helper.rb', line 58 def prepend_header(name) k_s = name.to_s if /\Acontent[-_]type\z/i.match?(k_s) 'CONTENT_TYPE' elsif /\Acontent[-_]length\z/i.match?(k_s) 'CONTENT_LENGTH' else "HTTP_#{k_s.upcase.gsub('-', '_')}" end end |
.read_raw_body(request) ⇒ String
Read raw body from a Rack::Request (rewinds after reading). Falls back to ‘raw_body` if provided.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/apimatic-core/utilities/rack_request_helper.rb', line 38 def read_raw_body(request) if request.respond_to?(:body) && request.body.respond_to?(:read) body = request.body.read request.body.rewind if request.body.respond_to?(:rewind) body elsif request.respond_to?(:raw_body) request.raw_body.to_s else ''.dup end end |