Skip to content

Commit 06f79cd

Browse files
[DOC] Enhanced RDoc for Net::HTTP (#122)
1 parent 51b9af1 commit 06f79cd

File tree

1 file changed

+57
-34
lines changed

1 file changed

+57
-34
lines changed

lib/net/http.rb

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,8 +1596,7 @@ def edit_path(path)
15961596
# get(path, initheader = nil) {|res| ... }
15971597
#
15981598
# Sends a GET request to the server;
1599-
# returns a Net::HTTPResponse object,
1600-
# which actually will be an instance of a subclass of that class:
1599+
# returns an instance of a subclass of Net::HTTPResponse.
16011600
#
16021601
# The request is based on the Net::HTTP::Get object
16031602
# created from string +path+ and initial headers hash +initheader+.
@@ -1631,57 +1630,81 @@ def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+
16311630
res
16321631
end
16331632

1634-
# Gets only the header from +path+ on the connected-to host.
1635-
# +header+ is a Hash like { 'Accept' => '*/*', ... }.
1633+
# Sends a HEAD request to the server;
1634+
# returns an instance of a subclass of Net::HTTPResponse.
16361635
#
1637-
# This method returns a Net::HTTPResponse object.
1638-
#
1639-
# This method never raises an exception.
1636+
# The request is based on the Net::HTTP::Head object
1637+
# created from string +path+ and initial headers hash +initheader+.
16401638
#
1641-
# response = nil
1642-
# Net::HTTP.start('some.www.server', 80) {|http|
1643-
# response = http.head('/index.html')
1644-
# }
1645-
# p response['content-type']
1639+
# res = http.head('/todos/1') # => #<Net::HTTPOK 200 OK readbody=true>
1640+
# res.body # => nil
1641+
# res.to_hash.take(3)
1642+
# # =>
1643+
# [["date", ["Wed, 15 Feb 2023 15:25:42 GMT"]],
1644+
# ["content-type", ["application/json; charset=utf-8"]],
1645+
# ["connection", ["close"]]]
16461646
#
16471647
def head(path, initheader = nil)
16481648
request(Head.new(path, initheader))
16491649
end
16501650

1651-
# Posts +data+ (must be a String) to +path+. +header+ must be a Hash
1652-
# like { 'Accept' => '*/*', ... }.
1651+
# :call-seq:
1652+
# post(path, data, initheader = nil) {|res| ... }
1653+
#
1654+
# Sends a POST request to the server;
1655+
# returns an instance of a subclass of Net::HTTPResponse.
1656+
#
1657+
# The request is based on the Net::HTTP::Post object
1658+
# created from string +path+, string +data+, and initial headers hash +initheader+.
1659+
#
1660+
# With a block given, calls the block with the response body:
16531661
#
1654-
# This method returns a Net::HTTPResponse object.
1662+
# data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
1663+
# http.post('/todos', data) do |res|
1664+
# p res
1665+
# end # => #<Net::HTTPCreated 201 Created readbody=true>
16551666
#
1656-
# If called with a block, yields each fragment of the
1657-
# entity body in turn as a string as it is read from
1658-
# the socket. Note that in this case, the returned response
1659-
# object will *not* contain a (meaningful) body.
1667+
# Output:
16601668
#
1661-
# +dest+ argument is obsolete.
1662-
# It still works but you must not use it.
1669+
# "{\n \"{\\\"userId\\\": 1, \\\"id\\\": 1, \\\"title\\\": \\\"delectus aut autem\\\", \\\"completed\\\": false}\": \"\",\n \"id\": 201\n}"
16631670
#
1664-
# This method never raises exception.
1671+
# With no block given, simply returns the response object:
16651672
#
1666-
# response = http.post('/cgi-bin/search.rb', 'query=foo')
1673+
# http.post('/todos', data) # => #<Net::HTTPCreated 201 Created readbody=true>
16671674
#
1668-
# # using block
1669-
# File.open('result.txt', 'w') {|f|
1670-
# http.post('/cgi-bin/search.rb', 'query=foo') do |str|
1671-
# f.write str
1672-
# end
1673-
# }
1675+
# Related:
16741676
#
1675-
# You should set Content-Type: header field for POST.
1676-
# If no Content-Type: field given, this method uses
1677-
# "application/x-www-form-urlencoded" by default.
1677+
# - Net::HTTP::Post: request class for \HTTP method POST.
1678+
# - Net::HTTP.post: sends POST request, returns response body.
16781679
#
16791680
def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
16801681
send_entity(path, data, initheader, dest, Post, &block)
16811682
end
16821683

1683-
# Sends a PATCH request to the +path+ and gets a response,
1684-
# as an HTTPResponse object.
1684+
# :call-seq:
1685+
# patch(path, data, initheader = nil) {|res| ... }
1686+
#
1687+
# Sends a PATCH request to the server;
1688+
# returns an instance of a subclass of Net::HTTPResponse.
1689+
#
1690+
# The request is based on the Net::HTTP::Patch object
1691+
# created from string +path+, string +data+, and initial headers hash +initheader+.
1692+
#
1693+
# With a block given, calls the block with the response body:
1694+
#
1695+
# data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
1696+
# http.patch('/todos/1', data) do |res|
1697+
# p res
1698+
# end # => #<Net::HTTPOK 200 OK readbody=true>
1699+
#
1700+
# Output:
1701+
#
1702+
# "{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false,\n \"{\\\"userId\\\": 1, \\\"id\\\": 1, \\\"title\\\": \\\"delectus aut autem\\\", \\\"completed\\\": false}\": \"\"\n}"
1703+
#
1704+
# With no block given, simply returns the response object:
1705+
#
1706+
# http.patch('/todos/1', data) # => #<Net::HTTPCreated 201 Created readbody=true>
1707+
#
16851708
def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
16861709
send_entity(path, data, initheader, dest, Patch, &block)
16871710
end

0 commit comments

Comments
 (0)