Skip to content

Commit 9d9040f

Browse files
[DOC] Enhanced RDoc for Net::HTTP (#85)
1 parent 1a212e2 commit 9d9040f

File tree

1 file changed

+192
-62
lines changed

1 file changed

+192
-62
lines changed

lib/net/http.rb

Lines changed: 192 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,11 @@ def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil)
452452
# headers = {'Content-type' => 'application/json; charset=UTF-8'}
453453
# Net::HTTP.get(uri, headers)
454454
#
455+
# Related:
456+
#
457+
# - Net::HTTP::Get: request class for \HTTP method +GET+.
458+
# - Net::HTTP#get: convenience method for \HTTP method +GET+.
459+
#
455460
def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
456461
get_response(uri_or_host, path_or_headers, port).body
457462
end
@@ -460,7 +465,7 @@ def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
460465
# Net::HTTP.get_response(hostname, path, port = 80) -> http_response
461466
# Net::HTTP:get_response(uri, headers = {}, port = uri.port) -> http_response
462467
#
463-
# Like Net::HTTP.get, but returns an Net::HTTPResponse object
468+
# Like Net::HTTP.get, but returns a Net::HTTPResponse object
464469
# instead of the body string.
465470
def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
466471
if path_or_headers && !path_or_headers.is_a?(Hash)
@@ -479,16 +484,31 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
479484
end
480485
end
481486

482-
# Posts data to the specified URI object.
487+
# Posts data to a host; returns a Net::HTTPResponse object.
483488
#
484-
# Example:
489+
# Argument +url+ must be a URL;
490+
# argument +data+ must be a string:
485491
#
486-
# require 'net/http'
487-
# require 'uri'
492+
# _uri = uri.dup
493+
# _uri.path = '/posts'
494+
# data = '{"title": "foo", "body": "bar", "userId": 1}'
495+
# headers = {'content-type': 'application/json'}
496+
# res = Net::HTTP.post(_uri, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true>
497+
# puts res.body
488498
#
489-
# Net::HTTP.post URI('http://www.example.com/api/search'),
490-
# { "q" => "ruby", "max" => "50" }.to_json,
491-
# "Content-Type" => "application/json"
499+
# Output:
500+
#
501+
# {
502+
# "title": "foo",
503+
# "body": "bar",
504+
# "userId": 1,
505+
# "id": 101
506+
# }
507+
#
508+
# Related:
509+
#
510+
# - Net::HTTP::Post: request class for \HTTP method +POST+.
511+
# - Net::HTTP#post: convenience method for \HTTP method +POST+.
492512
#
493513
def HTTP.post(url, data, header = nil)
494514
start(url.hostname, url.port,
@@ -497,22 +517,25 @@ def HTTP.post(url, data, header = nil)
497517
}
498518
end
499519

500-
# Posts HTML form data to the specified URI object.
501-
# The form data must be provided as a Hash mapping from String to String.
502-
# Example:
503-
#
504-
# { "cmd" => "search", "q" => "ruby", "max" => "50" }
520+
# Posts data to a host; returns a Net::HTTPResponse object.
505521
#
506-
# This method also does Basic Authentication if and only if +url+.user exists.
507-
# But userinfo for authentication is deprecated (RFC3986).
508-
# So this feature will be removed.
522+
# Argument +url+ must be a URI;
523+
# argument +data+ must be a hash:
509524
#
510-
# Example:
525+
# _uri = uri.dup
526+
# _uri.path = '/posts'
527+
# data = {title: 'foo', body: 'bar', userId: 1}
528+
# res = Net::HTTP.post_form(_uri, data) # => #<Net::HTTPCreated 201 Created readbody=true>
529+
# puts res.body
511530
#
512-
# require 'net/http'
531+
# Output:
513532
#
514-
# Net::HTTP.post_form URI('http://www.example.com/search.cgi'),
515-
# { "q" => "ruby", "max" => "50" }
533+
# {
534+
# "title": "foo",
535+
# "body": "bar",
536+
# "userId": "1",
537+
# "id": 101
538+
# }
516539
#
517540
def HTTP.post_form(url, params)
518541
req = Post.new(url)
@@ -528,17 +551,26 @@ def HTTP.post_form(url, params)
528551
# HTTP session management
529552
#
530553

531-
# The default port to use for HTTP requests; defaults to 80.
554+
# Returns intger +80+, the default port to use for HTTP requests:
555+
#
556+
# Net::HTTP.default_port # => 80
557+
#
532558
def HTTP.default_port
533559
http_default_port()
534560
end
535561

