Class: Footprint::Middleware

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

Overview

Class that is actually the Middleware we use to decorate the application with loggers, taking advantage of the @env.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) ⇒ Middleware

Synopsis

Basic Middleware usage:

use Footprint::Middleware

Advanced Middleware usage:

use Footprint::Middleware do
  set Logger, STDOUT
end

Clazz init:

Footprint::Middleware.new app # => #<Footprint::Middleware>

Args

app

Rack application instance.

block

Optional block, used to instance_eval (defaults to nil)

Description

Initialize the Middleware with the app that uses this Middleware and an optional block.

Sets as the default logger a Footprint::Log::Basic on STDOUT.

Decorate the given app with a new method called logger, that will return the instance of the logger from the env.

If any block is given, a instance_eval is called.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/footprint/middleware/logger.rb', line 55

def initialize(app, &block)

  @app = app

  set Footprint::Log::Basic, STDOUT

  @app.class.send(:define_method, :logger,
                  Proc.new do
                    @env[:footprint_logger]
                  end)

  self.instance_eval &block if block
end

Instance Attribute Details

#appObject

The instance of the Rack app that uses this middleware.



12
13
14
# File 'lib/footprint/middleware/logger.rb', line 12

def app
  @app
end

#loggerObject

The instance of the logger used to decorate the Rack application.



15
16
17
# File 'lib/footprint/middleware/logger.rb', line 15

def logger
  @logger
end

Instance Method Details

#call(env) ⇒ Object

Synopsis

Footprint:Middleware.call env # => env

Args

env

Rack request environment.

Return

env returned always.

Description

The current instance of the logger is set on env and the app is called further with the enriched env.



88
89
90
91
# File 'lib/footprint/middleware/logger.rb', line 88

def call(env)
  env[:footprint_logger] = @logger
  @app.call env
end

#set(clazz, *args) ⇒ Object

Synopsis

Basic usage:

Footprint.Middleware.set Logger, STDOUT # => logger

Args

clazz

Any Logger class that has the default Logger methods.

args

Optional array of arguments passed to the Logger class for initialize (defaults to nil)

Return

logger returned always.

Description

Initialize the current instance of the logger with a new instance of the class given as parameter.

The initialization of the class given is done using the second parameter.



120
121
122
# File 'lib/footprint/middleware/logger.rb', line 120

def set clazz, *args
  @logger = clazz.send(:new, *args)
end