class Protocol::HTTP::Middleware::Builder

A convenient interface for constructing middleware stacks.

def initialize(default_app = NotFound)

@parameter default_app [Object] The default application to use if no middleware is specified.

Initialize the builder with the given default application.
def initialize(default_app = NotFound)
	@use = []
	@app = default_app
end

def run(app)

@parameter app [Middleware] The application to use if no middleware is able to handle the request.

Specify the (default) middleware application to use.
def run(app)
	@app = app
end

def to_app

@returns [Middleware] The application.

Convert the builder to an application by chaining the middleware together.
def to_app
	@use.reverse.inject(@app) {|app, use| use.call(app)}
end

def use(middleware, *arguments, **options, &block)

@parameter block [Proc] The block to pass to the middleware constructor.
@parameter options [Hash] The options to pass to the middleware constructor.
@parameter arguments [Array] The arguments to pass to the middleware constructor.
@parameter middleware [Class | Object] The middleware class to use.

Use the given middleware with the given arguments and options.
def use(middleware, *arguments, **options, &block)
	@use << proc {|app| middleware.new(app, *arguments, **options, &block)}
end