Skip to content

Commit 995ff35

Browse files
committed
Enhanced RDoc for Net::HTTPHeader
1 parent e924f12 commit 995ff35

File tree

1 file changed

+77
-66
lines changed

1 file changed

+77
-66
lines changed

lib/net/http/header.rb

Lines changed: 77 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,21 @@
2020
#
2121
# A field key may be:
2222
#
23-
# - A string: Key <tt>'Foo'</tt> is treated as if it were
24-
# <tt>'Foo'.downcase</tt>; i.e., <tt>'foo'</tt>.
25-
# - A symbol: Key <tt>:Foo</tt> is treated as if it were
26-
# <tt>:Foo.to_s.downcase</tt>; i.e., <tt>'foo'</tt>.
23+
# - A string: Key <tt>'Accept'</tt> is treated as if it were
24+
# <tt>'Accept'.downcase</tt>; i.e., <tt>'accept'</tt>.
25+
# - A symbol: Key <tt>:Accept</tt> is treated as if it were
26+
# <tt>:Accept.to_s.downcase</tt>; i.e., <tt>'accept'</tt>.
2727
#
2828
# Examples:
2929
#
30-
# req = Net::HTTP.get(uri)
31-
# req[:Accept] # => "*/*"
30+
# req = Net::HTTP::Get.new(uri)
31+
# req[:accept] # => "*/*"
3232
# req['Accept'] # => "*/*"
3333
# req['ACCEPT'] # => "*/*"
3434
#
3535
# req['accept'] = 'text/html'
36-
# req['accept'] # => "text/html"
37-
# req[:Accept] = 'text/html'
38-
# req['accept'] # => "text/html"
39-
# req['Accept'] = 'application/json'
40-
# req['accept'] # => "application/json"
41-
# req['ACCEPT'] = 'text/plain'
42-
# req['accept'] # => "text/plain"
36+
# req[:accept] = 'text/html'
37+
# req['ACCEPT'] = 'text/html'
4338
#
4439
# === Field Values
4540
#
@@ -73,15 +68,15 @@
7368
#
7469
# - \String:
7570
#
76-
# req[:foo] = 'bar'
77-
# req[:foo] # => "bar"
78-
# req.get_fields(:foo) # => ["bar"]
71+
# req['Accept'] = 'text/html' # => "text/html"
72+
# req['Accept'] # => "text/html"
73+
# req.get_fields('Accept') # => ["text/html"]
7974
#
8075
# - \Symbol:
8176
#
82-
# req[:foo] = :bar
83-
# req[:foo] # => "bar"
84-
# req.get_fields(:foo) # => ["bar"]
77+
# req['Accept'] = :text # => :text
78+
# req['Accept'] # => "text"
79+
# req.get_fields('Accept') # => ["text"]
8580
#
8681
# - Simple array:
8782
#
@@ -113,35 +108,35 @@
113108
# === Getters
114109
#
115110
# - #[]: Returns the string value for the given field.
116-
# - #content_length: Returns the integer value of field +:content-length+.
117-
# - #content_range: Returns the Range value of field +:content-range+.
118-
# - #content_type: Returns the string value of field +:content-type+.
119-
# - #main_type: Returns first part of the string value of field +:content-type+.
120-
# - #sub_type: Returns second part of the string value of field +:content-type+.
121-
# - #range: Returns an array of Range objects, or +nil+.
122-
# - #range_length: Returns the integer length of the range given in field +:range+.
123-
# - #type_params: Returns the string parameters for +:content-type+.
111+
# - #content_length: Returns the integer value of field <tt>'Content-Length'</tt>.
112+
# - #content_range: Returns the Range value of field <tt>'Content-Range'</tt>.
113+
# - #content_type: Returns the string value of field <tt>'Content-Type'</tt>.
114+
# - #main_type: Returns first part of the string value of field <tt>'Content-Type'</tt>.
115+
# - #sub_type: Returns second part of the string value of field <tt>'Content-Type'</tt>.
116+
# - #range: Returns an array of Range objects of field <tt>'Range'</tt>, or +nil+.
117+
# - #range_length: Returns the integer length of the range given in field <tt>'Content-Range'</tt>.
118+
# - #type_params: Returns the string parameters for <tt>'Content-Type'</tt>.
124119
#
125120
# === Setters
126121
#
127122
# - #[]=: Sets the string or array value for the given field.
128-
# - #basic_auth: Sets the string authorization header for +:Basic+ authorization.
129-
# - #content_length=: Sets the integer length for field +:content-length+.
130-
# - #content_type=: Sets the string value for field +:content-type+.
131-
# - #proxy_basic_auth: Set Proxy-Authorization: header for “Basic” authorization.
132-
# - #range=: Sets the HTTP Range: header. Accepts either a Range object as a single argument, or a beginning index and a length from that index. Example:
123+
# - #basic_auth: Sets the string authorization header for <tt>'Authorization'</tt>.
124+
# - #content_length=: Sets the integer length for field <tt>'Content-Length</tt>.
125+
# - #content_type=: Sets the string value for field <tt>'Content-Type'</tt>.
126+
# - #proxy_basic_auth: Sets the string authorization header for <tt>'Proxy-Authorization'</tt>.
127+
# - #range=: Sets the value for field +'Range'+.
133128
#
134129
# === Queries
135130
#
136-
# - #chunked?: Returns whether field +:transfer-encoding+ is set to <tt>'chunked'</tt>.
137-
# - #connection_close?: Returns whether field +:connection+ is set to <tt>'close'</tt>.
138-
# - #connection_keep_alive?: Returns whether field +:connection+ is set to <tt>'keep-alive'</tt>.
131+
# - #chunked?: Returns whether field <tt>'Transfer-Encoding'</tt> is set to <tt>'chunked'</tt>.
132+
# - #connection_close?: Returns whether field <tt>'Connection'</tt> is set to <tt>'close'</tt>.
133+
# - #connection_keep_alive?: Returns whether field <tt>'Connection'</tt> is set to <tt>'keep-alive'</tt>.
139134
# - #key?: Returns whether a given field exists.
140135
#
141136
# === Form Setters
142137
#
143138
# - #set_form: Sets an HTML form data set.
144-
# - #set_form_data: Set header fields and a body from HTML form data.
139+
# - #set_form_data: Sets header fields and a body from HTML form data.
145140
#
146141
# === Iterators
147142
#
@@ -151,6 +146,15 @@
151146
# - #each_name: Passes each field name to the block.
152147
# - #each_value: Passes each field value to the block.
153148
#
149+
# == Hash of Headers
150+
#
151+
# Each of the iterator methods above returns a hash of the headers of +self+.
152+
# Note that modifying that returned hash actually modifies the headers:
153+
#
154+
# h = req.each_header {|k, v| }
155+
# h.clear
156+
# req.to_hash # => {}
157+
#
154158
module Net::HTTPHeader
155159

