Skip to content

Commit 22c0d34

Browse files
[DOC] Enhanced RDoc for Net::HTTP (#116)
1 parent 6da39b0 commit 22c0d34

File tree

1 file changed

+59
-19
lines changed

1 file changed

+59
-19
lines changed

lib/net/http.rb

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,7 @@ def read_timeout=(sec)
10581058
# EOF
10591059
# headers = {'content-type': 'application/json'}
10601060
# http = Net::HTTP.new(hostname)
1061+
# http.write_timeout # => 60
10611062
# http.post(_uri.path, data, headers)
10621063
# # => #<Net::HTTPCreated 201 Created readbody=true>
10631064
# http.write_timeout = 0
@@ -1068,12 +1069,15 @@ def write_timeout=(sec)
10681069
@write_timeout = sec
10691070
end
10701071

1071-
# Seconds to wait for 100 Continue response. If the \HTTP object does not
1072-
# receive a response in this many seconds it sends the request body. The
1073-
# default value is +nil+.
1072+
# Returns the continue timeout value.
1073+
# See Net::HTTP.continue_timeout=.
1074+
#
10741075
attr_reader :continue_timeout
10751076

1076-
# Setter for the continue_timeout attribute.
1077+
# Sets the continue timeout value,
1078+
# which is the number of seconds to wait for an expected 100 Continue response.
1079+
# If the \HTTP object does not receive a response in this many seconds
1080+
# it sends the request body.
10771081
def continue_timeout=(sec)
10781082
@socket.continue_timeout = sec if @socket
10791083
@continue_timeout = sec
@@ -1089,7 +1093,20 @@ def continue_timeout=(sec)
10891093
# Content-Length headers. For backwards compatibility, the default is true.
10901094
attr_accessor :ignore_eof
10911095

1092-
# Returns true if the \HTTP session has been started.
1096+
# Returns +true+ if the \HTTP session has been started:
1097+
#
1098+
# http = Net::HTTP.new(hostname)
1099+
# http.started? # => false
1100+
# http.start
1101+
# http.started? # => true
1102+
# http.finish # => nil
1103+
# http.started? # => false
1104+
#
1105+
# Net::HTTP.start(hostname) do |http|
1106+
# http.started?
1107+
# end # => true
1108+
# http.started? # => false
1109+
#
10931110
def started?
10941111
@started
10951112
end
@@ -1098,15 +1115,18 @@ def started?
10981115

10991116
attr_accessor :close_on_empty_response
11001117

1101-
# Returns true if SSL/TLS is being used with \HTTP.
1118+
# Returns +true+ if +self+ uses SSL, +false+ otherwise.
1119+
# See Net::HTTP#use_ssl=.
11021120
def use_ssl?
11031121
@use_ssl
11041122
end
11051123

1106-
# Turn on/off SSL.
1107-
# This flag must be set before starting session.
1108-
# If you change use_ssl value after session started,
1109-
# IOError is raised.
1124+
# Sets whether a new session is to use
1125+
# {Transport Layer Security}[https://en.wikipedia.org/wiki/Transport_Layer_Security]:
1126+
#
1127+
# Raises IOError if attempting to change during a session.
1128+
#
1129+
# Raises OpenSSL::SSL::SSLError if the port is not an HTTPS port.
11101130
def use_ssl=(flag)
11111131
flag = flag ? true : false
11121132
if started? and @use_ssl != flag
@@ -1205,22 +1225,35 @@ def use_ssl=(flag)
12051225
# See OpenSSL::SSL::SSLContext#verify_hostname=
12061226
attr_accessor :verify_hostname
12071227

1208-
# Returns the X.509 certificates the server presented.
1228+
# The X509 certificate chain (an array of strings) for the session's socket peer,
1229+
# or +nil+ if none.
12091230
def peer_cert
12101231
if not use_ssl? or not @socket
12111232
return nil
12121233
end
12131234
@socket.io.peer_cert
12141235
end
12151236

1216-
# Opens a TCP connection and \HTTP session.
1237+
# Starts an \HTTP session.
12171238
#
1218-
# When this method is called with a block, it passes the \Net::HTTP
1219-
# object to the block, and closes the TCP connection and \HTTP session
1220-
# after the block has been executed.
1239+
# Without a block, returns +self+:
12211240
#
1222-
# When called with a block, it returns the return value of the
1223-
# block; otherwise, it returns self.
1241+
# http = Net::HTTP.new(hostname)
1242+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
1243+
# http.start
1244+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=true>
1245+
# http.started? # => true
1246+
# http.finish
1247+
#
1248+
# With a block, calls the block with +self+,
1249+
# finishes the session when the block exits,
1250+
# and returns the block's value:
1251+
#
1252+
# http.start do |http|
1253+
# http
1254+
# end
1255+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
1256+
# http.started? # => false
12241257
#
12251258
def start # :yield: http
12261259
raise IOError, 'HTTP session already opened' if @started
@@ -1356,8 +1389,15 @@ def on_connect
13561389
end
13571390
private :on_connect
13581391

1359-
# Finishes the \HTTP session and closes the TCP connection.
1360-
# Raises IOError if the session has not been started.
1392+
# Finishes the \HTTP session:
1393+
#
1394+
# http = Net::HTTP.new(hostname)
1395+
# http.start
1396+
# http.started? # => true
1397+
# http.finish # => nil
1398+
# http.started? # => false
1399+
#
1400+
# Raises IOError if not in a session.
13611401
def finish
13621402
raise IOError, 'HTTP session not yet started' unless started?
13631403
do_finish

0 commit comments

Comments
 (0)