Class: Protocol::HTTP::Middleware::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/http/middleware/builder.rb

Overview

A convenient interface for constructing middleware stacks.

Instance Method Summary collapse

Constructor Details

#initialize(default_app = NotFound) ⇒ Builder

Initialize the builder with the given default application.



16
17
18
19
# File 'lib/protocol/http/middleware/builder.rb', line 16

def initialize(default_app = NotFound)
	@use = []
	@app = default_app
end

Instance Method Details

#run(app) ⇒ Object

Specify the (default) middleware application to use.



34
35
36
# File 'lib/protocol/http/middleware/builder.rb', line 34

def run(app)
	@app = app
end

#to_appObject

Convert the builder to an application by chaining the middleware together.



41
42
43
# File 'lib/protocol/http/middleware/builder.rb', line 41

def to_app
	@use.reverse.inject(@app) {|app, use| use.call(app)}
end

#use(middleware, *arguments, **options, &block) ⇒ Object

Use the given middleware with the given arguments and options.



27
28
29
# File 'lib/protocol/http/middleware/builder.rb', line 27

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