Class: Sparrow::HttpMessage Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/sparrow/http_message.rb

Overview

This class is abstract.

RequestHttpMessage and ResponseHttpMessage should be used in practice

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.

Direct Known Subclasses

RequestHttpMessage, ResponseHttpMessage

Constant Summary collapse

FORM_HASH_KEY =

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

'rack.request.form_hash'
RACK_INPUT_KEY =

The rack environment hash key to access the input/output

'rack.input'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ HttpMessage

Initializes the HttpMessage

Parameters:

  • env (Hash)

    The Rack environment



22
23
24
# File 'lib/sparrow/http_message.rb', line 22

def initialize(env)
  @env = env
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Delegates all unknown method calls to the wrapped request

See Also:



68
69
70
# File 'lib/sparrow/http_message.rb', line 68

def method_missing(method_name, *args)
  request.public_send(method_name, *args)
end

Instance Attribute Details

#envHash (readonly)

Returns the Rack environment.

Returns:

  • (Hash)

    the Rack environment

See Also:



17
18
19
# File 'lib/sparrow/http_message.rb', line 17

def env
  @env
end

Instance Method Details

#acceptObject

The HTTP Accept Header field

Returns:

  • String the HTTP Accept Header value



54
55
56
# File 'lib/sparrow/http_message.rb', line 54

def accept
  http_header(:accept)
end

#content_typeObject

The HTTP Content Type Field

Returns:

  • String the HTTP Content-Type Header value



61
62
63
# File 'lib/sparrow/http_message.rb', line 61

def content_type
  http_header(:content_type)
end

#form_hash?Boolean

Returns true any values is insides the FORM_HASH_KEY of the rack environment.

Returns:

  • (Boolean)

    true any values is insides the FORM_HASH_KEY of the rack environment

See Also:

  • FORM_HASH_KEY
  • #env


41
42
43
# File 'lib/sparrow/http_message.rb', line 41

def form_hash?
  env[FORM_HASH_KEY].present?
end

#pathString

Requested path within this HTTP message

Returns:

  • (String)

    the path



47
48
49
# File 'lib/sparrow/http_message.rb', line 47

def path
  http_header(:path_info)
end

#requestObject

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.

Returns:

  • (Object)

See Also:



33
34
35
# File 'lib/sparrow/http_message.rb', line 33

def request
  @request ||= request_class.new(env)
end