Skip to content

Commit b098caa

Browse files
[DOC] Enhanced RDoc for Net::HTTP (#110)
1 parent b5f4f84 commit b098caa

File tree

2 files changed

+78
-91
lines changed

2 files changed

+78
-91
lines changed

doc/net-http/examples.rdoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ Many code examples here use these example websites:
1010

1111
Some examples also assume these variables:
1212

13-
uri = URI('https://jsonplaceholder.typicode.com')
13+
uri = URI('https://jsonplaceholder.typicode.com/')
1414
uri.freeze # Examples may not modify.
1515
hostname = uri.hostname # => "jsonplaceholder.typicode.com"
16+
path = uri.path # => "/"
1617
port = uri.port # => 443
1718

1819
So that example requests may be written as:

lib/net/http.rb

Lines changed: 76 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class HTTPHeaderSyntaxError < StandardError; end
3939
# - {Hypertext Transfer Protocol}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol].
4040
# - {Technical overview}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Technical_overview].
4141
#
42+
# == About the Examples
43+
#
44+
# :include: doc/net-http/examples.rdoc
45+
#
4246
# == Strategies
4347
#
4448
# - If you will make only a few GET requests,
@@ -49,17 +53,19 @@ class HTTPHeaderSyntaxError < StandardError; end
4953
# a {session}[rdoc-ref:Net::HTTP@Sessions] that sends a single request:
5054
#
5155
# # Return string response body.
52-
# Net::HTTP.get(hostname, path, port = 80)
53-
# Net::HTTP.get(uri, headers = {}, port = 80)
56+
# Net::HTTP.get(hostname, path)
57+
# Net::HTTP.get(uri)
5458
#
5559
# # Write string response body to $stdout.
56-
# Net::HTTP.get_print(hostname, path_or_uri, port = 80)
57-
# Net::HTTP.get_print(uri, headers = {}, port = 80)
60+
# Net::HTTP.get_print(hostname, path)
61+
# Net::HTTP.get_print(uri)
5862
#
5963
# # Return response as Net::HTTPResponse object.
60-
# Net::HTTP.get_response(hostname, path_or_uri, port = 80)
61-
# Net::HTTP.get_response(uri, headers = {}, port = 80)
62-
# Net::HTTP.post(uri, data, headers = {})
64+
# Net::HTTP.get_response(hostname, path)
65+
# Net::HTTP.get_response(uri)
66+
# data = '{"title": "foo", "body": "bar", "userId": 1}'
67+
# Net::HTTP.post(uri, data)
68+
# params = {title: 'foo', body: 'bar', userId: 1}
6369
# Net::HTTP.post_form(uri, params)
6470
#
6571
# - If performance is important, consider using sessions, which lower request overhead.
@@ -69,32 +75,29 @@ class HTTPHeaderSyntaxError < StandardError; end
6975
#
7076
# Net::HTTP.start(hostname) do |http|
7177
# # Session started automatically before block execution.
72-
# http.get(path_or_uri, headers = {})
73-
# http.head(path_or_uri, headers = {})
74-
# http.post(path_or_uri, body, headers = {}) # Can also have a block.
75-
# http.put(path_or_uri, body, headers = {})
76-
# http.delete(path_or_uri, headers = {Depth: 'Infinity'})
77-
# http.options(path_or_uri, headers = {})
78-
# http.trace(path_or_uri, headers = {})
79-
# http.patch(path_or_uri, body, headers = {}) # Can also have a block.
80-
# http.copy(path_or_uri, headers = {})
81-
# http.lock(path_or_uri, body, headers = {})
82-
# http.mkcol(path_or_uri, body = nil, headers = {})
83-
# http.move(path_or_uri, headers = {})
84-
# http.propfind(path_or_uri, body = nil, headers = {'Depth' => '0'})
85-
# http.proppatch(path_or_uri, body, headers = {})
86-
# http.unlock(path_or_uri, body, headers = {})
78+
# http.get(path)
79+
# http.head(path)
80+
# body = 'Some text'
81+
# http.post(path, body) # Can also have a block.
82+
# http.put(path, body)
83+
# http.delete(path)
84+
# http.options(path)
85+
# http.trace(path)
86+
# http.patch(path, body) # Can also have a block.
87+
# http.copy(path)
88+
# http.lock(path, body)
89+
# http.mkcol(path, body)
90+
# http.move(path)
91+
# http.propfind(path, body)
92+
# http.proppatch(path, body)
93+
# http.unlock(path, body)
8794
# # Session finished automatically at block exit.
8895
# end
8996
#
9097
# The methods cited above are convenience methods that, via their few arguments,
9198
# allow minimal control over the requests.
9299
# For greater control, consider using {request objects}[rdoc-ref:Net::HTTPRequest].
93100
#
94-
# == About the Examples
95-
#
96-
# :include: doc/net-http/examples.rdoc
97-
#
98101
# == URIs
99102
#
100103
# On the internet, a URI
@@ -263,91 +266,77 @@ class HTTPHeaderSyntaxError < StandardError; end
263266
#
264267
# For example, all 2XX responses are instances of a Net::HTTPSuccess
265268
# subclass, a 3XX response is an instance of a Net::HTTPRedirection
266-
# subclass and a 200 response is an instance of the Net::HTTPOK class. For
267-
# details of response classes, see the section "HTTP Response Classes"
268-
# below.
269+
# subclass and a 200 response is an instance of the Net::HTTPOK class.
270+
# For details, see HTTPResponse.
269271
#
270272
# Using a case statement you can handle various types of responses properly:
271273
#
272-
# def fetch(uri_str, limit = 10)
274+
# def fetch(uri, limit = 10)
273275
# # You should choose a better exception.
274276
# raise ArgumentError, 'too many HTTP redirects' if limit == 0
275277
#
276-
# response = Net::HTTP.get_response(URI(uri_str))
277-
#
278-
# case response
278+
# res = Net::HTTP.get_response(URI(uri))
279+
# case res
279280
# when Net::HTTPSuccess then
280-
# response
281+
# res
281282
# when Net::HTTPRedirection then
282-
# location = response['location']
283+
# location = res['location']
283284
# warn "redirected to #{location}"
284285
# fetch(location, limit - 1)
285286
# else
286-
# response.value
287+
# res.value
287288
# end
288289
# end
289290
#
290-
# print fetch('http://www.ruby-lang.org')
291+
# fetch(uri)
291292
#
292293
# == Basic Authentication
293294
#
294295
# Basic authentication is performed according to
295-
# [RFC2617](http://www.ietf.org/rfc/rfc2617.txt).
296-
#
297-
# uri = URI('http://example.com/index.html?key=value')
296+
# {RFC2617}[http://www.ietf.org/rfc/rfc2617.txt]:
298297
#
299298
# req = Net::HTTP::Get.new(uri)
300-
# req.basic_auth 'user', 'pass'
301-
#
302-
# res = Net::HTTP.start(uri.hostname, uri.port) {|http|
299+
# req.basic_auth('user', 'pass')
300+
# res = Net::HTTP.start(hostname) do |http|
303301
# http.request(req)
304-
# }
305-
# puts res.body
302+
# end
306303
#
307304
# == Streaming Response Bodies
308305
#
309-
# By default Net::HTTP reads an entire response into memory. If you are
306+
# By default \Net::HTTP reads an entire response into memory. If you are
310307
# handling large files or wish to implement a progress bar you can instead
311308
# stream the body directly to an IO.
312309
#
313-
# uri = URI('http://example.com/large_file')
314-
#
315-
# Net::HTTP.start(uri.host, uri.port) do |http|
316-
# request = Net::HTTP::Get.new uri
317-
#
318-
# http.request request do |response|
319-
# open 'large_file', 'w' do |io|
320-
# response.read_body do |chunk|
321-
# io.write chunk
310+
# Net::HTTP.start(hostname) do |http|
311+
# req = Net::HTTP::Get.new(uri)
312+
# http.request(req) do |res|
313+
# open('t.tmp', 'w') do |f|
314+
# res.read_body do |chunk|
315+
# f.write chunk
322316
# end
323317
# end
324318
# end
325319
# end
326320
#
327321
# == HTTPS
328322
#
329-
# HTTPS is enabled for an HTTP connection by Net::HTTP#use_ssl=.
330-
#
331-
# uri = URI('https://secure.example.com/some_path?query=string')
323+
# HTTPS is enabled for an HTTP connection by Net::HTTP#use_ssl=:
332324
#
333-
# Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
334-
# request = Net::HTTP::Get.new uri
335-
# response = http.request request # Net::HTTPResponse object
325+
# Net::HTTP.start(hostname, :use_ssl => true) do |http|
326+
# req = Net::HTTP::Get.new(uri)
327+
# res = http.request(req)
336328
# end
337329
#
338-
# Or if you simply want to make a GET request, you may pass in an URI
339-
# object that has an HTTPS URL. Net::HTTP automatically turns on TLS
340-
# verification if the URI object has a 'https' URI scheme.
330+
# Or if you simply want to make a GET request, you may pass in a URI
331+
# object that has an HTTPS URL. \Net::HTTP automatically turns on TLS
332+
# verification if the URI object has a 'https' URI scheme:
341333
#
342-
# uri = URI('https://example.com/')
343-
# Net::HTTP.get(uri) # => String
344-
#
345-
# In previous versions of Ruby you would need to require 'net/https' to use
346-
# HTTPS. This is no longer true.
334+
# uri # => #<URI::HTTPS https://jsonplaceholder.typicode.com/>
335+
# Net::HTTP.get(uri)
347336
#
348337
# == Proxies
349338
#
350-
# Net::HTTP will automatically create a proxy from the +http_proxy+
339+
# \Net::HTTP will automatically create a proxy from the +http_proxy+
351340
# environment variable if it is present. To disable use of +http_proxy+,
352341
# pass +nil+ for the proxy address.
353342
#
@@ -360,12 +349,9 @@ class HTTPHeaderSyntaxError < StandardError; end
360349
# # always proxy via your.proxy.addr:8080
361350
# }
362351
#
363-
# See Net::HTTP.new for further details and examples such as proxies that
364-
# require a username and password.
365-
#
366352
# == Compression
367353
#
368-
# Net::HTTP automatically adds Accept-Encoding for compression of response
354+
# \Net::HTTP automatically adds Accept-Encoding for compression of response
369355
# bodies and automatically decompresses gzip and deflate responses unless a
370356
# Range header was sent.
371357
#
@@ -686,7 +672,7 @@ class << HTTP
686672
alias newobj new # :nodoc:
687673
end
688674

689-
# Returns a new Net::HTTP object +http+
675+
# Returns a new \Net::HTTP object +http+
690676
# (but does not open a TCP connection or HTTP session).
691677
#
692678
# <b>No Proxy</b>
@@ -819,7 +805,7 @@ def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_p
819805
http
820806
end
821807

822-
# Creates a new Net::HTTP object for the specified server address,
808+
# Creates a new \Net::HTTP object for the specified server address,
823809
# without opening the TCP connection or initializing the HTTP session.
824810
# The +address+ should be a DNS hostname or IP address.
825811
def initialize(address, port = nil)
@@ -1007,7 +993,7 @@ def ipaddr=(addr)
1007993
# Number of seconds to wait for the connection to open. Any number
1008994
# may be used, including Floats for fractional seconds. If the HTTP
1009995
# object cannot open a connection in this many seconds, it raises a
1010-
# Net::OpenTimeout exception. The default value is 60 seconds.
996+
# \Net::OpenTimeout exception. The default value is 60 seconds.
1011997
attr_accessor :open_timeout
1012998

1013999
# Number of seconds to wait for one block to be read (via one read(2)
@@ -1019,12 +1005,12 @@ def ipaddr=(addr)
10191005
# Number of seconds to wait for one block to be written (via one write(2)
10201006
# call). Any number may be used, including Floats for fractional
10211007
# seconds. If the HTTP object cannot write data in this many seconds,
1022-
# it raises a Net::WriteTimeout exception. The default value is 60 seconds.
1023-
# Net::WriteTimeout is not raised on Windows.
1008+
# it raises a \Net::WriteTimeout exception. The default value is 60 seconds.
1009+
# \Net::WriteTimeout is not raised on Windows.
10241010
attr_reader :write_timeout
10251011

10261012
# Sets the maximum number of times to retry an idempotent request in case of
1027-
# Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
1013+
# \Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
10281014
# Errno::ECONNABORTED, Errno::EPIPE, OpenSSL::SSL::SSLError,
10291015
# Timeout::Error.
10301016
# The initial value is 1.
@@ -1084,7 +1070,7 @@ def continue_timeout=(sec)
10841070

10851071
# Seconds to reuse the connection of the previous request.
10861072
# If the idle time is less than this Keep-Alive Timeout,
1087-
# Net::HTTP reuses the TCP/IP socket used by the previous communication.
1073+
# \Net::HTTP reuses the TCP/IP socket used by the previous communication.
10881074
# The default value is 2 seconds.
10891075
attr_accessor :keep_alive_timeout
10901076

@@ -1109,7 +1095,7 @@ def use_ssl?
11091095
# Turn on/off SSL.
11101096
# This flag must be set before starting session.
11111097
# If you change use_ssl value after session started,
1112-
# a Net::HTTP object raises IOError.
1098+
# IOError is raised.
11131099
def use_ssl=(flag)
11141100
flag = flag ? true : false
11151101
if started? and @use_ssl != flag
@@ -1218,7 +1204,7 @@ def peer_cert
12181204

12191205
# Opens a TCP connection and HTTP session.
12201206
#
1221-
# When this method is called with a block, it passes the Net::HTTP
1207+
# When this method is called with a block, it passes the \Net::HTTP
12221208
# object to the block, and closes the TCP connection and HTTP session
12231209
# after the block has been executed.
12241210
#
@@ -1387,11 +1373,11 @@ def do_finish
13871373
@proxy_user = nil
13881374
@proxy_pass = nil
13891375

1390-
# Creates an HTTP proxy class which behaves like Net::HTTP, but
1376+
# Creates an HTTP proxy class which behaves like \Net::HTTP, but
13911377
# performs all access via the specified proxy.
13921378
#
13931379
# This class is obsolete. You may pass these same parameters directly to
1394-
# Net::HTTP.new. See Net::HTTP.new for details of the arguments.
1380+
# \Net::HTTP.new. See Net::HTTP.new for details of the arguments.
13951381
def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil) #:nodoc:
13961382
return self unless p_addr
13971383

