Class: Langis::Middleware::EnvFieldTransform

Inherits:
Object
  • Object
show all
Defined in:
lib/langis/middleware.rb

Overview

Middleware class that modifies the Rackish input environment by transforming a specific value in the environment before passing it along to the rest of the Rackish application chain.

Some useful applications of this transform:

  • Serialization or Deserialization of input data.

  • Filter or modify the message; to explicitly whitelist the list of properties in the message that may be exposed to a service or third party.

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ EnvFieldTransform

Returns a new instance of EnvFieldTransform.

Parameters:

  • app

    The Rackish Application for which this instance is acting as middleware.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :key (String) — default: Langis::MESSAGE_KEY

    The hash key of the Rackish environment whose value is the object that we want to transform (transformation object).

  • :to_method (Symbol, String) — default: :to_json

    The transformation object that will respond to the invokation of this method name. The return value of that method will replace the original transformation object in the Rackish environment as the environment is passed on to the rest of the Rackish app chain.

  • :to_args (Array, Object) — default: []

    The parameter or list of parameters to pass to the transformation method.



30
31
32
33
34
35
36
# File 'lib/langis/middleware.rb', line 30

def initialize(app, options={})
  @app = app
  @to_method = options[:to_method] || :to_json
  @to_args = options[:to_args] || []
  @to_args = [@to_args] unless @to_args.is_a? Array
  @key = options[:key] || MESSAGE_KEY
end

Instance Method Details

#call(env) ⇒ Array<Integer,Hash,#each>

Executes the object transformation, and invokes the rest of the Rackish app chain.

Parameters:

  • env (Hash)

    The input Rackish Environment.

Returns:

  • (Array<Integer,Hash,#each>)

    The return of the proxied Rackish application chain.



45
46
47
48
# File 'lib/langis/middleware.rb', line 45

def call(env)
  item = env[@key].send @to_method, *@to_args
  return @app.call env.merge({ @key => item })
end