class Sparrow::Strategies::RawInput

Handles plain JSON input parameter conversion which is sent via env

Attributes

env[R]

@return [Hash] the Rack environment @see initialize

type[R]

@return [Symbol] the HTTP message type @see initialize

Public Class Methods

handle(env, type) click to toggle source

Do the transformation @return [Hash] the converted JSON as a Hash representation

# File lib/sparrow/strategies/raw_input.rb, line 19
def self.handle(env, type)
  self.new(env, type).handle
end
new(env, type = :request, params = nil) click to toggle source

Create a new RawInput Strategy @param [Hash] env the Rack environment @param [Symbol] type the HTTP message type. Must be either :request or

:response

@param [Hash] params the HTTP message parameters to be transformed

if not already present in the env
# File lib/sparrow/strategies/raw_input.rb, line 29
def initialize(env, type = :request, params = nil)
  @env    = env || {}
  @params = params
  @type   = type
end

Public Instance Methods

handle() click to toggle source

Starts the conversion @return [Hash] the converted Rack environment

Calls superclass method Sparrow::Transformable#handle
# File lib/sparrow/strategies/raw_input.rb, line 38
def handle
  super
  handle_raw_rack
end
params() click to toggle source

The parameters to be transformed @return [Hash] the JSON parameters @see initialize

# 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

Private Instance Methods

handle_raw_rack() click to toggle source
# File lib/sparrow/strategies/raw_input.rb, line 60
def handle_raw_rack
  if params.present?
    new_raw_input        = json_body.force_encoding("BINARY")
    @env[HttpMessage::RACK_INPUT_KEY] = StringIO.new(new_raw_input)
    @env[HttpMessage::RACK_INPUT_KEY].rewind
    @env['CONTENT_LENGTH'] = new_raw_input.length
  end
end