Class: Sparrow::Strategies::RawInput

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

Overview

Handles plain JSON input parameter conversion which is sent via env

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) ⇒ RawInput

Create a new RawInput 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



29
30
31
32
33
# File 'lib/sparrow/strategies/raw_input.rb', line 29

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/raw_input.rb', line 11

def env
  @env
end

#typeSymbol (readonly)

Returns the HTTP message type.

Returns:

  • (Symbol)

    the HTTP message type

See Also:



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

def type
  @type
end

Class Method Details

.handle(env, type) ⇒ Hash

Do the transformation

Returns:

  • (Hash)

    the converted JSON as a Hash representation



19
20
21
# File 'lib/sparrow/strategies/raw_input.rb', line 19

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

Instance Method Details

#handleHash

Starts the conversion

Returns:

  • (Hash)

    the converted Rack environment



38
39
40
41
# File 'lib/sparrow/strategies/raw_input.rb', line 38

def handle
  super
  handle_raw_rack
end

#paramsHash

The parameters to be transformed

Returns:

  • (Hash)

    the JSON parameters

See Also:



47
48
49
50
51
52
53
54
55
56
# File 'lib/sparrow/strategies/raw_input.rb', line 47

def params
  if @params
    @params
  else
    input_io = @env[HttpMessage::RACK_INPUT_KEY]
    params   = input_io.send(:read)
    input_io.rewind
    params
  end
end