Skip to content

Commit fdb8020

Browse files
committed
Improved documentation for Protocol::HTTP::Request.
1 parent d2d51d6 commit fdb8020

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

lib/protocol/http/request.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ module HTTP
2525
class Request
2626
prepend Body::Reader
2727

28+
# Initialize the request.
29+
#
30+
# @parameter scheme [String | Nil] The request scheme, usually `"http"` or `"https"`.
31+
# @parameter authority [String | Nil] The request authority, usually a hostname and port number, e.g. `"example.com:80"`.
32+
# @parameter method [String | Nil] The request method, usually one of `"GET"`, `"HEAD"`, `"POST"`, `"PUT"`, `"DELETE"`, `"CONNECT"` or `"OPTIONS"`, etc.
33+
# @parameter path [String | Nil] The request path, usually a path and query string, e.g. `"/index.html"`, `"/search?q=hello"`, etc.
34+
# @parameter version [String | Nil] The request version, usually `"http/1.0"`, `"http/1.1"`, `"h2"`, or `"h3"`.
35+
# @parameter headers [Headers] The request headers, usually containing metadata associated with the request such as the `"user-agent"`, `"accept"` (content type), `"accept-language"`, etc.
36+
# @parameter body [Body::Readable] The request body.
37+
# @parameter protocol [String | Array(String) | Nil] The request protocol, usually empty, but occasionally `"websocket"` or `"webtransport"`.
38+
# @parameter interim_response [Proc] A callback which is called when an interim response is received.
2839
def initialize(scheme = nil, authority = nil, method = nil, path = nil, version = nil, headers = Headers.new, body = nil, protocol = nil, interim_response = nil)
2940
@scheme = scheme
3041
@authority = authority
@@ -81,6 +92,11 @@ def send_interim_response(status, headers)
8192
@interim_response&.call(status, headers)
8293
end
8394

95+
# Register a callback to be called when an interim response is received.
96+
#
97+
# @yields {|status, headers| ...} The callback to be called when an interim response is received.
98+
# @parameter status [Integer] The HTTP status code, e.g. `100`, `101`, etc.
99+
# @parameter headers [Hash] The headers, e.g. `{"link" => "</style.css>; rel=stylesheet"}`, etc.
84100
def on_interim_response(&block)
85101
if interim_response = @interim_response
86102
@interim_response = ->(status, headers) do
@@ -120,6 +136,9 @@ def idempotent?
120136
@method != Methods::POST && (@body.nil? || @body.empty?)
121137
end
122138

139+
# Convert the request to a hash, suitable for serialization.
140+
#
141+
# @returns [Hash] The request as a hash.
123142
def as_json(...)
124143
{
125144
scheme: @scheme,
@@ -133,10 +152,16 @@ def as_json(...)
133152
}
134153
end
135154

155+
# Convert the request to JSON.
156+
#
157+
# @returns [String] The request as JSON.
136158
def to_json(...)
137159
as_json.to_json(...)
138160
end
139161

162+
# Summarize the request as a string.
163+
#
164+
# @returns [String] The request as a string.
140165
def to_s
141166
"#{@scheme}://#{@authority}: #{@method} #{@path} #{@version}"
142167
end

0 commit comments

Comments
 (0)