Class: Roda::RodaPlugins::Middleware::Forwarder
- Inherits:
-
Object
- Object
- Roda::RodaPlugins::Middleware::Forwarder
- Defined in:
- lib/roda/plugins/middleware.rb
Overview
Forwarder instances are what is actually used as middleware.
Instance Method Summary collapse
-
#call(env) ⇒ Object
When calling the middleware, first call the current middleware.
-
#initialize(mid, app, *args, &block) ⇒ Forwarder
constructor
Make a subclass of
mid
to use as the current middleware, and storeapp
as the next middleware to call.
Constructor Details
#initialize(mid, app, *args, &block) ⇒ Forwarder
Make a subclass of mid
to use as the current middleware, and store app
as the next middleware to call.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/roda/plugins/middleware.rb', line 135 def initialize(mid, app, *args, &block) @mid = Class.new(mid) # :nocov: @mid.set_temporary_name("#{mid.name}(middleware)") if mid.name && RUBY_VERSION >= "3.3" # :nocov: if @mid.opts[:middleware_next_if_not_found] @mid.plugin(:not_found, &NEXT_PROC) end if configure = @mid.opts[:middleware_configure] configure.call(@mid, *args, &block) elsif block || !args.empty? raise RodaError, "cannot provide middleware args or block unless loading middleware plugin with a block" end @app = app end |
Instance Method Details
#call(env) ⇒ Object
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.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/roda/plugins/middleware.rb', line 154 def call(env) res = nil call_next = catch(:next) do env[@mid.opts[:middleware_env_var]] = true res = @mid.call(env) false end if call_next res = @app.call(env) if modified_headers = env.delete('roda.response_headers') res[1] = modified_headers.merge(res[1]) end end if handle_result = @mid.opts[:middleware_handle_result] handle_result.call(env, res) end res end |