Class: Roda::RodaPlugins::Middleware::Forwarder

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

Overview

Forward instances are what is actually used as middleware.

Instance Method Summary collapse

Constructor Details

#initialize(mid, app) ⇒ Forwarder

Store the current middleware and the next middleware to call.



42
43
44
45
# File 'lib/roda/plugins/middleware.rb', line 42

def initialize(mid, app)
  @mid = mid
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

When calling the middleware, first call the current middleware. If this returns a result, return that result directly. Otherwise, pass handling of the request to the next middleware.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/roda/plugins/middleware.rb', line 50

def call(env)
  res = nil

  call_next = catch(:next) do
    scope = @mid.new(env)
    scope.request.forward_next = true
    res = scope.call(&@mid.route_block)
    false
  end

  if call_next
    @app.call(env)
  else
    res
  end
end