class Roda::RodaPlugins::Middleware::Forwarder

  1. lib/roda/plugins/middleware.rb
Superclass: Object

Forwarder instances are what is actually used as middleware.

Methods

Public Class

  1. new

Public Instance

  1. call

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
136 def initialize(mid, app, *args, &block)
137   @mid = Class.new(mid)
138   RodaPlugins.set_temp_name(@mid){"#{mid}::middleware_subclass"}
139   if @mid.opts[:middleware_next_if_not_found]
140     @mid.plugin(:not_found, &NEXT_PROC)
141   end
142   if configure = @mid.opts[:middleware_configure]
143     configure.call(@mid, *args, &block)
144   elsif block || !args.empty?
145     raise RodaError, "cannot provide middleware args or block unless loading middleware plugin with a block"
146   end
147   @app = app
148 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
153 def call(env)
154   res = nil
155 
156   call_next = catch(:next) do
157     env[@mid.opts[:middleware_env_var]] = true
158     res = @mid.call(env)
159     false
160   end
161 
162   if call_next
163     res = @app.call(env)
164 
165     if modified_headers = env.delete('roda.response_headers')
166       res[1] = modified_headers.merge(res[1])
167     end
168   end
169 
170   if handle_result = @mid.opts[:middleware_handle_result]
171     handle_result.call(env, res)
172   end
173 
174   res
175 end