Class: Protocol::HTTP::Middleware::Builder
- Inherits:
-
Object
- Object
- Protocol::HTTP::Middleware::Builder
- Defined in:
- lib/protocol/http/middleware/builder.rb
Overview
A convenient interface for constructing middleware stacks.
Instance Method Summary collapse
-
#initialize(default_app = NotFound) ⇒ Builder
constructor
Initialize the builder with the given default application.
-
#run(app) ⇒ Object
Specify the (default) middleware application to use.
-
#to_app ⇒ Object
Convert the builder to an application by chaining the middleware together.
-
#use(middleware, *arguments, **options, &block) ⇒ Object
Use the given middleware with the given arguments and options.
Constructor Details
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_app ⇒ Object
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, **, &block) @use << proc {|app| middleware.new(app, *arguments, **, &block)} end |