Skip to content

Commit d3c27a4

Browse files
committed
Improved documentation for Protocol::HTTP::Middleware.
1 parent 674da27 commit d3c27a4

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lib/protocol/http/middleware.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/protocol/http/middleware/builder.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,42 @@
88
module Protocol
99
module HTTP
1010
class Middleware
11+
# A convenient interface for constructing middleware stacks.
1112
class Builder
13+
# Initialize the builder with the given default application.
14+
#
15+
# @parameter default_app [Object] The default application to use if no middleware is specified.
1216
def initialize(default_app = NotFound)
1317
@use = []
1418
@app = default_app
1519
end
1620

21+
# Use the given middleware with the given arguments and options.
22+
#
23+
# @parameter middleware [Class | Object] The middleware class to use.
24+
# @parameter arguments [Array] The arguments to pass to the middleware constructor.
25+
# @parameter options [Hash] The options to pass to the middleware constructor.
26+
# @parameter block [Proc] The block to pass to the middleware constructor.
1727
def use(middleware, *arguments, **options, &block)
1828
@use << proc {|app| middleware.new(app, *arguments, **options, &block)}
1929
end
2030

31+
# Specify the (default) middleware application to use.
32+
#
33+
# @parameter app [Middleware] The application to use if no middleware is able to handle the request.
2134
def run(app)
2235
@app = app
2336
end
2437

38+
# Convert the builder to an application by chaining the middleware together.
39+
#
40+
# @returns [Middleware] The application.
2541
def to_app
2642
@use.reverse.inject(@app) {|app, use| use.call(app)}
2743
end
2844
end
2945

46+
# Build a middleware application using the given block.
3047
def self.build(&block)
3148
builder = Builder.new
3249

0 commit comments

Comments
 (0)