class Sparrow::HttpMessage

Wrapper class for either a ::ActiveDispatch::Request or ::Rack::Request instance for the given rack environment. The wrapped class is determined based on the presence of the Rails constant. @abstract RequestHttpMessage and ResponseHttpMessage should be used in

practice

Constants

FORM_HASH_KEY

The rack environment hash key that determines if it is a form hash request

RACK_INPUT_KEY

The rack environment hash key to access the input/output

Attributes

env[R]

@return [Hash] the Rack environment @see initialize

Public Class Methods

new(env) click to toggle source

Initializes the HttpMessage @param [Hash] env The Rack environment

# File lib/sparrow/http_message.rb, line 22
def initialize(env)
  @env = env
end

Public Instance Methods

accept() click to toggle source

The HTTP Accept Header field @return String the HTTP Accept Header value

# File lib/sparrow/http_message.rb, line 54
def accept
  http_header(:accept)
end
content_type() click to toggle source

The HTTP Content Type Field @return String the HTTP Content-Type Header value

# File lib/sparrow/http_message.rb, line 61
def content_type
  http_header(:content_type)
end
form_hash?() click to toggle source

@return [Boolean] true any values is insides the FORM_HASH_KEY of the

rack environment

@see ::FORM_HASH_KEY @see env

# File lib/sparrow/http_message.rb, line 41
def form_hash?
  env[FORM_HASH_KEY].present?
end
method_missing(method_name, *args) click to toggle source

Delegates all unknown method calls to the wrapped request @see request

# File lib/sparrow/http_message.rb, line 68
def method_missing(method_name, *args)
  request.public_send(method_name, *args)
end
path() click to toggle source

Requested path within this HTTP message @return [String] the path

# File lib/sparrow/http_message.rb, line 47
def path
  http_header(:path_info)
end
request() click to toggle source

Depending on the environment this attribute may either be a [::ActionDispatch::Request], when running in a Rails environment, or a [::Rack::Request] otherwise Encapsulates the Rack env. @see env @return [Object]

# File lib/sparrow/http_message.rb, line 33
def request
  @request ||= request_class.new(env)
end

Private Instance Methods

http_header(key) click to toggle source

Make sure to use any appropriate format of common HTTP Header key syntax for the given key

# File lib/sparrow/http_message.rb, line 85
def http_header(key)
  key        = key.to_s
  header_key = [
      key,
      key.upcase,
      key.upcase.dasherize,
      key.humanize,
      key.dasherize,
      key.parameterize,
      key.underscore.split('_').map(&:humanize).join('-')
  ].detect do |transformed_key|
    headers_hash[transformed_key]
  end

  return nil unless header_key
  headers_hash[header_key].to_s.split(';').first
end
request_class() click to toggle source
# File lib/sparrow/http_message.rb, line 74
def request_class
  if defined?(Rails)
    ::ActionDispatch::Request
  else
    ::Rack::Request
  end
end