Class: Deas::TrailingSlashes

Inherits:
Object
  • Object
show all
Defined in:
lib/deas/trailing_slashes.rb

Defined Under Namespace

Modules: AllowHandler, RequireNoHandler

Constant Summary collapse

HANDLERS =
{
  Deas::Router::REMOVE_TRAILING_SLASHES => RequireNoHandler,
  Deas::Router::ALLOW_TRAILING_SLASHES  => AllowHandler
}

Instance Method Summary collapse

Constructor Details

#initialize(app, router) ⇒ TrailingSlashes

Returns a new instance of TrailingSlashes.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/deas/trailing_slashes.rb', line 18

def initialize(app, router)
  @app    = app
  @router = router

  if !@router.trailing_slashes_set?
    val  = @router.trailing_slashes
    desc = val.nil? ? 'no' : "an invalid (`#{val.inspect}`)"
    raise ArgumentError, "TrailingSlashes middleware is in use but there is "\
                         "#{desc} trailing slashes router directive set."
  end
end

Instance Method Details

#call(env) ⇒ Object

The Rack call interface. The receiver acts as a prototype and runs each request in a clone object unless the rack.run_once variable is set in the environment. Ripped from: github.com/rtomayko/rack-cache/blob/master/lib/rack/cache/context.rb



34
35
36
37
38
39
40
# File 'lib/deas/trailing_slashes.rb', line 34

def call(env)
  if env['rack.run_once']
    call! env
  else
    clone.call! env
  end
end

#call!(env) ⇒ Object

The real Rack call interface.



43
44
45
# File 'lib/deas/trailing_slashes.rb', line 43

def call!(env)
  HANDLERS[@router.trailing_slashes].run(env){ @app.call(env) }
end