Skip to content

Commit b19f3b7

Browse files
authored
Improve documentation for Methods and Middleware. (#34)
1 parent 075c3d0 commit b19f3b7

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

lib/protocol/http/methods.rb

+43-7
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,50 @@
55

66
module Protocol
77
module HTTP
8-
# All supported HTTP methods
8+
# Provides a convenient interface for commonly supported HTTP methods.
9+
#
10+
# | Method Name | Request Body | Response Body | Safe | Idempotent | Cacheable |
11+
# | ----------- | ------------ | ------------- | ---- | ---------- | --------- |
12+
# | GET | Optional | Yes | Yes | Yes | Yes |
13+
# | HEAD | Optional | No | Yes | Yes | Yes |
14+
# | POST | Yes | Yes | No | No | Yes |
15+
# | PUT | Yes | Yes | No | Yes | No |
16+
# | DELETE | Optional | Yes | No | Yes | No |
17+
# | CONNECT | Optional | Yes | No | No | No |
18+
# | OPTIONS | Optional | Yes | Yes | Yes | No |
19+
# | TRACE | No | Yes | Yes | Yes | No |
20+
# | PATCH | Yes | Yes | No | No | No |
21+
#
22+
# These methods are defined in this module using lower case names. They are for convenience only and you should not overload those methods.
23+
#
24+
# See <https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods> for more details.
925
class Methods
26+
# The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.
1027
GET = 'GET'
28+
29+
# The HEAD method asks for a response identical to a GET request, but without the response body.
30+
HEAD = 'HEAD'
31+
32+
# The POST method submits an entity to the specified resource, often causing a change in state or side effects on the server.
1133
POST = 'POST'
34+
35+
# The PUT method replaces all current representations of the target resource with the request payload.
1236
PUT = 'PUT'
13-
PATCH = 'PATCH'
37+
38+
# The DELETE method deletes the specified resource.
1439
DELETE = 'DELETE'
15-
HEAD = 'HEAD'
40+
41+
# The CONNECT method establishes a tunnel to the server identified by the target resource.
42+
CONNECT = 'CONNECT'
43+
44+
# The OPTIONS method describes the communication options for the target resource.
1645
OPTIONS = 'OPTIONS'
17-
LINK = 'LINK'
18-
UNLINK = 'UNLINK'
46+
47+
# The TRACE method performs a message loop-back test along the path to the target resource.
1948
TRACE = 'TRACE'
20-
CONNECT = 'CONNECT'
49+
50+
# The PATCH method applies partial modifications to a resource.
51+
PATCH = 'PATCH'
2152

2253
def self.valid?(name)
2354
const_defined?(name)
@@ -26,13 +57,18 @@ def self.valid?(name)
2657
return false
2758
end
2859

60+
# Enumerate all HTTP methods.
61+
# @yields {|name, value| ...}
62+
# @parameter name [Symbol] The name of the method, e.g. `:GET`.
63+
# @parameter value [String] The value of the method, e.g. `"GET"`.
2964
def self.each
65+
return to_enum(:each) unless block_given?
66+
3067
constants.each do |name|
3168
yield name, const_get(name)
3269
end
3370
end
3471

35-
# Use Methods.constants to get all constants.
3672
self.each do |name, value|
3773
define_method(name.downcase) do |location, headers = nil, body = nil|
3874
self.call(

lib/protocol/http/middleware.rb

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010

1111
module Protocol
1212
module HTTP
13+
# The middleware interface provides a convenient wrapper for implementing HTTP middleware.
14+
#
15+
# A middleware instance generally needs to respond to two methods:
16+
#
17+
# - `call(request)` -> `response`
18+
# - `close()`
19+
#
20+
# The call method is called for each request. The close method is called when the server is shutting down.
21+
#
22+
# You do not need to use the Middleware class to implement middleware. You can implement the interface directly.
1323
class Middleware < Methods
1424
# Convert a block to a middleware delegate.
1525
def self.for(&block)

test/protocol/http/methods.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@
3131
it_behaves_like ValidMethod, "DELETE"
3232
it_behaves_like ValidMethod, "HEAD"
3333
it_behaves_like ValidMethod, "OPTIONS"
34-
it_behaves_like ValidMethod, "LINK"
35-
it_behaves_like ValidMethod, "UNLINK"
3634
it_behaves_like ValidMethod, "TRACE"
3735
it_behaves_like ValidMethod, "CONNECT"
3836

39-
it "defines exactly 11 methods" do
40-
expect(subject.constants.length).to be == 11
37+
it "defines exactly 9 methods" do
38+
expect(subject.constants.length).to be == 9
4139
end
4240

4341
with '.valid?' do

0 commit comments

Comments
 (0)