5
5
6
6
module Protocol
7
7
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.
9
25
class Methods
26
+ # The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.
10
27
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.
11
33
POST = 'POST'
34
+
35
+ # The PUT method replaces all current representations of the target resource with the request payload.
12
36
PUT = 'PUT'
13
- PATCH = 'PATCH'
37
+
38
+ # The DELETE method deletes the specified resource.
14
39
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.
16
45
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.
19
48
TRACE = 'TRACE'
20
- CONNECT = 'CONNECT'
49
+
50
+ # The PATCH method applies partial modifications to a resource.
51
+ PATCH = 'PATCH'
21
52
22
53
def self . valid? ( name )
23
54
const_defined? ( name )
@@ -26,13 +57,18 @@ def self.valid?(name)
26
57
return false
27
58
end
28
59
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"`.
29
64
def self . each
65
+ return to_enum ( :each ) unless block_given?
66
+
30
67
constants . each do |name |
31
68
yield name , const_get ( name )
32
69
end
33
70
end
34
71
35
- # Use Methods.constants to get all constants.
36
72
self . each do |name , value |
37
73
define_method ( name . downcase ) do |location , headers = nil , body = nil |
38
74
self . call (
0 commit comments