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