1
1
# frozen_string_literal: false
2
2
#
3
3
# The \HTTPHeader module provides access to \HTTP headers.
4
- # The headers are a hash-like collection of key/value pairs called _fields_.
5
4
#
6
5
# The module is included in:
7
6
#
8
7
# - Net::HTTPGenericRequest (and therefore Net::HTTPRequest).
9
8
# - Net::HTTPResponse.
10
9
#
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
+ #
11
32
# == About the Examples
12
33
#
13
34
# :include: doc/net-http/examples.rdoc
@@ -366,8 +387,17 @@ def each_capitalized_name #:yield: +key+
366
387
end
367
388
end
368
389
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"
371
401
#
372
402
# Returns an enumerator if no block is given.
373
403
def each_value #:yield: +value+
@@ -377,32 +407,43 @@ def each_value #:yield: +value+
377
407
end
378
408
end
379
409
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
+ #
381
418
def delete ( key )
382
419
@header . delete ( key . downcase . to_s )
383
420
end
384
421
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
+ #
386
428
def key? ( key )
387
429
@header . key? ( key . downcase . to_s )
388
430
end
389
431
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
+ #
395
442
def to_hash
396
443
@header . dup
397
444
end
398
445
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.
406
447
#
407
448
# Net::HTTPHeader#canonical_each is an alias for Net::HTTPHeader#each_capitalized.
408
449
def each_capitalized
0 commit comments