class Sparrow::Strategies::Ignore

This strategy is called when the HTTP message shall not be transformed, i.e. ignored. This is inspired by the NullObject-Pattern to be convenient.

Public Class Methods

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

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

:response

@param [Hash] params The HTTP message params if not given in the env

# File lib/sparrow/strategies/ignore.rb, line 16
def initialize(env, type = :request, params = nil)
  @env    = env
  @params = params
  @type   = type
end

Public Instance Methods

handle() click to toggle source

handles the conversion, i.e. here “do nothing” Which is not strictly true - at write the rack.input to the form hash key for convenience reasons to enable further middlewares to work with it @return [Hash] the rack env

# File lib/sparrow/strategies/ignore.rb, line 44
def handle
  # synchronize rack.input and form hash values
  input = @env[HttpMessage::RACK_INPUT_KEY].gets

  begin
    @env[HttpMessage::FORM_HASH_KEY] = JSON.parse(input)
  rescue JSON::ParserError
    # ignore
  ensure
    @env[HttpMessage::RACK_INPUT_KEY].rewind
  end if input.present?

  @env
end
json_body() click to toggle source

Alias for params @see params

# File lib/sparrow/strategies/ignore.rb, line 62
def json_body
  params
end
params() click to toggle source

Although we are ignoring any kind of conversion we still need to read the parameters from the environment to be convenient with all other calls in the chains and architecture sigh Checks env @return [Hash] the params

# File lib/sparrow/strategies/ignore.rb, line 28
def params
  ret = @params || @env[HttpMessage::RACK_INPUT_KEY].send(:read)
  @env[HttpMessage::RACK_INPUT_KEY].rewind
  ret
end
transform_params() click to toggle source

Transforms the params to a Ruby JSON Hash representation @return [Hash] the JSON

# File lib/sparrow/strategies/ignore.rb, line 69
def transform_params
  ensure_json
end