[ruby/net-http] [DOC] Enhanced RDoc for Net::HTTP#get

(https://github.com/ruby/net-http/pull/121)

https://github.com/ruby/net-http/commit/51b9af1eed
This commit is contained in:
Burdette Lamar 2023-02-15 09:00:27 -06:00 committed by git
parent 3a9d52466a
commit 847a0df058

View File

@ -1592,45 +1592,38 @@ module Net #:nodoc:
public public
# Retrieves data from +path+ on the connected-to host which may be an # :call-seq:
# absolute path String or a URI to extract the path from. # get(path, initheader = nil) {|res| ... }
# #
# +initheader+ must be a Hash like { 'Accept' => '*/*', ... }, # Sends a GET request to the server;
# and it defaults to an empty hash. # returns a Net::HTTPResponse object,
# If +initheader+ doesn't have the key 'accept-encoding', then # which actually will be an instance of a subclass of that class:
# a value of "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" is used,
# so that gzip compression is used in preference to deflate
# compression, which is used in preference to no compression.
# Ruby doesn't have libraries to support the compress (Lempel-Ziv)
# compression, so that is not supported. The intent of this is
# to reduce bandwidth by default. If this routine sets up
# compression, then it does the decompression also, removing
# the header as well to prevent confusion. Otherwise
# it leaves the body as it found it.
# #
# This method returns a Net::HTTPResponse object. # The request is based on the Net::HTTP::Get object
# created from string +path+ and initial headers hash +initheader+.
# #
# If called with a block, yields each fragment of the # With a block given, calls the block with the response body:
# entity body in turn as a string as it is read from
# the socket. Note that in this case, the returned response
# object will *not* contain a (meaningful) body.
# #
# +dest+ argument is obsolete. # http.get('/todos/1') do |res|
# It still works but you must not use it. # p res
# end # => #<Net::HTTPOK 200 OK readbody=true>
# #
# This method never raises an exception. # Output:
# #
# response = http.get('/index.html') # "{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false\n}"
# #
# # using block # With no block given, simply returns the response object:
# File.open('result.txt', 'w') {|f| #
# http.get('/~foo/') do |str| # http.get('/') # => #<Net::HTTPOK 200 OK readbody=true>
# f.write str #
# end # Related:
# } #
# - Net::HTTP::Get: request class for \HTTP method GET.
# - Net::HTTP.get: sends GET request, returns response body.
# #
def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+ def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+
res = nil res = nil
request(Get.new(path, initheader)) {|r| request(Get.new(path, initheader)) {|r|
r.read_body dest, &block r.read_body dest, &block
res = r res = r