@@ -1419,16 +1405,16 @@ def proxy_class?
14191405
defined?(@is_proxy_class) ? @is_proxy_class : false
14201406
end
14211407

1422-
# Address of proxy host. If Net::HTTP does not use a proxy, nil.
1408+
# Address of proxy host. If \Net::HTTP does not use a proxy, nil.
14231409
attr_reader :proxy_address
14241410

1425-
# Port number of proxy host. If Net::HTTP does not use a proxy, nil.
1411+
# Port number of proxy host. If \Net::HTTP does not use a proxy, nil.
14261412
attr_reader :proxy_port
14271413

1428-
# User name for accessing proxy. If Net::HTTP does not use a proxy, nil.
1414+
# User name for accessing proxy. If \Net::HTTP does not use a proxy, nil.
14291415
attr_reader :proxy_user
14301416

1431-
# User password for accessing proxy. If Net::HTTP does not use a proxy,
1417+
# User password for accessing proxy. \If Net::HTTP does not use a proxy,
14321418
# nil.
14331419
attr_reader :proxy_pass
14341420
end
@@ -1796,7 +1782,7 @@ def send_request(name, path, data = nil, header = nil)
17961782
#
17971783
# If +req+ is a Net::HTTP::Post or Net::HTTP::Put request containing
17981784
# data, the data is also sent. Providing data for a Net::HTTP::Head or
1799-
# Net::HTTP::Get request results in an ArgumentError.
1785+
# \Net::HTTP::Get request results in an ArgumentError.
18001786
#
18011787
# Returns an HTTPResponse object.
18021788
#

0 commit comments

Comments
 (0)