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

https://github.com/ruby/net-http/commit/77c6878970
This commit is contained in:
Burdette Lamar 2022-11-26 18:21:43 -06:00 committed by git
parent 73e42ef856
commit b55da1cd97

View File

@ -1,13 +1,34 @@
# frozen_string_literal: false # frozen_string_literal: false
# #
# The \HTTPHeader module provides access to \HTTP headers. # The \HTTPHeader module provides access to \HTTP headers.
# The headers are a hash-like collection of key/value pairs called _fields_.
# #
# The module is included in: # The module is included in:
# #
# - Net::HTTPGenericRequest (and therefore Net::HTTPRequest). # - Net::HTTPGenericRequest (and therefore Net::HTTPRequest).
# - Net::HTTPResponse. # - Net::HTTPResponse.
# #
# The headers are a hash-like collection of key/value pairs called _fields_.
#
# == Request and Response Fields
#
# Headers may be included in:
#
# - A Net::HTTPRequest object:
# the object's headers will be sent with the request.
# Any fields may be defined in the request;
# see {Setters}[rdoc-ref:Net::HTTPHeader@Setters].
# - A Net::HTTPResponse object:
# the objects headers are usually those returned from the host.
# Fields may be retrieved from the object;
# see {Getters}[rdoc-ref:Net::HTTPHeader@Getters]
# and {Iterators}[rdoc-ref:Net::HTTPHeader@Iterators].
#
# Exactly which fields should be sent or expected depends on the host;
# see:
#
# - {Request fields}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields].
# - {Response fields}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Response_fields].
#
# == About the Examples # == About the Examples
# #
# :include: doc/net-http/examples.rdoc # :include: doc/net-http/examples.rdoc
@ -366,8 +387,17 @@ module Net::HTTPHeader
end end
end end
# Iterates through header values, passing each value to the # Calls the block with each field value:
# code block. #
# req = Net::HTTP::Get.new(uri)
# req.each_value {|value| p value }
#
# Output:
#
# "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
# "*/*"
# "Ruby"
# "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+
@ -377,32 +407,43 @@ module Net::HTTPHeader
end end
end end
# Removes a header field, specified by case-insensitive key. # Removes the header for the given case-insensitive +key+
# (see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]);
# returns the deleted value, or +nil+ if no such field exists:
#
# req = Net::HTTP::Get.new(uri)
# req.delete('Accept') # => ["*/*"]
# req.delete('Nosuch') # => nil
#
def delete(key) def delete(key)
@header.delete(key.downcase.to_s) @header.delete(key.downcase.to_s)
end end
# true if +key+ header exists. # Returns +true+ if the field for the case-insensitive +key+ exists, +false+ otherwise:
#
# req = Net::HTTP::Get.new(uri)
# req.key?('Accept') # => true
# req.key?('Nosuch') # => false
#
def key?(key) def key?(key)
@header.key?(key.downcase.to_s) @header.key?(key.downcase.to_s)
end end
# Returns a Hash consisting of header names and array of values. # Returns a hash of the key/value pairs:
# e.g. #
# {"cache-control" => ["private"], # req = Net::HTTP::Get.new(uri)
# "content-type" => ["text/html"], # req.to_hash
# "date" => ["Wed, 22 Jun 2005 22:11:50 GMT"]} # # =>
# {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
# "accept"=>["*/*"],
# "user-agent"=>["Ruby"],
# "host"=>["jsonplaceholder.typicode.com"]}
#
def to_hash def to_hash
@header.dup @header.dup
end end
# As for #each_header, except the keys are provided in capitalized form. # Like #each_header, but the keys are returned in capitalized form.
#
# Note that header names are capitalized systematically;
# capitalization may not match that used by the remote HTTP
# server in its response.
#
# Returns an enumerator if no block is given.
# #
# Net::HTTPHeader#canonical_each is an alias for Net::HTTPHeader#each_capitalized. # Net::HTTPHeader#canonical_each is an alias for Net::HTTPHeader#each_capitalized.
def each_capitalized def each_capitalized