@@ -207,9 +207,7 @@ def size #:nodoc: obsolete
207207 # or +nil+ if there is no such key;
208208 # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
209209 #
210- # res = Net::HTTP.start(hostname) do |http|
211- # http.get('/todos/1')
212- # end
210+ # res = Net::HTTP.get_response(hostname, '/todos/1')
213211 # res['Connection'] # => "keep-alive"
214212 # res['Nosuch'] # => nil
215213 #
@@ -293,9 +291,7 @@ def add_field(key, val)
293291 # or +nil+ if there is no such field;
294292 # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
295293 #
296- # res = Net::HTTP.start(hostname) do |http|
297- # http.get('/todos/1')
298- # end
294+ # res = Net::HTTP.get_response(hostname, '/todos/1')
299295 # res.get_fields('Connection') # => ["keep-alive"]
300296 # res.get_fields('Nosuch') # => nil
301297 #
@@ -314,9 +310,7 @@ def get_fields(key)
314310 # ignores the +default_val+;
315311 # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
316312 #
317- # res = Net::HTTP.start(hostname) do |http|
318- # http.get('/todos/1')
319- # end
313+ # res = Net::HTTP.get_response(hostname, '/todos/1')
320314 #
321315 # # Field exists; block not called.
322316 # res.fetch('Connection') do |value|
@@ -343,9 +337,7 @@ def fetch(key, *args, &block) #:yield: +key+
343337
344338 # Calls the block with each key/value pair:
345339 #
346- # res = Net::HTTP.start(hostname) do |http|
347- # http.get('/todos/1')
348- # end
340+ # res = Net::HTTP.get_response(hostname, '/todos/1')
349341 # res.each_header do |key, value|
350342 # p [key, value] if key.start_with?('c')
351343 # end
@@ -372,20 +364,18 @@ def each_header #:yield: +key+, +value+
372364
373365 # Calls the block with each field key:
374366 #
375- # res = Net::HTTP.start(hostname) do |http|
376- # http.get('/todos/1')
377- # end
367+ # res = Net::HTTP.get_response(hostname, '/todos/1')
378368 # res.each_key do |key|
379369 # p key if key.start_with?('c')
380370 # end
381371 #
382372 # Output:
383373 #
384- # "content-type"
385- # "connection"
386- # "cache-control"
387- # "cf-cache-status"
388- # "cf-ray"
374+ # "content-type"
375+ # "connection"
376+ # "cache-control"
377+ # "cf-cache-status"
378+ # "cf-ray"
389379 #
390380 # Returns an enumerator if no block is given.
391381 #
@@ -399,9 +389,7 @@ def each_name(&block) #:yield: +key+
399389
400390 # Calls the block with each capitalized field name:
401391 #
402- # res = Net::HTTP.start(hostname) do |http|
403- # http.get('/todos/1')
404- # end
392+ # res = Net::HTTP.get_response(hostname, '/todos/1')
405393 # res.each_capitalized_name do |key|
406394 # p key if key.start_with?('C')
407395 # end
@@ -427,9 +415,7 @@ def each_capitalized_name #:yield: +key+
427415
428416 # Calls the block with each string field value:
429417 #
430- # res = Net::HTTP.start(hostname) do |http|
431- # http.get('/todos/1')
432- # end
418+ # res = Net::HTTP.get_response(hostname, '/todos/1')
433419 # res.each_value do |value|
434420 # p value if value.start_with?('c')
435421 # end
@@ -610,15 +596,36 @@ def set_range(r, e = nil)
610596
611597 alias range = set_range
612598
613- # Returns an Integer object which represents the HTTP Content-Length:
614- # header field, or +nil+ if that field was not provided.
599+ # Returns the value of field <tt>'Content-Length'</tt> as an integer,
600+ # or +nil+ if there is no such field;
601+ # see {Content-Length request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-length-request-header]:
602+ #
603+ # res = Net::HTTP.get_response(hostname, '/nosuch/1')
604+ # res.content_length # => 2
605+ # res = Net::HTTP.get_response(hostname, '/todos/1')
606+ # res.content_length # => nil
607+ #
615608 def content_length
616609 return nil unless key? ( 'Content-Length' )
617610 len = self [ 'Content-Length' ] . slice ( /\d +/ ) or
618611 raise Net ::HTTPHeaderSyntaxError , 'wrong Content-Length format'
619612 len . to_i
620613 end
621614
615+ # Sets the value of field <tt>'Content-Length'</tt> to the given numeric;
616+ # see {Content-Length response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-length-response-header]:
617+ #
618+ # _uri = uri.dup
619+ # hostname = _uri.hostname # => "jsonplaceholder.typicode.com"
620+ # _uri.path = '/posts' # => "/posts"
621+ # req = Net::HTTP::Post.new(_uri) # => #<Net::HTTP::Post POST>
622+ # req.body = '{"title": "foo","body": "bar","userId": 1}'
623+ # req.content_length = req.body.size # => 42
624+ # req.content_type = 'application/json'
625+ # res = Net::HTTP.start(hostname) do |http|
626+ # http.request(req)
627+ # end # => #<Net::HTTPCreated 201 Created readbody=true>
628+ #
622629 def content_length = ( len )
623630 unless len
624631 @header . delete 'content-length'
@@ -627,20 +634,31 @@ def content_length=(len)
627634 @header [ 'content-length' ] = [ len . to_i . to_s ]
628635 end
629636
630- # Returns "true" if the "transfer-encoding" header is present and
631- # set to "chunked". This is an HTTP/1.1 feature, allowing
632- # the content to be sent in "chunks" without at the outset
633- # stating the entire content length.
637+ # Returns +true+ if field <tt>'Transfer-Encoding'</tt>
638+ # exists and has value <tt>'chunked'</tt>,
639+ # +false+ otherwise;
640+ # see {Transfer-Encoding response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#transfer-encoding-response-header]:
641+ #
642+ # res = Net::HTTP.get_response(hostname, '/todos/1')
643+ # res['Transfer-Encoding'] # => "chunked"
644+ # res.chunked? # => true
645+ #
634646 def chunked?
635647 return false unless @header [ 'transfer-encoding' ]
636648 field = self [ 'Transfer-Encoding' ]
637649 ( /(?:\A |[^\- \w ])chunked(?![\- \w ])/i =~ field ) ? true : false
638650 end
639651
640- # Returns a Range object which represents the value of the Content-Range:
641- # header field.
642- # For a partial entity body, this indicates where this fragment
643- # fits inside the full entity body, as range of byte offsets.
652+ # Returns a Range object representing the value of field
653+ # <tt>'Content-Range'</tt>, or +nil+ if no such field exists;
654+ # see {Content-Range response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-range-response-header]:
655+ #
656+ # res = Net::HTTP.get_response(hostname, '/todos/1')
657+ # res['Content-Range'] # => nil
658+ # res['Content-Range'] = 'bytes 0-499/1000'
659+ # res['Content-Range'] # => "bytes 0-499/1000"
660+ # res.content_range # => 0..499
661+ #
644662 def content_range
645663 return nil unless @header [ 'content-range' ]
646664 m = %r<\A \s *(\w +)\s +(\d +)-(\d +)/(\d +|\* )> . match ( self [ 'Content-Range' ] ) or
@@ -649,14 +667,29 @@ def content_range
649667 m [ 2 ] . to_i .. m [ 3 ] . to_i
650668 end
651669
652- # The length of the range represented in Content-Range: header.
670+ # Returns the integer representing length of the value of field
671+ # <tt>'Content-Range'</tt>, or +nil+ if no such field exists;
672+ # see {Content-Range response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-range-response-header]:
673+ #
674+ # res = Net::HTTP.get_response(hostname, '/todos/1')
675+ # res['Content-Range'] # => nil
676+ # res['Content-Range'] = 'bytes 0-499/1000'
677+ # res.range_length # => 500
678+ #
653679 def range_length
654680 r = content_range ( ) or return nil
655681 r . end - r . begin + 1
656682 end
657683
658- # Returns a content type string such as "text/html".
659- # This method returns nil if Content-Type: header field does not exist.
684+ # Returns the {media type}[https://en.wikipedia.org/wiki/Media_type]
685+ # from the value of field <tt>'Content-Type'</tt>,
686+ # or +nil+ if no such field exists;
687+ # see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
688+ #
689+ # res = Net::HTTP.get_response(hostname, '/todos/1')
690+ # res['content-type'] # => "application/json; charset=utf-8"
691+ # res.content_type # => "application/json"
692+ #
660693 def content_type
661694 return nil unless main_type ( )
662695 if sub_type ( )
@@ -665,26 +698,46 @@ def content_type
665698 end
666699 end
667700
668- # Returns a content type string such as "text".
669- # This method returns nil if Content-Type: header field does not exist.
701+ # Returns the leading ('type') part of the
702+ # {media type}[https://en.wikipedia.org/wiki/Media_type]
703+ # from the value of field <tt>'Content-Type'</tt>,
704+ # or +nil+ if no such field exists;
705+ # see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
706+ #
707+ # res = Net::HTTP.get_response(hostname, '/todos/1')
708+ # res['content-type'] # => "application/json; charset=utf-8"
709+ # res.main_type # => "application"
710+ #
670711 def main_type
671712 return nil unless @header [ 'content-type' ]
672713 self [ 'Content-Type' ] . split ( ';' ) . first . to_s . split ( '/' ) [ 0 ] . to_s . strip
673714 end
674715
675- # Returns a content type string such as "html".
676- # This method returns nil if Content-Type: header field does not exist
677- # or sub-type is not given (e.g. "Content-Type: text").
716+ # Returns the trailing ('subtype') part of the
717+ # {media type}[https://en.wikipedia.org/wiki/Media_type]
718+ # from the value of field <tt>'Content-Type'</tt>,
719+ # or +nil+ if no such field exists;
720+ # see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
721+ #
722+ # res = Net::HTTP.get_response(hostname, '/todos/1')
723+ # res['content-type'] # => "application/json; charset=utf-8"
724+ # res.sub_type # => "json"
725+ #
678726 def sub_type
679727 return nil unless @header [ 'content-type' ]
680728 _ , sub = *self [ 'Content-Type' ] . split ( ';' ) . first . to_s . split ( '/' )
681729 return nil unless sub
682730 sub . strip
683731 end
684732
685- # Any parameters specified for the content type, returned as a Hash.
686- # For example, a header of Content-Type: text/html; charset=EUC-JP
687- # would result in type_params returning {'charset' => 'EUC-JP'}
733+ # Returns the trailing ('parameters') part of the value of field <tt>'Content-Type'</tt>,
734+ # or +nil+ if no such field exists;
735+ # see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
736+ #
737+ # res = Net::HTTP.get_response(hostname, '/todos/1')
738+ # res['content-type'] # => "application/json; charset=utf-8"
739+ # res.type_params # => {"charset"=>"utf-8"}
740+ #
688741 def type_params
689742 result = { }
690743 list = self [ 'Content-Type' ] . to_s . split ( ';' )
@@ -696,10 +749,12 @@ def type_params
696749 result
697750 end
698751
699- # Sets the content type in an HTTP header.
700- # The +type+ should be a full HTTP content type, e.g. "text/html".
701- # The +params+ are an optional Hash of parameters to add after the
702- # content type, e.g. {'charset' => 'iso-8859-1'}.
752+ # Sets the value of field <tt>'Content-Type'</tt>;
753+ # returns the new value;
754+ # see {Content-Type request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-request-header]:
755+ #
756+ # req = Net::HTTP::Get.new(uri)
757+ # req.set_content_type('application/json') # => ["application/json"]
703758 #
704759 # Net::HTTPHeader#content_type= is an alias for Net::HTTPHeader#set_content_type.
705760 def set_content_type ( type , params = { } )
0 commit comments