From 9bfa6c0f1896c1fb54ca4d24829c24b162c0bd09 Mon Sep 17 00:00:00 2001 From: BurdetteLamar Date: Sun, 5 Mar 2023 22:11:28 +0000 Subject: [PATCH 1/4] Enhanced RDoc for Net::HTTP --- lib/net/http/response.rb | 53 +++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 90c425b1..707febfc 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -224,13 +224,28 @@ def initialize(httpv, code, msg) #:nodoc: internal use only # Accept-Encoding header from the user. attr_accessor :decode_content - # The encoding to use for the response body. If Encoding, use that encoding. - # If other true value, attempt to detect the appropriate encoding, and use - # that. + # Returns the value set by body_encoding=, or +false+ if none; + # see #body_encoding=. attr_reader :body_encoding - # Set the encoding to use for the response body. If given a String, find - # the related Encoding. + # Sets the encoding that should be used when reading the body: + # + # - If the given value is an Encoding object, that encoding will be used. + # - Otherwise if the value is a string, the value of + # {Encoding#find(value)}[https://docs.ruby-lang.org/en/master/Encoding.html#method-c-find] + # will be used. + # - Otherwise an encoding will be deduced from the body itself. + # + # Examples: + # + # res = Net::HTTP.get_response(hostname, '/') + # # => # + # res.body_encoding # => false + # res.body_encoding = Encoding::US_ASCII + # res.body_encoding # => # + # res.body_encoding = 'US-ASCII' + # res.body_encoding # => # + # def body_encoding=(value) value = Encoding.find(value) if value.is_a?(String) @body_encoding = value @@ -362,26 +377,30 @@ def read_body(dest = nil, &block) @body end - # Returns the full entity body. + # Returns the string response body; + # note that repeated calls for the unmodified body return a cached string: # - # Calling this method a second or subsequent time will return the - # string already read. + # path = '/todos/1' + # Net::HTTP.start(hostname) do |http| + # res = http.get(path) + # p res.body + # p res.body.object_id + # p res.body.object_id + # p http.head(path).body # No body. + # end # - # http.request_get('/index.html') {|res| - # puts res.body - # } + # Output: # - # http.request_get('/index.html') {|res| - # p res.body.object_id # 538149362 - # p res.body.object_id # 538149362 - # } + # "{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false\n}" + # 13456920 + # 13456920 + # nil # def body read_body() end - # Because it may be necessary to modify the body, Eg, decompression - # this method facilitates that. + # Sets the body of the response to the given value. def body=(value) @body = value end From 882f4108cf550622e181cc7e4df7da32b19addc2 Mon Sep 17 00:00:00 2001 From: BurdetteLamar Date: Mon, 6 Mar 2023 21:07:05 +0000 Subject: [PATCH 2/4] Enhanced RDoc for Net::HTTP --- lib/net/http/response.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 707febfc..8db4bc0c 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -384,16 +384,12 @@ def read_body(dest = nil, &block) # Net::HTTP.start(hostname) do |http| # res = http.get(path) # p res.body - # p res.body.object_id - # p res.body.object_id # p http.head(path).body # No body. # end # # Output: # # "{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false\n}" - # 13456920 - # 13456920 # nil # def body From f40060ba34ddc7f55b3b43b93b8bef0a8e1ce841 Mon Sep 17 00:00:00 2001 From: BurdetteLamar Date: Tue, 7 Mar 2023 23:01:00 +0000 Subject: [PATCH 3/4] Enhanced RDoc for Net::HTTP --- lib/net/http/response.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 8db4bc0c..e789ecde 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -238,14 +238,18 @@ def initialize(httpv, code, msg) #:nodoc: internal use only # # Examples: # - # res = Net::HTTP.get_response(hostname, '/') - # # => # - # res.body_encoding # => false - # res.body_encoding = Encoding::US_ASCII - # res.body_encoding # => # - # res.body_encoding = 'US-ASCII' - # res.body_encoding # => # - # + # http = Net::HTTP.new(hostname) + # req = Net::HTTP::Get.new('/') + # + # http.request(req) do |res| + # p res.body.encoding # => # + # end + # + # http.request(req) do |res| + # res.body_encoding = "UTF-8" + # p res.body.encoding # => # + # end # res.body_encoding # => false + # def body_encoding=(value) value = Encoding.find(value) if value.is_a?(String) @body_encoding = value From c2878717239c5f72cf320ee42284cf454d0f8ced Mon Sep 17 00:00:00 2001 From: BurdetteLamar Date: Wed, 8 Mar 2023 15:23:40 +0000 Subject: [PATCH 4/4] Enhanced RDoc for Net::HTTP --- lib/net/http/response.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index e789ecde..43a4a698 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -248,7 +248,7 @@ def initialize(httpv, code, msg) #:nodoc: internal use only # http.request(req) do |res| # res.body_encoding = "UTF-8" # p res.body.encoding # => # - # end # res.body_encoding # => false + # end # def body_encoding=(value) value = Encoding.find(value) if value.is_a?(String)