156160
def initialize_http_header(initheader) #:nodoc:
@@ -181,10 +185,10 @@ def size #:nodoc: obsolete
181185
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
182186
#
183187
# req = Net::HTTP::Get.new(uri)
184-
# req[:accept] # => "*/*"
185-
# req[:foo] = %w[bar baz bat]
186-
# req[:foo] # => "bar, baz, bat"
187-
# res[:nosuch] # => nil
188+
# req['Accept'] # => "*/*"
189+
# req['Foo'] = %w[bar baz bat]
190+
# req['Foo'] # => "bar, baz, bat"
191+
# res['Nosuch'] # => nil
188192
#
189193
def [](key)
190194
a = @header[key.downcase.to_s] or return nil
@@ -196,9 +200,9 @@ def [](key)
196200
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
197201
#
198202
# req = Net::HTTP::Get.new(uri)
199-
# req[:accept] # => "*/*"
200-
# req[:accept] = 'text/html'
201-
# req[:accept] # => "text/html"
203+
# req['Accept'] # => "*/*"
204+
# req['Accept'] = 'text/html'
205+
# req['Accept'] # => "text/html"
202206
#
203207
def []=(key, val)
204208
unless val
@@ -213,13 +217,13 @@ def []=(key, val)
213217
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
214218
#
215219
# req = Net::HTTP::Get.new(uri)
216-
# req.add_field(:foo, 'bar')
217-
# req[:foo] # => "bar"
218-
# req.add_field(:foo, 'baz')
219-
# req[:foo] # => "bar, baz"
220-
# req.add_field(:foo, %w[baz bam])
221-
# req[:foo] # => "bar, baz, baz, bam"
222-
# req.get_fields(:foo) # => ["bar", "baz", "baz", "bam"]
220+
# req.add_field('Foo', 'bar')
221+
# req['Foo'] # => "bar"
222+
# req.add_field('Foo', 'baz')
223+
# req['Foo'] # => "bar, baz"
224+
# req.add_field('Foo', %w[baz bam])
225+
# req['Foo'] # => "bar, baz, baz, bam"
226+
# req.get_fields('Foo') # => ["bar", "baz", "baz", "bam"]
223227
#
224228
def add_field(key, val)
225229
stringified_downcased_key = key.downcase.to_s
@@ -263,11 +267,11 @@ def add_field(key, val)
263267
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
264268
#
265269
# req = Net::HTTP::Get.new(uri)
266-
# req[:foo] = 'bar'
267-
# req.get_fields(:foo) # => ["bar"]
268-
# req.add_field(:foo, 'baz')
269-
# req.get_fields(:foo) # => ["bar", "baz"]
270-
# req.get_fields(:nosuch) # => nil
270+
# req['Foo'] = 'bar'
271+
# req.get_fields('Foo') # => ["bar"]
272+
# req.add_field('Foo', 'baz')
273+
# req.get_fields('Foo') # => ["bar", "baz"]
274+
# req.get_fields('Nosuch') # => nil
271275
#
272276
def get_fields(key)
273277
stringified_downcased_key = key.downcase.to_s
@@ -283,25 +287,25 @@ def get_fields(key)
283287
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
284288
#
285289
# req = Net::HTTP::Get.new(uri)
286-
# req[:foo] = 'bar'
287-
# req.fetch(:foo) {|key| key.capitalize } # => "bar"
288-
# req.fetch(:nosuch) {|key| key.capitalize } # => "Nosuch"
290+
# req['Foo'] = 'bar'
291+
# req.fetch('Foo') {|key| key.capitalize } # => "bar"
292+
# req.fetch('Nosuch') {|key| key.capitalize } # => "Nosuch"
289293
#
290294
# With no block, returns the string value for +key+ if it exists;
291295
# otherwise, returns +default_val+ if it was given;
292296
# otherwise raises an exception:
293297
#
294-
# req.fetch(:foo) # => "bar"
295-
# req.fetch(:nosuch, :baz) # => :baz
296-
# req.fetch(:nosuch) # Raises IndexError.
298+
# req.fetch('Foo') # => "bar"
299+
# req.fetch('Nosuch', :baz) # => :baz
300+
# req.fetch('Nosuch') # Raises KeyError.
297301
#
298302
def fetch(key, *args, &block) #:yield: +key+
299303
a = @header.fetch(key.downcase.to_s, *args, &block)
300304
a.kind_of?(Array) ? a.join(', ') : a
301305
end
302306