536-
# The default port to use for HTTP requests; defaults to 80.
562+
# Returns integer +80+, the default port to use for HTTP requests:
563+
#
564+
# Net::HTTP.http_default_port # => 80
565+
#
537566
def HTTP.http_default_port
538567
80
539568
end
540569

541-
# The default port to use for HTTPS requests; defaults to 443.
570+
# Returns integer +443+, the default port to use for HTTPS requests:
571+
#
572+
# Net::HTTP.https_default_port # => 443
573+
#
542574
def HTTP.https_default_port
543575
443
544576
end
@@ -548,20 +580,42 @@ def HTTP.socket_type #:nodoc: obsolete
548580
end
549581

550582
# :call-seq:
551-
# HTTP.start(address, port, p_addr, p_port, p_user, p_pass) {|http| ... }
552-
# HTTP.start(address, port=nil, p_addr=:ENV, p_port=nil, p_user=nil, p_pass=nil, opt) {|http| ... }
583+
# HTTP.start(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, opts) -> http
584+
# HTTP.start(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, opts) {|http| ... } -> object
585+
#
586+
# Creates a new \Net::HTTP object, +http+, via \Net::HTTP.new:
587+
#
588+
# Net::HTTP.new(address, port, p_addr, p_port, p_user, p_pass)
589+
#
590+
# - For arguments +hostname+ through +p_pass+, see Net::HTTP.new.
591+
# - For argument +opts+, see below.
592+
#
593+
# Note: If +port+ is +nil+ and <tt>opts[:use_ssl]</tt> is a truthy value,
594+
# the value passed to +new+ is Net::HTTP.https_default_port, not +port+.
553595
#
554-
# Creates a new \Net::HTTP object,
555-
# opens a TCP connection and \HTTP session.
596+
# With no block given:
556597
#
557-
# Argument +address+ is the hostname or IP address of the server.
598+
# - Calls <tt>http.start</tt> with no block (see #start),
599+
# which opens a TCP connection and \HTTP session.
600+
# - Returns +http+.
601+
# - The caller should call #finish to close the session:
602+
#
603+
# http = Net::HTTP.start(hostname)
604+
# http.started? # => true
605+
# http.finish
606+
# http.started? # => false
558607
#
559608
# With a block given:
560609
#
561-
# - Passes the object to the given block,
562-
# which may make any number of requests to the host.
563-
# - Closes the \HTTP session on block exit.
564-
# - Returns the block's value.
610+
# - Calls <tt>http.start</tt> with the block (see #start), which:
611+
#
612+
# - Opens a TCP connection and \HTTP session.
613+
# - Calls the block,
614+
# which may make any number of requests to the host.
615+
# - Closes the \HTTP session and TCP connection on block exit.
616+
# - Returns the block's value +object+.
617+
#
618+
# - Returns +object+.
565619
#
566620
# Example:
567621
#
@@ -586,19 +640,7 @@ def HTTP.socket_type #:nodoc: obsolete
586640
# "completed": false
587641
# }
588642
#
589-
# With no block given, returns the \Net::HTTP object;
590-
# the caller should call #finish to close the session.
591-
#
592-
# Other arguments:
593-
#
594-
# - +port+: Server port number.
595-
# - +p_addr+: Proxy address.
596-
# - +p_port+: Proxy port.
597-
# - +p_user+: Proxy user name.
598-
# - +p_pass+: Proxy password.
599-
# - +opts+: Optional options hash.
600-
#
601-
# The options hash +opts+ sets certain values,
643+
# If the last argument given is a hash, it is the +opts+ hash,
602644
# where each key is a method or accessor to be called,
603645
# and its value is the value to be set.
604646
#
@@ -649,25 +691,113 @@ class << HTTP
649691
alias newobj new # :nodoc:
650692
end
651693

