@@ -22,49 +22,77 @@ module HTTP
2222 # You do not need to use the Middleware class to implement middleware. You can implement the interface directly.
2323 class Middleware < Methods
2424 # Convert a block to a middleware delegate.
25+ #
26+ # @parameter block [Proc] The block to convert to a middleware delegate.
27+ # @returns [Middleware] The middleware delegate.
2528 def self . for ( &block )
29+ # Add a close method to the block.
2630 def block . close
2731 end
2832
2933 return self . new ( block )
3034 end
3135
36+ # Initialize the middleware with the given delegate.
37+ #
38+ # @parameter delegate [Object] The delegate object. A delegate is used for passing along requests that are not handled by *this* middleware.
3239 def initialize ( delegate )
3340 @delegate = delegate
3441 end
3542
43+ # @attribute [Object] The delegate object that is used for passing along requests that are not handled by *this* middleware.
3644 attr :delegate
3745
46+ # Close the middleware. Invokes the close method on the delegate.
3847 def close
3948 @delegate . close
4049 end
4150
51+ # Call the middleware with the given request. Invokes the call method on the delegate.
4252 def call ( request )
4353 @delegate . call ( request )
4454 end
4555
56+ # A simple middleware that always returns a 200 response.
4657 module Okay
58+ # Close the middleware - idempotent no-op.
4759 def self . close
4860 end
4961
62+ # Call the middleware with the given request, always returning a 200 response.
63+ #
64+ # @parameter request [Request] The request object.
65+ # @returns [Response] The response object, which always contains a 200 status code.
5066 def self . call ( request )
5167 Response [ 200 ]
5268 end
5369 end
5470
71+ # A simple middleware that always returns a 404 response.
5572 module NotFound
73+ # Close the middleware - idempotent no-op.
5674 def self . close
5775 end
5876
77+ # Call the middleware with the given request, always returning a 404 response. This middleware is useful as a default.
78+ #
79+ # @parameter request [Request] The request object.
80+ # @returns [Response] The response object, which always contains a 404 status code.
5981 def self . call ( request )
6082 Response [ 404 ]
6183 end
6284 end
6385
86+ # A simple middleware that always returns "Hello World!".
6487 module HelloWorld
88+ # Close the middleware - idempotent no-op.
6589 def self . close
6690 end
6791
92+ # Call the middleware with the given request.
93+ #
94+ # @parameter request [Request] The request object.
95+ # @returns [Response] The response object, whihc always contains "Hello World!".
6896 def self . call ( request )
6997 Response [ 200 , Headers [ "content-type" => "text/plain" ] , [ "Hello World!" ] ]
7098 end
0 commit comments