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