Forwarder
instances are what is actually used as middleware.
Public Class methods
new(mid, app, *args, &block)
Make a subclass of mid
to use as the current middleware, and store app
as the next middleware to call.
[show source]
# File lib/roda/plugins/middleware.rb 92 def initialize(mid, app, *args, &block) 93 @mid = Class.new(mid) 94 if configure = @mid.opts[:middleware_configure] 95 configure.call(@mid, *args, &block) 96 elsif block || !args.empty? 97 raise RodaError, "cannot provide middleware args or block unless loading middleware plugin with a block" 98 end 99 @app = app 100 end
Public Instance methods
call(env)
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.
[show source]
# File lib/roda/plugins/middleware.rb 105 def call(env) 106 res = nil 107 108 call_next = catch(:next) do 109 env[@mid.opts[:middleware_env_var]] = true 110 res = @mid.call(env) 111 false 112 end 113 114 if call_next 115 res = @app.call(env) 116 117 if modified_headers = env.delete('roda.response_headers') 118 res[1] = modified_headers.merge(res[1]) 119 end 120 end 121 122 if handle_result = @mid.opts[:middleware_handle_result] 123 handle_result.call(env, res) 124 end 125 126 res 127 end