Skip to content

Commit 77c6878

Browse files
[DOC] Enhanced RDoc for Net::HTTPHeader
1 parent 9aa9f42 commit 77c6878

File tree

1 file changed

+58
-17
lines changed

1 file changed

+58
-17
lines changed

lib/net/http/header.rb

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
# frozen_string_literal: false
22
#
33
# The \HTTPHeader module provides access to \HTTP headers.
4-
# The headers are a hash-like collection of key/value pairs called _fields_.
54
#
65
# The module is included in:
76
#
87
# - Net::HTTPGenericRequest (and therefore Net::HTTPRequest).
98
# - Net::HTTPResponse.
109
#
10+
# The headers are a hash-like collection of key/value pairs called _fields_.
11+
#
12+
# == Request and Response Fields
13+
#
14+
# Headers may be included in:
15+
#
16+
# - A Net::HTTPRequest object:
17+
# the object's headers will be sent with the request.
18+
# Any fields may be defined in the request;
19+
# see {Setters}[rdoc-ref:Net::HTTPHeader@Setters].
20+
# - A Net::HTTPResponse object:
21+
# the objects headers are usually those returned from the host.
22+
# Fields may be retrieved from the object;
23+
# see {Getters}[rdoc-ref:Net::HTTPHeader@Getters]
24+
# and {Iterators}[rdoc-ref:Net::HTTPHeader@Iterators].
25+
#
26+
# Exactly which fields should be sent or expected depends on the host;
27+
# see:
28+
#
29+
# - {Request fields}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields].
30+
# - {Response fields}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Response_fields].
31+
#
1132
# == About the Examples
1233
#
1334
# :include: doc/net-http/examples.rdoc
@@ -366,8 +387,17 @@ def each_capitalized_name #:yield: +key+
366387
end
367388
end
368389

369-
# Iterates through header values, passing each value to the
370-
# code block.
390+
# Calls the block with each field value:
391+
#
392+
# req = Net::HTTP::Get.new(uri)
393+
# req.each_value {|value| p value }
394+
#
395+
# Output:
396+
#
397+
# "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
398+
# "*/*"
399+
# "Ruby"
400+
# "jsonplaceholder.typicode.com"
371401
#
372402
# Returns an enumerator if no block is given.
373403
def each_value #:yield: +value+
@@ -377,32 +407,43 @@ def each_value #:yield: +value+
377407
end
378408
end
379409

380-
# Removes a header field, specified by case-insensitive key.
410+
# Removes the header for the given case-insensitive +key+
411+
# (see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]);
412+
# returns the deleted value, or +nil+ if no such field exists:
413+
#
414+
# req = Net::HTTP::Get.new(uri)
415+
# req.delete('Accept') # => ["*/*"]
416+
# req.delete('Nosuch') # => nil
417+
#
381418
def delete(key)
382419
@header.delete(key.downcase.to_s)
383420
end
384421

385-
# true if +key+ header exists.
422+
# Returns +true+ if the field for the case-insensitive +key+ exists, +false+ otherwise:
423+
#
424+
# req = Net::HTTP::Get.new(uri)
425+
# req.key?('Accept') # => true
426+
# req.key?('Nosuch') # => false
427+
#
386428
def key?(key)
387429
@header.key?(key.downcase.to_s)
388430
end
389431

390-
# Returns a Hash consisting of header names and array of values.
391-
# e.g.
392-
# {"cache-control" => ["private"],
393-
# "content-type" => ["text/html"],
394-
# "date" => ["Wed, 22 Jun 2005 22:11:50 GMT"]}
432+
# Returns a hash of the key/value pairs:
433+
#
434+
# req = Net::HTTP::Get.new(uri)
435+
# req.to_hash
436+
# # =>
437+
# {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
438+
# "accept"=>["*/*"],
439+
# "user-agent"=>["Ruby"],
440+
# "host"=>["jsonplaceholder.typicode.com"]}
441+
#
395442
def to_hash
396443
@header.dup
397444
end
398445

399-
# As for #each_header, except the keys are provided in capitalized form.
400-
#
401-
# Note that header names are capitalized systematically;
402-
# capitalization may not match that used by the remote HTTP
403-
# server in its response.
404-
#
405-
# Returns an enumerator if no block is given.
446+
# Like #each_header, but the keys are returned in capitalized form.
406447
#
407448
# Net::HTTPHeader#canonical_each is an alias for Net::HTTPHeader#each_capitalized.
408449
def each_capitalized

0 commit comments

Comments
 (0)