lib/roda/plugins/middleware.rb in roda-3.9.0 vs lib/roda/plugins/middleware.rb in roda-3.10.0
- old
+ new
@@ -67,14 +67,19 @@
# Configure the middleware plugin. Options:
# :env_var :: Set the environment variable to use to indicate to the roda
# application that the current request is a middleware request.
# You should only need to override this if you are using multiple
# roda middleware in the same application.
+ # :handle_result :: Callable object that will be called with request environment
+ # and rack response for all requests passing through the middleware,
+ # after either the middleware or next app handles the request
+ # and returns a response.
def self.configure(app, opts={}, &block)
app.opts[:middleware_env_var] = opts[:env_var] if opts.has_key?(:env_var)
app.opts[:middleware_env_var] ||= 'roda.forward_next'
app.opts[:middleware_configure] = block if block
+ app.opts[:middleware_handle_result] = opts[:handle_result]
end
# Forwarder instances are what is actually used as middleware.
class Forwarder
# Make a subclass of +mid+ to use as the current middleware,
@@ -100,13 +105,17 @@
res = @mid.call(env)
false
end
if call_next
- @app.call(env)
- else
- res
+ res = @app.call(env)
end
+
+ if handle_result = @mid.opts[:middleware_handle_result]
+ handle_result.call(env, res)
+ end
+
+ res
end
end
module ClassMethods
# Create a Forwarder instead of a new instance if a non-Hash is given.