[ruby/net-http] [DOC] Enhanced RDoc for Net::HTTPHeader

(https://github.com/ruby/net-http/pull/83)

https://github.com/ruby/net-http/commit/1ea5004098
This commit is contained in:
Burdette Lamar 2022-12-01 17:17:31 -06:00 committed by git
parent 3d272b0fc8
commit 171e94bd95

View File

@ -126,38 +126,49 @@
# Various convenience methods retrieve values, set values, query values, # Various convenience methods retrieve values, set values, query values,
# set form values, or iterate over fields. # set form values, or iterate over fields.
# #
# === Getters
#
# - #[]: Returns the string value for the given field.
# - #content_length: Returns the integer value of field <tt>'Content-Length'</tt>.
# - #content_range: Returns the Range value of field <tt>'Content-Range'</tt>.
# - #content_type: Returns the string value of field <tt>'Content-Type'</tt>.
# - #main_type: Returns first part of the string value of field <tt>'Content-Type'</tt>.
# - #sub_type: Returns second part of the string value of field <tt>'Content-Type'</tt>.
# - #range: Returns an array of Range objects of field <tt>'Range'</tt>, or +nil+.
# - #range_length: Returns the integer length of the range given in field <tt>'Content-Range'</tt>.
# - #type_params: Returns the string parameters for <tt>'Content-Type'</tt>.
#
# === Setters # === Setters
# #
# - #[]=: Sets the string or array value for the given field. # \Method #[]= can set any field, but does little to validate the new value;
# some of the other setter methods provide some validation:
#
# - #[]=: Sets the string or array value for the given key.
# - #add_field: Creates or adds to the array value for the given key.
# - #basic_auth: Sets the string authorization header for <tt>'Authorization'</tt>. # - #basic_auth: Sets the string authorization header for <tt>'Authorization'</tt>.
# - #content_length=: Sets the integer length for field <tt>'Content-Length</tt>. # - #content_length=: Sets the integer length for field <tt>'Content-Length</tt>.
# - #content_type=: Sets the string value for field <tt>'Content-Type'</tt>. # - #content_type=: Sets the string value for field <tt>'Content-Type'</tt>.
# - #proxy_basic_auth: Sets the string authorization header for <tt>'Proxy-Authorization'</tt>. # - #proxy_basic_auth: Sets the string authorization header for <tt>'Proxy-Authorization'</tt>.
# - #set_range: Sets the value for field <tt>'Range'</tt>. # - #set_range: Sets the value for field <tt>'Range'</tt>.
# #
# === Form Setters
#
# - #set_form: Sets an HTML form data set.
# - #set_form_data: Sets header fields and a body from HTML form data.
#
# === Getters
#
# \Method #[] can retrieve the value of any field that exists,
# but always as a string;
# some of the other getter methods return something different
# from the simple string value:
#
# - #[]: Returns the string field value for the given key.
# - #content_length: Returns the integer value of field <tt>'Content-Length'</tt>.
# - #content_range: Returns the Range value of field <tt>'Content-Range'</tt>.
# - #content_type: Returns the string value of field <tt>'Content-Type'</tt>.
# - #fetch: Returns the string field value for the given key.
# - #get_fields: Returns the array field value for the given +key+.
# - #main_type: Returns first part of the string value of field <tt>'Content-Type'</tt>.
# - #sub_type: Returns second part of the string value of field <tt>'Content-Type'</tt>.
# - #range: Returns an array of Range objects of field <tt>'Range'</tt>, or +nil+.
# - #range_length: Returns the integer length of the range given in field <tt>'Content-Range'</tt>.
# - #type_params: Returns the string parameters for <tt>'Content-Type'</tt>.
#
# === Queries # === Queries
# #
# - #chunked?: Returns whether field <tt>'Transfer-Encoding'</tt> is set to <tt>'chunked'</tt>. # - #chunked?: Returns whether field <tt>'Transfer-Encoding'</tt> is set to <tt>'chunked'</tt>.
# - #connection_close?: Returns whether field <tt>'Connection'</tt> is set to <tt>'close'</tt>. # - #connection_close?: Returns whether field <tt>'Connection'</tt> is set to <tt>'close'</tt>.
# - #connection_keep_alive?: Returns whether field <tt>'Connection'</tt> is set to <tt>'keep-alive'</tt>. # - #connection_keep_alive?: Returns whether field <tt>'Connection'</tt> is set to <tt>'keep-alive'</tt>.
# - #key?: Returns whether a given field exists. # - #key?: Returns whether a given key exists.
#
# === Form Setters
#
# - #set_form: Sets an HTML form data set.
# - #set_form_data: Sets header fields and a body from HTML form data.
# #
# === Iterators # === Iterators
# #
@ -165,7 +176,7 @@
# - #each_capitalized_name: Passes each capitalized field name to the block. # - #each_capitalized_name: Passes each capitalized field name to the block.
# - #each_header: Passes each field name/value pair to the block. # - #each_header: Passes each field name/value pair to the block.
# - #each_name: Passes each field name to the block. # - #each_name: Passes each field name to the block.
# - #each_value: Passes each field value to the block. # - #each_value: Passes each string field value to the block.
# #
module Net::HTTPHeader module Net::HTTPHeader
@ -196,12 +207,14 @@ module Net::HTTPHeader
# or +nil+ if there is no such key; # or +nil+ if there is no such key;
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]: # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
# #
# req = Net::HTTP::Get.new(uri) # res = Net::HTTP.start(hostname) do |http|
# req['Accept'] # => "*/*" # http.get('/todos/1')
# req['Foo'] = %w[bar baz bat] # end
# req['Foo'] # => "bar, baz, bat" # res['Connection'] # => "keep-alive"
# res['Nosuch'] # => nil # res['Nosuch'] # => nil
# #
# Note that some field values may be retrieved via convenience methods;
# see {Getters}[rdoc-ref:Net::HTTPHeader@Getters].
def [](key) def [](key)
a = @header[key.downcase.to_s] or return nil a = @header[key.downcase.to_s] or return nil
a.join(', ') a.join(', ')
@ -216,6 +229,8 @@ module Net::HTTPHeader
# req['Accept'] = 'text/html' # req['Accept'] = 'text/html'
# req['Accept'] # => "text/html" # req['Accept'] # => "text/html"
# #
# Note that some field values may be set via convenience methods;
# see {Setters}[rdoc-ref:Net::HTTPHeader@Setters].
def []=(key, val) def []=(key, val)
unless val unless val
@header.delete key.downcase.to_s @header.delete key.downcase.to_s
@ -278,12 +293,11 @@ module Net::HTTPHeader
# or +nil+ if there is no such field; # or +nil+ if there is no such field;
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]: # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
# #
# req = Net::HTTP::Get.new(uri) # res = Net::HTTP.start(hostname) do |http|
# req['Foo'] = 'bar' # http.get('/todos/1')
# req.get_fields('Foo') # => ["bar"] # end
# req.add_field('Foo', 'baz') # res.get_fields('Connection') # => ["keep-alive"]
# req.get_fields('Foo') # => ["bar", "baz"] # res.get_fields('Nosuch') # => nil
# req.get_fields('Nosuch') # => nil
# #
def get_fields(key) def get_fields(key)
stringified_downcased_key = key.downcase.to_s stringified_downcased_key = key.downcase.to_s
@ -300,18 +314,27 @@ module Net::HTTPHeader
# ignores the +default_val+; # ignores the +default_val+;
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]: # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
# #
# req = Net::HTTP::Get.new(uri) # res = Net::HTTP.start(hostname) do |http|
# req['Foo'] = 'bar' # http.get('/todos/1')
# req.fetch('Foo') {|key| key.capitalize } # => "bar" # end
# req.fetch('Nosuch') {|key| key.capitalize } # => "Nosuch" #
# # Field exists; block not called.
# res.fetch('Connection') do |value|
# fail 'Cannot happen'
# end # => "keep-alive"
#
# # Field does not exist; block called.
# res.fetch('Nosuch') do |value|
# value.downcase
# end # => "nosuch"
# #
# With no block, returns the string value for +key+ if it exists; # With no block, returns the string value for +key+ if it exists;
# otherwise, returns +default_val+ if it was given; # otherwise, returns +default_val+ if it was given;
# otherwise raises an exception: # otherwise raises an exception:
# #
# req.fetch('Foo') # => "bar" # res.fetch('Connection', 'Foo') # => "keep-alive"
# req.fetch('Nosuch', :baz) # => :baz # res.fetch('Nosuch', 'Foo') # => "Foo"
# req.fetch('Nosuch') # Raises KeyError. # res.fetch('Nosuch') # Raises KeyError.
# #
def fetch(key, *args, &block) #:yield: +key+ def fetch(key, *args, &block) #:yield: +key+
a = @header.fetch(key.downcase.to_s, *args, &block) a = @header.fetch(key.downcase.to_s, *args, &block)
@ -320,15 +343,20 @@ module Net::HTTPHeader
# Calls the block with each key/value pair: # Calls the block with each key/value pair:
# #
# req = Net::HTTP::Get.new(uri) # res = Net::HTTP.start(hostname) do |http|
# req.each_header {|key, value| p [key, value] } # http.get('/todos/1')
# end
# res.each_header do |key, value|
# p [key, value] if key.start_with?('c')
# end
# #
# Output: # Output:
# #
# ["accept-encoding", "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"] # ["content-type", "application/json; charset=utf-8"]
# ["accept", "*/*"] # ["connection", "keep-alive"]
# ["user-agent", "Ruby"] # ["cache-control", "max-age=43200"]
# ["host", "jsonplaceholder.typicode.com"] # ["cf-cache-status", "HIT"]
# ["cf-ray", "771d17e9bc542cf5-ORD"]
# #
# Returns an enumerator if no block is given. # Returns an enumerator if no block is given.
# #
@ -344,15 +372,20 @@ module Net::HTTPHeader
# Calls the block with each field key: # Calls the block with each field key:
# #
# req = Net::HTTP::Get.new(uri) # res = Net::HTTP.start(hostname) do |http|
# req.each_key {|key| p key } # http.get('/todos/1')
# end
# res.each_key do |key|
# p key if key.start_with?('c')
# end
# #
# Output: # Output:
# #
# "accept-encoding" # "content-type"
# "accept" # "connection"
# "user-agent" # "cache-control"
# "host" # "cf-cache-status"
# "cf-ray"
# #
# Returns an enumerator if no block is given. # Returns an enumerator if no block is given.
# #
@ -366,15 +399,20 @@ module Net::HTTPHeader
# Calls the block with each capitalized field name: # Calls the block with each capitalized field name:
# #
# req = Net::HTTP::Get.new(uri) # res = Net::HTTP.start(hostname) do |http|
# req.each_capitalized_name {|key| p key } # http.get('/todos/1')
# end
# res.each_capitalized_name do |key|
# p key if key.start_with?('C')
# end
# #
# Output: # Output:
# #
# "Accept-Encoding" # "Content-Type"
# "Accept" # "Connection"
# "User-Agent" # "Cache-Control"
# "Host" # "Cf-Cache-Status"
# "Cf-Ray"
# #
# The capitalization is system-dependent; # The capitalization is system-dependent;
# see {Case Mapping}[rdoc-ref:case_mapping.rdoc]. # see {Case Mapping}[rdoc-ref:case_mapping.rdoc].
@ -387,17 +425,20 @@ module Net::HTTPHeader
end end
end end
# Calls the block with each field value: # Calls the block with each string field value:
# #
# req = Net::HTTP::Get.new(uri) # res = Net::HTTP.start(hostname) do |http|
# req.each_value {|value| p value } # http.get('/todos/1')
# end
# res.each_value do |value|
# p value if value.start_with?('c')
# end
# #
# Output: # Output:
# #
# "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" # "chunked"
# "*/*" # "cf-q-config;dur=6.0000002122251e-06"
# "Ruby" # "cloudflare"
# "jsonplaceholder.typicode.com"
# #
# Returns an enumerator if no block is given. # Returns an enumerator if no block is given.
def each_value #:yield: +value+ def each_value #:yield: +value+
@ -465,6 +506,7 @@ module Net::HTTPHeader
# or +nil+ if there is no such field; # or +nil+ if there is no such field;
# see {Range request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#range-request-header]: # see {Range request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#range-request-header]:
# #
# req = Net::HTTP::Get.new(uri)
# req['Range'] = 'bytes=0-99,200-299,400-499' # req['Range'] = 'bytes=0-99,200-299,400-499'
# req.range # => [0..99, 200..299, 400..499] # req.range # => [0..99, 200..299, 400..499]
# req.delete('Range') # req.delete('Range')
@ -522,6 +564,7 @@ module Net::HTTPHeader
# #
# With argument +length+: # With argument +length+:
# #
# req = Net::HTTP::Get.new(uri)
# req.set_range(100) # => 100 # req.set_range(100) # => 100
# req['Range'] # => "bytes=0-99" # req['Range'] # => "bytes=0-99"
# #