Class: Sparrow::Strategies::FormHash

Inherits:
Object
  • Object
show all
Includes:
Transformable
Defined in:
lib/sparrow/strategies/form_hash.rb

Overview

Handles Form Hash transformations, i.e. HTTP messages that contain form data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transformable

#json_body, #transform_params

Constructor Details

#initialize(env, type = :request, params = nil) ⇒ FormHash

Create a new FormHash Strategy

Parameters:

  • env (Hash)

    the Rack environment

  • type (Symbol) (defaults to: :request)

    the HTTP message type. Must be either :request or :response

  • params (Hash) (defaults to: nil)

    the HTTP message parameters to be transformed if not already present in the env



28
29
30
31
32
# File 'lib/sparrow/strategies/form_hash.rb', line 28

def initialize(env, type = :request, params = nil)
  @env    = env
  @params = params
  @type   = type
end

Instance Attribute Details

#envHash (readonly)

Returns the Rack environment.

Returns:

  • (Hash)

    the Rack environment

See Also:



11
12
13
# File 'lib/sparrow/strategies/form_hash.rb', line 11

def env
  @env
end

#typeSymbol (readonly)

Returns the HTTP message type. Either :request, or :response.

Returns:

  • (Symbol)

    the HTTP message type. Either :request, or :response

See Also:



14
15
16
# File 'lib/sparrow/strategies/form_hash.rb', line 14

def type
  @type
end

Class Method Details

.handle(env, type) ⇒ Object

Shortcut for handling a HTTP Message with this strategy.



18
19
20
# File 'lib/sparrow/strategies/form_hash.rb', line 18

def self.handle(env, type)
  self.new(env, type).handle
end

Instance Method Details

#handleHash

Do the transformation

Returns:

  • (Hash)

    the converted JSON as a Hash representation



37
38
39
40
# File 'lib/sparrow/strategies/form_hash.rb', line 37

def handle
  super
  handle_form_hash
end

#paramsHash

If given in the constructor, this simply returns the given params argument. Otherwise it will look in the #env for form hash data params

Returns:

  • (Hash)

    the HTTP message params

See Also:



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

def params
  @params || env[HttpMessage::FORM_HASH_KEY]
end