Skip to content

Commit 86b84eb

Browse files
[DOC] Enhanced RDoc for Net::HTTP (#89)
1 parent 46e966b commit 86b84eb

File tree

1 file changed

+101
-10
lines changed

1 file changed

+101
-10
lines changed

lib/net/http.rb

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -864,18 +864,63 @@ def initialize(address, port = nil)
864864
end
865865
end
866866

867+
# Returns a string representation of +self+:
868+
#
869+
# Net::HTTP.new(hostname).inspect
870+
# # => "#<Net::HTTP jsonplaceholder.typicode.com:80 open=false>"
871+
#
867872
def inspect
868873
"#<#{self.class} #{@address}:#{@port} open=#{started?}>"
869874
end
870875

871876
# *WARNING* This method opens a serious security hole.
872877
# Never use this method in production code.
873878
#
874-
# Sets an output stream for debugging.
879+
# Sets the output stream for debugging:
875880
#
876881
# http = Net::HTTP.new(hostname)
877-
# http.set_debug_output $stderr
878-
# http.start { .... }
882+
# File.open('t.tmp', 'w') do |file|
883+
# http.set_debug_output(file)
884+
# http.start
885+
# http.get('/nosuch/1')
886+
# http.finish
887+
# end
888+
# puts File.read('t.tmp')
889+
#
890+
# Output:
891+
#
892+
# opening connection to jsonplaceholder.typicode.com:80...
893+
# opened
894+
# <- "GET /nosuch/1 HTTP/1.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: Ruby\r\nHost: jsonplaceholder.typicode.com\r\n\r\n"
895+
# -> "HTTP/1.1 404 Not Found\r\n"
896+
# -> "Date: Mon, 12 Dec 2022 21:14:11 GMT\r\n"
897+
# -> "Content-Type: application/json; charset=utf-8\r\n"
898+
# -> "Content-Length: 2\r\n"
899+
# -> "Connection: keep-alive\r\n"
900+
# -> "X-Powered-By: Express\r\n"
901+
# -> "X-Ratelimit-Limit: 1000\r\n"
902+
# -> "X-Ratelimit-Remaining: 999\r\n"
903+
# -> "X-Ratelimit-Reset: 1670879660\r\n"
904+
# -> "Vary: Origin, Accept-Encoding\r\n"
905+
# -> "Access-Control-Allow-Credentials: true\r\n"
906+
# -> "Cache-Control: max-age=43200\r\n"
907+
# -> "Pragma: no-cache\r\n"
908+
# -> "Expires: -1\r\n"
909+
# -> "X-Content-Type-Options: nosniff\r\n"
910+
# -> "Etag: W/\"2-vyGp6PvFo4RvsFtPoIWeCReyIC8\"\r\n"
911+
# -> "Via: 1.1 vegur\r\n"
912+
# -> "CF-Cache-Status: MISS\r\n"
913+
# -> "Server-Timing: cf-q-config;dur=1.3000000762986e-05\r\n"
914+
# -> "Report-To: {\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=yOr40jo%2BwS1KHzhTlVpl54beJ5Wx2FcG4gGV0XVrh3X9OlR5q4drUn2dkt5DGO4GDcE%2BVXT7CNgJvGs%2BZleIyMu8CLieFiDIvOviOY3EhHg94m0ZNZgrEdpKD0S85S507l1vsEwEHkoTm%2Ff19SiO\"}],\"group\":\"cf-nel\",\"max_age\":604800}\r\n"
915+
# -> "NEL: {\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}\r\n"
916+
# -> "Server: cloudflare\r\n"
917+
# -> "CF-RAY: 778977dc484ce591-DFW\r\n"
918+
# -> "alt-svc: h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400\r\n"
919+
# -> "\r\n"
920+
# reading 2 bytes...
921+
# -> "{}"
922+
# read 2 bytes
923+
# Conn keep-alive
879924
#
880925
def set_debug_output(output)
881926
warn 'Net::HTTP#set_debug_output called after HTTP started', uplevel: 1 if started?
@@ -899,8 +944,24 @@ def set_debug_output(output)
899944
# body encoding.
900945
attr_reader :response_body_encoding
901946

902-
# Set the encoding to use for the response body. If given a String, find
903-
# the related Encoding.
947+
# Sets the encoding to be used for the response body;
948+
# returns the encoding.
949+
#
950+
# The given +value+ may be:
951+
#
952+
# - An Encoding object.
953+
# - The name of an encoding.
954+
# - An alias for an encoding name.
955+
#
956+
# See {Encoding}[https://docs.ruby-lang.org/en/master/Encoding.html].
957+
#
958+
# Examples:
959+
#
960+
# http = Net::HTTP.new(hostname)
961+
# http.response_body_encoding = Encoding::US_ASCII # => #<Encoding:US-ASCII>
962+
# http.response_body_encoding = 'US-ASCII' # => "US-ASCII"
963+
# http.response_body_encoding = 'ASCII' # => "ASCII"
964+
#
904965
def response_body_encoding=(value)
905966
value = Encoding.find(value) if value.is_a?(String)
906967
@response_body_encoding = value
@@ -912,12 +973,37 @@ def response_body_encoding=(value)
912973
attr_writer :proxy_user
913974
attr_writer :proxy_pass
914975

915-
# The IP address to connect to/used to connect to
976+
# Returns the IP address for the connection.
977+
#
978+
# If the session has not been started,
979+
# returns the value set by #ipaddr=,
980+
# or +nil+ if it has not been set:
981+
#
982+
# http = Net::HTTP.new(hostname)
983+
# http.ipaddr # => nil
984+
# http.ipaddr = '172.67.155.76'
985+
# http.ipaddr # => "172.67.155.76"
986+
#
987+
# If the session has been started,
988+
# returns the IP address from the socket:
989+
#
990+
# http = Net::HTTP.new(hostname)
991+
# http.start
992+
# http.ipaddr # => "172.67.155.76"
993+
# http.finish
994+
#
916995
def ipaddr
917996
started? ? @socket.io.peeraddr[3] : @ipaddr
918997
end
919998

920-
# Set the IP address to connect to
999+
# Sets the IP address for the connection:
1000+
#
1001+
# http = Net::HTTP.new(hostname)
1002+
# http.ipaddr # => nil
1003+
# http.ipaddr = '172.67.155.76'
1004+
# http.ipaddr # => "172.67.155.76"
1005+
#
1006+
# The IP address may not be set if the session has been started.
9211007
def ipaddr=(addr)
9221008
raise IOError, "ipaddr value changed, but session already started" if started?
9231009
@ipaddr = addr
@@ -942,12 +1028,17 @@ def ipaddr=(addr)
9421028
# Net::WriteTimeout is not raised on Windows.
9431029
attr_reader :write_timeout
9441030

945-
# Maximum number of times to retry an idempotent request in case of
1031+
# Sets the maximum number of times to retry an idempotent request in case of
9461032
# Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
9471033
# Errno::ECONNABORTED, Errno::EPIPE, OpenSSL::SSL::SSLError,
9481034
# Timeout::Error.
949-
# Should be a non-negative integer number. Zero means no retries.
950-
# The default value is 1.
1035+
# The initial value is 1.
1036+
#
1037+
# Argument +retries+ must be a non-negative numeric value:
1038+
#
1039+
# http.max_retries = 2 # => 2
1040+
# http.max_retries # => 2
1041+
#
9511042
def max_retries=(retries)
9521043
retries = retries.to_int
9531044
if retries < 0

0 commit comments

Comments
 (0)