303307
# Calls the block with each key/value pair;
304-
# returns the value of #to_hash;
308+
# returns the {hash of headers}[rdoc-ref:Net::HTTPHeader@Hash+of+Headers];
305309
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
306310
#
307311
# req = Net::HTTP::Get.new(uri)
@@ -315,6 +319,8 @@ def fetch(key, *args, &block) #:yield: +key+
315319
# ["host", "jsonplaceholder.typicode.com"]
316320
#
317321
# Returns an enumerator if no block is given.
322+
#
323+
# Net::HTTPHeader#each is an alias for Net::HTTPHeader#each_header.
318324
def each_header #:yield: +key+, +value+
319325
block_given? or return enum_for(__method__) { @header.size }
320326
@header.each do |k,va|
@@ -325,7 +331,7 @@ def each_header #:yield: +key+, +value+
325331
alias each each_header
326332

327333
# Calls the block with each field key;
328-
# returns the value of #to_hash;
334+
# returns the {hash of headers}[rdoc-ref:Net::HTTPHeader@Hash+of+Headers];
329335
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
330336
#
331337
# req = Net::HTTP::Get.new(uri)
@@ -349,7 +355,7 @@ def each_name(&block) #:yield: +key+
349355
alias each_key each_name
350356

351357
# Calls the block with each capitalized field name;
352-
# returns the value of #to_hash;
358+
# returns the {hash of headers}[rdoc-ref:Net::HTTPHeader@Hash+of+Headers];
353359
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
354360
#
355361
# req = Net::HTTP::Get.new(uri)
@@ -410,6 +416,8 @@ def to_hash
410416
# server in its response.
411417
#
412418
# Returns an enumerator if no block is given.
419+
#
420+
# Net::HTTPHeader#canonical_each is an alias for Net::HTTPHeader#each_capitalized.
413421
def each_capitalized
414422
block_given? or return enum_for(__method__) { @header.size }
415423
@header.each do |k,v|
@@ -505,7 +513,7 @@ def set_range(r, e = nil)
505513
r
506514
end
507515

508-
alias range= set_range
516+
n range= set_range
509517

510518
# Returns an Integer object which represents the HTTP Content-Length:
511519
# header field, or +nil+ if that field was not provided.
@@ -596,7 +604,9 @@ def type_params
596604
# Sets the content type in an HTTP header.
597605
# The +type+ should be a full HTTP content type, e.g. "text/html".
598606
# The +params+ are an optional Hash of parameters to add after the
599-
# content type, e.g. {'charset' => 'iso-8859-1'}
607+
# content type, e.g. {'charset' => 'iso-8859-1'}.
608+
#
609+
# Net::HTTPHeader#content_type= is an alias for Net::HTTPHeader#set_content_type.
600610
def set_content_type(type, params = {})
601611
@header['content-type'] = [type + params.map{|k,v|"; #{k}=#{v}"}.join('')]
602612
end
@@ -616,6 +626,7 @@ def set_content_type(type, params = {})
616626
# http.form_data = {"q" => ["ruby", "perl"], "lang" => "en"}
617627
# http.set_form_data({"q" => "ruby", "lang" => "en"}, ';')
618628
#
629+
# Net::HTTPHeader#form_data= is an alias for Net::HTTPHeader#set_form_data.
619630
def set_form_data(params, sep = '&')
620631
query = URI.encode_www_form(params)
621632
query.gsub!(/&/, sep) if sep != '&'

0 commit comments

Comments
 (0)