class Builder
A convenient interface for constructing middleware stacks.
Definitions
def initialize(default_app = NotFound)
Initialize the builder with the given default application.
Signature
-
parameter
default_appObject The default application to use if no middleware is specified.
Implementation
def initialize(default_app = NotFound)
@use = []
@app = default_app
end
def use(middleware, *arguments, **options, &block)
Use the given middleware with the given arguments and options.
Signature
-
parameter
middlewareClass | Object The middleware class to use.
-
parameter
argumentsArray The arguments to pass to the middleware constructor.
-
parameter
optionsHash The options to pass to the middleware constructor.
-
parameter
blockProc The block to pass to the middleware constructor.
Implementation
def use(middleware, *arguments, **options, &block)
@use << proc {|app| middleware.new(app, *arguments, **options, &block)}
end
def run(app)
Specify the (default) middleware application to use.
Signature
-
parameter
appMiddleware The application to use if no middleware is able to handle the request.
Implementation
def run(app)
@app = app
end
def to_app
Convert the builder to an application by chaining the middleware together.
Signature
-
returns
Middleware The application.
Implementation
def to_app
@use.reverse.inject(@app) {|app, use| use.call(app)}
end