Class: Rack::ReplaceableResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/replaceable_response.rb

Overview

Middleware that allows setting a predefined response for a request. If response is set, it will be returned instead of handing env on to the next middleware/endpoint. Response is stored in env, so it will be eaten by the GC as soon as possible.

Examples:

class Wrapper < Wrapped
  def call(env)
    @app.response_for(env, some_response) if some_condition
    super
  end
end

use Wrapper
use ReplaceableResponse
run SomeApp

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ReplaceableResponse

Returns a new instance of ReplaceableResponse.



20
# File 'lib/rack/replaceable_response.rb', line 20

def initialize(app)                 @app = app                                      end

Instance Method Details

#call(env) ⇒ Object



22
# File 'lib/rack/replaceable_response.rb', line 22

def call(env)                       response_for(env) or @app.call(env)             end

#response_for(env, result = nil) ⇒ Object Also known as: set_response_for



21
# File 'lib/rack/replaceable_response.rb', line 21

def response_for(env, result = nil) (env['cached.responses'] ||= {})[self] = result end