Skip to content

Commit 4444e8c

Browse files
committed
Enhanced RDoc for Net::HTTP
1 parent 6b30c53 commit 4444e8c

File tree

3 files changed

+15
-48
lines changed

3 files changed

+15
-48
lines changed

lib/net/http.rb

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class HTTPHeaderSyntaxError < StandardError; end
106106
# Many code examples here use these example websites:
107107
#
108108
# - https://jsonplaceholder.typicode.com.
109-
# - http:example.com.
109+
# - http://example.com.
110110
#
111111
# Some examples also assume these variables:
112112
#
@@ -137,7 +137,7 @@ class HTTPHeaderSyntaxError < StandardError; end
137137
# It consists of some or all of: scheme, hostname, path, query, and fragment;
138138
# see {URI syntax}[https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#Syntax].
139139
#
140-
# A Ruby {URI::Generic}[]https://docs.ruby-lang.org/en/master/URI/Generic.html] object
140+
# A Ruby {URI::Generic}[https://docs.ruby-lang.org/en/master/URI/Generic.html] object
141141
# represents an internet URI.
142142
# It provides, among others, methods
143143
# +scheme+, +hostname+, +path+, +query+, and +fragment+.
@@ -204,30 +204,6 @@ class HTTPHeaderSyntaxError < StandardError; end
204204
# {Request Fields}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields].
205205
# A host may also accept other custom fields.
206206
#
207-
# The following example performs a conditional GET using the
208-
# <tt>If-Modified-Since</tt> header:
209-
#
210-
# - If the file +cached_response+ has been modified since the time
211-
# put into the header,
212-
# the return is a \Net::HTTPSuccess object,
213-
# and the file is overwritten with the response body.
214-
# - Otherwise, the return is a \Net::HTTPNotModified object,
215-
# and the file remains unchanged.
216-
#
217-
# The code:
218-
#
219-
# path = 'cached_response'
220-
# File.write(path, '') unless File.exist?(path)
221-
# file = File.stat(path)
222-
# req = Net::HTTP::Get.new(uri)
223-
# req['If-Modified-Since'] = file.mtime.rfc2822
224-
# res = Net::HTTP.start(hostname) do |http|
225-
# http.request(req)
226-
# end
227-
# if res.is_a?(Net::HTTPSuccess)
228-
# File.write(path, res.body)
229-
# end
230-
#
231207
# == Sessions
232208
#
233209
# A _session_ is a connection between a server (host) and a client that:
@@ -465,7 +441,7 @@ class << HTTP
465441

466442
# :call-seq:
467443
# Net::HTTP.get_print(hostname, path, port = 80) -> nil
468-
# Net::HTTP:get_print(uri, headers = {}, port = 80) -> nil
444+
# Net::HTTP:get_print(uri, headers = {}, port = uri.port) -> nil
469445
#
470446
# Like Net::HTTP.get, but writes the returned body to $stdout;
471447
# returns +nil+.
@@ -480,7 +456,7 @@ def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil)
480456

481457
# :call-seq:
482458
# Net::HTTP.get(hostname, path, port = 80) -> body
483-
# Net::HTTP:get(uri, headers = {}, port = 80) -> body
459+
# Net::HTTP:get(uri, headers = {}, port = uri.port) -> body
484460
#
485461
# Sends a GET request and returns the \HTTP response body as a string.
486462
#
@@ -496,33 +472,22 @@ def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil)
496472
# "userId": 1,
497473
# "id": 1,
498474
# "title": "delectus aut autem",
499-
# "completed":
475+
# "completed": false
500476
# }
501477
#
502478
# With URI object +uri+ and optional hash argument +headers+:
503479
#
504480
# uri = URI('https://jsonplaceholder.typicode.com/todos/1')
505-
# headers = {Accept: 'text/html'}
506-
# puts Net::HTTP.get(uri, headers)
481+
# headers = {'Content-type' => 'application/json; charset=UTF-8'}
482+
# Net::HTTP.get(uri, headers)
507483
#
508-
# Output:
509-
#
510-
# {
511-
# "userId": 1,
512-
# "id": 1,
513-
# "title": "delectus aut autem",
514-
# "completed": false
515-
# }
516-
#
517-
# In either case, the third argument is an integer port number,
518-
# which defaults to 80.
519484
def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
520485
get_response(uri_or_host, path_or_headers, port).body
521486
end
522487

523488
# :call-seq:
524489
# Net::HTTP.get_response(hostname, path, port = 80) -> http_response
525-
# Net::HTTP:get_response(uri, headers = {}, port = 80) -> http_response
490+
# Net::HTTP:get_response(uri, headers = {}, port = uri.port) -> http_response
526491
#
527492
# Like Net::HTTP.get, but returns an Net::HTTPResponse object
528493
# instead of the body string.
@@ -611,6 +576,9 @@ def HTTP.socket_type #:nodoc: obsolete
611576
BufferedIO
612577
end
613578

579+
# :call-seq:
580+
# HTTP.start(address, port, p_addr, p_port, p_user, p_pass) {|http| ... }
581+
# HTTP.start(address, port=nil, p_addr=:ENV, p_port=nil, p_user=nil, p_pass=nil, opt) {|http| ... }
614582
# Creates a new \Net::HTTP object,
615583
# opens a TCP connection and \HTTP session.
616584
#

lib/net/http/request.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Many code examples here use these example websites:
1717
#
1818
# - https://jsonplaceholder.typicode.com.
19-
# - http:example.com.
19+
# - http://example.com.
2020
#
2121
# Some examples also assume these variables:
2222
#
@@ -54,8 +54,7 @@
5454
#
5555
# A POST request may be sent using request class \Net::HTTP::Post:
5656
#
57-
# require 'json'
58-
# json = JSON.generate({title: 'foo', body: 'bar', userId: 1})
57+
# json = {title: 'foo', body: 'bar', userId: 1}
5958
# # => "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}"
6059
# _uri = uri.dup
6160
# _uri.path = '/posts'

lib/net/http/response.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# Many code examples here use these example websites:
1313
#
1414
# - https://jsonplaceholder.typicode.com.
15-
# - http:example.com.
15+
# - http://example.com.
1616
#
1717
# Some examples also assume these variables:
1818
#
@@ -94,7 +94,7 @@
9494
# - Net::HTTPAlreadyReported (208)
9595
# - Net::HTTPIMUsed (226)
9696
#
97-
# - HTTPRedirection:
97+
# - Net::HTTPRedirection:
9898
#
9999
# - Net::HTTPMultipleChoices (300)
100100
# - Net::HTTPMovedPermanently (301)

0 commit comments

Comments
 (0)