652-
# Creates a new Net::HTTP object without opening a TCP connection or
653-
# HTTP session.
694+
# Returns a new Net::HTTP object +http+
695+
# (but does not open a TCP connection or HTTP session).
696+
#
697+
# <b>No Proxy</b>
698+
#
699+
# With only string argument +hostname+ given
700+
# (and <tt>ENV['http_proxy']</tt> undefined or +nil+),
701+
# the returned +http+:
702+
#
703+
# - Has the given address.
704+
# - Has the default port number, Net::HTTP.default_port (80).
705+
# - Has no proxy.
706+
#
707+
# Example:
708+
#
709+
# http = Net::HTTP.new(hostname)
710+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
711+
# http.address # => "jsonplaceholder.typicode.com"
712+
# http.port # => 80
713+
# http.proxy? # => false
714+
#
715+
# With integer argument +port+ also given,
716+
# the returned +http+ has the given port:
717+
#
718+
# http = Net::HTTP.new(hostname, 8000)
719+
# # => #<Net::HTTP jsonplaceholder.typicode.com:8000 open=false>
720+
# http.port # => 8000
721+
#
722+
# <b>Proxy Using Argument +p_addr+ as a \String</b>
723+
#
724+
# When argument +p_addr+ is a string hostname,
725+
# the returned +http+ has a proxy:
726+
#
727+
# http = Net::HTTP.new(hostname, nil, 'proxy.example')
728+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
729+
# http.proxy? # => true
730+
# http.proxy_address # => "proxy.example"
731+
# # These use default values.
732+
# http.proxy_port # => 80
733+
# http.proxy_user # => nil
734+
# http.proxy_pass # => nil
735+
#
736+
# The port, username, and password for the proxy may also be given:
737+
#
738+
# http = Net::HTTP.new(hostname, nil, 'proxy.example', 8000, 'pname', 'ppass')
739+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
740+
# http.proxy? # => true
741+
# http.proxy_address # => "proxy.example"
742+
# http.proxy_port # => 8000
743+
# http.proxy_user # => "pname"
744+
# http.proxy_pass # => "ppass"
745+
#
746+
# <b>Proxy Using <tt>ENV['http_proxy']</tt></b>
747+
#
748+
# When environment variable <tt>'http_proxy'</tt>
749+
# is set to a \URI string,
750+
# the returned +http+ will have that URI as its proxy;
751+
# note that the \URI string must have a protocol
752+
# such as <tt>'http'</tt> or <tt>'https'</tt>:
753+
#
754+
# ENV['http_proxy'] = 'http://example.com'
755+
# # => "http://example.com"
756+
# http = Net::HTTP.new(hostname)
757+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
758+
# http.proxy? # => true
759+
# http.address # => "jsonplaceholder.typicode.com"
760+
# http.proxy_address # => "example.com"
761+
#
762+
# The \URI string may include proxy username, password, and port number:
763+
#
764+
# ENV['http_proxy'] = 'http://pname:[email protected]:8000'
765+
# # => "http://pname:[email protected]:8000"
766+
# http = Net::HTTP.new(hostname)
767+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
768+
# http.proxy_port # => 8000
769+
# http.proxy_user # => "pname"
770+
# http.proxy_pass # => "ppass"
771+
#
772+
# <b>Argument +p_no_proxy+</b>
773+
#
774+
# You can use argument +p_no_proxy+ to reject certain proxies:
775+
#
776+
# - Reject a certain address:
777+
#
778+
# http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example')
779+
# http.proxy_address # => nil
780+
#
781+
# - Reject certain domains or subdomains:
782+
#
783+
# http = Net::HTTP.new('example.com', nil, 'my.proxy.example', 8000, 'pname', 'ppass', 'proxy.example')
784+
# http.proxy_address # => nil
785+
#
786+
# - Reject certain addresses and port combinations:
787+
#
788+
# http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:1234')
789+
# http.proxy_address # => "proxy.example"
790+
#
791+
# http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:8000')
792+
# http.proxy_address # => nil
654793
#
655-
# The +address+ should be a DNS hostname or IP address, the +port+ is the
656-
# port the server operates on. If no +port+ is given the default port for
657-
# HTTP or HTTPS is used.
794+
# - Reject a list of the types above delimited using a comma:
658795
#
659-
# If none of the +p_+ arguments are given, the proxy host and port are
660-
# taken from the +http_proxy+ environment variable (or its uppercase
661-
# equivalent) if present. If the proxy requires authentication you must
662-
# supply it by hand. See URI::Generic#find_proxy for details of proxy
663-
# detection from the environment. To disable proxy detection set +p_addr+
664-
# to nil.
796+
# http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000')
797+
# http.proxy_address # => nil
665798
#
666-
# If you are connecting to a custom proxy, +p_addr+ specifies the DNS name
667-
# or IP address of the proxy host, +p_port+ the port to use to access the
668-
# proxy, +p_user+ and +p_pass+ the username and password if authorization
669-
# is required to use the proxy, and p_no_proxy hosts which do not
670-
# use the proxy.
799+
# http = Net::HTTP.new('example.com', nil, 'my.proxy', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000')
800+
# http.proxy_address # => nil
671801
#
672802
def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil)
673803
http = super address, port
@@ -1161,7 +1291,7 @@ def do_finish
11611291
#
11621292
# This class is obsolete. You may pass these same parameters directly to
11631293
# Net::HTTP.new. See Net::HTTP.new for details of the arguments.
1164-
def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil)
1294+
def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil) #:nodoc:
11651295
return self unless p_addr
11661296

11671297
Class.new(self) {

0 commit comments

Comments
 (0)