Class: Sparrow::Strategies::Ignore

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

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new IgnoreStrategy

Parameters:

  • env (Hash)

    the Rack environment

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

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

  • params (Hash) (defaults to: nil)

    The HTTP message params if not given in the env



16
17
18
19
20
# File 'lib/sparrow/strategies/ignore.rb', line 16

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

Class Method Details

.handle(env, type) ⇒ Object



34
35
36
# File 'lib/sparrow/strategies/ignore.rb', line 34

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

Instance Method Details

#handleHash

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

Returns:

  • (Hash)

    the rack env



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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_bodyObject

Alias for #params

See Also:



62
63
64
# File 'lib/sparrow/strategies/ignore.rb', line 62

def json_body
  params
end

#paramsHash

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

Returns:

  • (Hash)

    the params



28
29
30
31
32
# 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_paramsHash

Transforms the params to a Ruby JSON Hash representation

Returns:

  • (Hash)

    the JSON



69
70
71
# File 'lib/sparrow/strategies/ignore.rb', line 69

def transform_params
  ensure_json
end