Skip to content

Commit 14c8405

Browse files
[DOC] Enhanced RDoc for HTTPGenericRequest (#113)
1 parent 68255ac commit 14c8405

File tree

1 file changed

+84
-8
lines changed

1 file changed

+84
-8
lines changed

lib/net/http/generic_request.rb

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# frozen_string_literal: false
2-
# HTTPGenericRequest is the parent of the Net::HTTPRequest class.
3-
# Do not use this directly; use a subclass of Net::HTTPRequest.
42
#
5-
# Mixes in the Net::HTTPHeader module to provide easier access to HTTP headers.
3+
# \HTTPGenericRequest is the parent of the Net::HTTPRequest class.
4+
#
5+
# Do not use this directly; instead, use a subclass of Net::HTTPRequest.
6+
#
7+
# == About the Examples
8+
#
9+
# :include: doc/net-http/examples.rdoc
610
#
711
class Net::HTTPGenericRequest
812

913
include Net::HTTPHeader
1014

11-
def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
15+
def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc:
1216
@method = m
1317
@request_has_body = reqbody
1418
@response_has_body = resbody
@@ -53,15 +57,47 @@ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
5357
@body_data = nil
5458
end
5559

60+
# Returns the string method name for the request:
61+
#
62+
# Net::HTTP::Get.new(uri).method # => "GET"
63+
# Net::HTTP::Post.new(uri).method # => "POST"
64+
#
5665
attr_reader :method
66+
67+
# Returns the string path for the request:
68+
#
69+
# Net::HTTP::Get.new(uri).path # => "/"
70+
# Net::HTTP::Post.new('example.com').path # => "example.com"
71+
#
5772
attr_reader :path
73+
74+
# Returns the URI object for the request, or +nil+ if none:
75+
#
76+
# Net::HTTP::Get.new(uri).uri
77+
# # => #<URI::HTTPS https://jsonplaceholder.typicode.com/>
78+
# Net::HTTP::Get.new('example.com').uri # => nil
79+
#
5880
attr_reader :uri
5981

60-
# Automatically set to false if the user sets the Accept-Encoding header.
61-
# This indicates they wish to handle Content-encoding in responses
62-
# themselves.
82+
# Returns +false+ if the request's header <tt>'Accept-Encoding'</tt>
83+
# has been set manually or deleted
84+
# (indicating that the user intends to handle encoding in the response),
85+
# +true+ otherwise:
86+
#
87+
# req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET>
88+
# req['Accept-Encoding'] # => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
89+
# req.decode_content # => true
90+
# req['Accept-Encoding'] = 'foo'
91+
# req.decode_content # => false
92+
# req.delete('Accept-Encoding')
93+
# req.decode_content # => false
94+
#
6395
attr_reader :decode_content
6496

97+
# Returns a string representation of the request:
98+
#
99+
# Net::HTTP::Post.new(uri).inspect # => "#<Net::HTTP::Post POST>"
100+
#
65101
def inspect
66102
"\#<#{self.class} #{@method}>"
67103
end
@@ -76,30 +112,70 @@ def []=(key, val) # :nodoc:
76112
super key, val
77113
end
78114

115+
# Returns whether the request may have a body:
116+
#
117+
# Net::HTTP::Post.new(uri).request_body_permitted? # => true
118+
# Net::HTTP::Get.new(uri).request_body_permitted? # => false
119+
#
79120
def request_body_permitted?
80121
@request_has_body
81122
end
82123

124+
# Returns whether the response may have a body:
125+
#
126+
# Net::HTTP::Post.new(uri).response_body_permitted? # => true
127+
# Net::HTTP::Head.new(uri).response_body_permitted? # => false
128+
#
83129
def response_body_permitted?
84130
@response_has_body
85131
end
86132

87-
def body_exist?
133+
def body_exist? # :nodoc:
88134
warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?", uplevel: 1 if $VERBOSE
89135
response_body_permitted?
90136
end
91137

138+
# Returns the string body for the request, or +nil+ if there is none:
139+
#
140+
# req = Net::HTTP::Post.new(uri)
141+
# req.body # => nil
142+
# req.body = '{"title": "foo","body": "bar","userId": 1}'
143+
# req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
144+
#
92145
attr_reader :body
93146

147+
# Sets the body for the request:
148+
#
149+
# req = Net::HTTP::Post.new(uri)
150+
# req.body # => nil
151+
# req.body = '{"title": "foo","body": "bar","userId": 1}'
152+
# req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
153+
#
94154
def body=(str)
95155
@body = str
96156
@body_stream = nil
97157
@body_data = nil
98158
str
99159
end
100160

161+
# Returns the body stream object for the request, or +nil+ if there is none:
162+
#
163+
# req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
164+
# req.body_stream # => nil
165+
# require 'stringio'
166+
# req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
167+
# req.body_stream # => #<StringIO:0x0000027d1e5affa8>
168+
#
101169
attr_reader :body_stream
102170

171+
# Sets the body stream for the request:
172+
#
173+
# req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
174+
# req.body_stream # => nil
175+
# require 'stringio'
176+
# req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
177+
# req.body_stream # => #<StringIO:0x0000027d1e5affa8>
178+
#
103179
def body_stream=(input)
104180
@body = nil
105181
@body_stream = input

0 commit comments

Comments
 (0)