Skip to content

Commit 2ea2038

Browse files
[DOC] Enhanced RDoc for Net::HTTP (#125)
1 parent aaf26b2 commit 2ea2038

File tree

1 file changed

+87
-43
lines changed

1 file changed

+87
-43
lines changed

lib/net/http.rb

Lines changed: 87 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,7 @@ def edit_path(path)
16031603
#
16041604
# With a block given, calls the block with the response body:
16051605
#
1606+
# http = Net::HTTP.new(hostname)
16061607
# http.get('/todos/1') do |res|
16071608
# p res
16081609
# end # => #<Net::HTTPOK 200 OK readbody=true>
@@ -1660,6 +1661,7 @@ def head(path, initheader = nil)
16601661
# With a block given, calls the block with the response body:
16611662
#
16621663
# data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
1664+
# http = Net::HTTP.new(hostname)
16631665
# http.post('/todos', data) do |res|
16641666
# p res
16651667
# end # => #<Net::HTTPCreated 201 Created readbody=true>
@@ -1693,6 +1695,7 @@ def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segme
16931695
# With a block given, calls the block with the response body:
16941696
#
16951697
# data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
1698+
# http = Net::HTTP.new(hostname)
16961699
# http.patch('/todos/1', data) do |res|
16971700
# p res
16981701
# end # => #<Net::HTTPOK 200 OK readbody=true>
@@ -1716,6 +1719,7 @@ def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segm
17161719
# created from string +path+, string +data+, and initial headers hash +initheader+.
17171720
#
17181721
# data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
1722+
# http = Net::HTTP.new(hostname)
17191723
# http.put('/todos/1', data) # => #<Net::HTTPOK 200 OK readbody=true>
17201724
#
17211725
def put(path, data, initheader = nil)
@@ -1729,6 +1733,7 @@ def put(path, data, initheader = nil)
17291733
# created from string +path+, string +body+, and initial headers hash +initheader+.
17301734
#
17311735
# data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
1736+
# http = Net::HTTP.new(hostname)
17321737
# http.proppatch('/todos/1', data)
17331738
#
17341739
def proppatch(path, body, initheader = nil)
@@ -1742,6 +1747,7 @@ def proppatch(path, body, initheader = nil)
17421747
# created from string +path+, string +body+, and initial headers hash +initheader+.
17431748
#
17441749
# data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
1750+
# http = Net::HTTP.new(hostname)
17451751
# http.lock('/todos/1', data)
17461752
#
17471753
def lock(path, body, initheader = nil)
@@ -1755,6 +1761,7 @@ def lock(path, body, initheader = nil)
17551761
# created from string +path+, string +body+, and initial headers hash +initheader+.
17561762
#
17571763
# data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
1764+
# http = Net::HTTP.new(hostname)
17581765
# http.unlock('/todos/1', data)
17591766
#
17601767
def unlock(path, body, initheader = nil)
@@ -1767,6 +1774,7 @@ def unlock(path, body, initheader = nil)
17671774
# The request is based on the Net::HTTP::Options object
17681775
# created from string +path+ and initial headers hash +initheader+.
17691776
#
1777+
# http = Net::HTTP.new(hostname)
17701778
# http.options('/')
17711779
#
17721780
def options(path, initheader = nil)
@@ -1780,6 +1788,7 @@ def options(path, initheader = nil)
17801788
# created from string +path+, string +body+, and initial headers hash +initheader+.
17811789
#
17821790
# data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
1791+
# http = Net::HTTP.new(hostname)
17831792
# http.propfind('/todos/1', data)
17841793
#
17851794
def propfind(path, body = nil, initheader = {'Depth' => '0'})
@@ -1792,6 +1801,7 @@ def propfind(path, body = nil, initheader = {'Depth' => '0'})
17921801
# The request is based on the Net::HTTP::Delete object
17931802
# created from string +path+ and initial headers hash +initheader+.
17941803
#
1804+
# http = Net::HTTP.new(hostname)
17951805
# http.delete('/todos/1')
17961806
#
17971807
def delete(path, initheader = {'Depth' => 'Infinity'})
@@ -1804,6 +1814,7 @@ def delete(path, initheader = {'Depth' => 'Infinity'})
18041814
# The request is based on the Net::HTTP::Move object
18051815
# created from string +path+ and initial headers hash +initheader+.
18061816
#
1817+
# http = Net::HTTP.new(hostname)
18071818
# http.move('/todos/1')
18081819
#
18091820
def move(path, initheader = nil)
@@ -1816,6 +1827,7 @@ def move(path, initheader = nil)
18161827
# The request is based on the Net::HTTP::Copy object
18171828
# created from string +path+ and initial headers hash +initheader+.
18181829
#
1830+
# http = Net::HTTP.new(hostname)
18191831
# http.copy('/todos/1')
18201832
#
18211833
def copy(path, initheader = nil)
@@ -1830,6 +1842,7 @@ def copy(path, initheader = nil)
18301842
#
18311843
# data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
18321844
# http.mkcol('/todos/1', data)
1845+
# http = Net::HTTP.new(hostname)
18331846
#
18341847
def mkcol(path, body = nil, initheader = nil)
18351848
request(Mkcol.new(path, initheader), body)
@@ -1841,6 +1854,7 @@ def mkcol(path, body = nil, initheader = nil)
18411854
# The request is based on the Net::HTTP::Trace object
18421855
# created from string +path+ and initial headers hash +initheader+.
18431856
#
1857+
# http = Net::HTTP.new(hostname)
18441858
# http.trace('/todos/1')
18451859
#
18461860
def trace(path, initheader = nil)
@@ -1873,50 +1887,56 @@ def request_get(path, initheader = nil, &block) # :yield: +response+
18731887
request(Get.new(path, initheader), &block)
18741888
end
18751889

1876-
# Sends a HEAD request to the +path+ and returns the response
1877-
# as a Net::HTTPResponse object.
1878-
#
1879-
# Returns the response.
1890+
# Sends a HEAD request to the server;
1891+
# returns an instance of a subclass of Net::HTTPResponse.
18801892
#
1881-
# This method never raises Net::* exceptions.
1893+
# The request is based on the Net::HTTP::Head object
1894+
# created from string +path+ and initial headers hash +initheader+.
18821895
#
1883-
# response = http.request_head('/index.html')
1884-
# p response['content-type']
1896+
# http = Net::HTTP.new(hostname)
1897+
# http.head('/todos/1') # => #<Net::HTTPOK 200 OK readbody=true>
18851898
#
18861899
def request_head(path, initheader = nil, &block)
18871900
request(Head.new(path, initheader), &block)
18881901
end
18891902

1890-
# Sends a POST request to the +path+.
1903+
# Sends a POST request to the server;
1904+
# forms the response into a Net::HTTPResponse object.
18911905
#
1892-
# Returns the response as a Net::HTTPResponse object.
1906+
# The request is based on the Net::HTTP::Post object
1907+
# created from string +path+, string +data+, and initial headers hash +initheader+.
18931908
#
1894-
# When called with a block, the block is passed an HTTPResponse
1895-
# object. The body of that response will not have been read yet;
1896-
# the block can process it using HTTPResponse#read_body, if desired.
1909+
# With no block given, returns the response object:
18971910
#
1898-
# Returns the response.
1911+
# http = Net::HTTP.new(hostname)
1912+
# http.post('/todos', 'xyzzy')
1913+
# # => #<Net::HTTPCreated 201 Created readbody=true>
1914+
#
1915+
# With a block given, calls the block with the response body
1916+
# and returns the response object:
18991917
#
1900-
# This method never raises Net::* exceptions.
1918+
# http.post('/todos', 'xyzzy') do |res|
1919+
# p res
1920+
# end # => #<Net::HTTPCreated 201 Created readbody=true>
19011921
#
1902-
# # example
1903-
# response = http.request_post('/cgi-bin/nice.rb', 'datadatadata...')
1904-
# p response.status
1905-
# puts response.body # body is already read in this case
1922+
# Output:
19061923
#
1907-
# # using block
1908-
# http.request_post('/cgi-bin/nice.rb', 'datadatadata...') {|response|
1909-
# p response.status
1910-
# p response['content-type']
1911-
# response.read_body do |str| # read body now
1912-
# print str
1913-
# end
1914-
# }
1924+
# "{\n \"xyzzy\": \"\",\n \"id\": 201\n}"
19151925
#
19161926
def request_post(path, data, initheader = nil, &block) # :yield: +response+
19171927
request Post.new(path, initheader), data, &block
19181928
end
19191929

1930+
# Sends a PUT request to the server;
1931+
# returns an instance of a subclass of Net::HTTPResponse.
1932+
#
1933+
# The request is based on the Net::HTTP::Put object
1934+
# created from string +path+, string +data+, and initial headers hash +initheader+.
1935+
#
1936+
# http = Net::HTTP.new(hostname)
1937+
# http.put('/todos/1', 'xyzzy')
1938+
# # => #<Net::HTTPOK 200 OK readbody=true>
1939+
#
19201940
def request_put(path, data, initheader = nil, &block) #:nodoc:
19211941
request Put.new(path, initheader), data, &block
19221942
end
@@ -1926,37 +1946,61 @@ def request_put(path, data, initheader = nil, &block) #:nodoc:
19261946
alias post2 request_post #:nodoc: obsolete
19271947
alias put2 request_put #:nodoc: obsolete
19281948

1929-
1930-
# Sends an \HTTP request to the \HTTP server.
1931-
# Also sends a DATA string if +data+ is given.
1949+
# Sends an \HTTP request to the server;
1950+
# returns an instance of a subclass of Net::HTTPResponse.
19321951
#
1933-
# Returns a Net::HTTPResponse object.
1952+
# The request is based on the Net::HTTPRequest object
1953+
# created from string +path+, string +data+, and initial headers hash +header+.
1954+
# That object is an instance of the
1955+
# {subclass of Net::HTTPRequest}[rdoc-ref:Net::HTTPRequest@Request+Subclasses],
1956+
# that corresponds to the given uppercase string +name+,
1957+
# which must be
1958+
# an {HTTP request method}[https://en.wikipedia.org/wiki/HTTP#Request_methods]
1959+
# or a {WebDAV request method}[https://en.wikipedia.org/wiki/WebDAV#Implementation].
19341960
#
1935-
# This method never raises Net::* exceptions.
1961+
# Examples:
19361962
#
1937-
# response = http.send_request('GET', '/index.html')
1938-
# puts response.body
1963+
# http = Net::HTTP.new(hostname)
1964+
# http.send_request('GET', '/todos/1')
1965+
# # => #<Net::HTTPOK 200 OK readbody=true>
1966+
# http.send_request('POST', '/todos', 'xyzzy')
1967+
# # => #<Net::HTTPCreated 201 Created readbody=true>
19391968
#
19401969
def send_request(name, path, data = nil, header = nil)
19411970
has_response_body = name != 'HEAD'
19421971
r = HTTPGenericRequest.new(name,(data ? true : false),has_response_body,path,header)
19431972
request r, data
19441973
end
19451974

1946-
# Sends an HTTPRequest object +req+ to the \HTTP server.
1975+
# Sends the given request +req+ to the server;
1976+
# forms the response into a Net::HTTPResponse object.
1977+
#
1978+
# The given +req+ must be an instance of a
1979+
# {subclass of Net::HTTPRequest}[rdoc-ref:Net::HTTPRequest@Request+Subclasses].
1980+
# Argument +body+ should be given only if needed for the request.
1981+
#
1982+
# With no block given, returns the response object:
1983+
#
1984+
# http = Net::HTTP.new(hostname)
19471985
#
1948-
# If +req+ is a Net::HTTP::Post or Net::HTTP::Put request containing
1949-
# data, the data is also sent. Providing data for a Net::HTTP::Head or
1950-
# \Net::HTTP::Get request results in an ArgumentError.
1986+
# req = Net::HTTP::Get.new('/todos/1')
1987+
# http.request(req)
1988+
# # => #<Net::HTTPOK 200 OK readbody=true>
19511989
#
1952-
# Returns an HTTPResponse object.
1990+
# req = Net::HTTP::Post.new('/todos')
1991+
# http.request(req, 'xyzzy')
1992+
# # => #<Net::HTTPCreated 201 Created readbody=true>
1993+
#
1994+
# With a block given, calls the block with the response and returns the response:
19531995
#
1954-
# When called with a block, passes an HTTPResponse object to the block.
1955-
# The body of the response will not have been read yet;
1956-
# the block can process it using HTTPResponse#read_body,
1957-
# if desired.
1996+
# req = Net::HTTP::Get.new('/todos/1')
1997+
# http.request(req) do |res|
1998+
# p res
1999+
# end # => #<Net::HTTPOK 200 OK readbody=true>
19582000
#
1959-
# This method never raises Net::* exceptions.
2001+
# Output:
2002+
#
2003+
# #<Net::HTTPOK 200 OK readbody=false>
19602004
#
19612005
def request(req, body = nil, &block) # :yield: +response+
19622006
unless started?

0 commit comments

Comments
 (0)