net-http 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76efd58d8b7892a8c7cd99b94ba163b3e7af2c7c213710d09d4f0ff5751fb01c
4
- data.tar.gz: 69b629454de4153eac43b49c1e622889a69a90b8b574be7a91af0668240c7b72
3
+ metadata.gz: d2201663415fa5809c53f6c1d8a06524739bcee246443e22175aa89cda45c92c
4
+ data.tar.gz: 971f571f8d9966a09baf43a5117d9f072ae78b890e6cc4991a958081728671c8
5
5
  SHA512:
6
- metadata.gz: bf1fe9f92f33a2b2c32ae4ca1c896076ab22df06551b7e669a21c616a2c89d36942a18a3520d3ecb3fd5db387481ac44abbd7c70dc8b39bce4987a681819ce1f
7
- data.tar.gz: 143b8dc3a5f8788c552e17ac45ee20ed0313e16bccb16de94d9a22d96b69a23e24d264c2978388b2b01e7f43d84834d027c65c901e3ed6d8fd668763eb422375
6
+ metadata.gz: '086e4def849a69d4ecad94e1a13fe35eee82453f8748be247ea2c133b40ae7d3b419a0f71fa9dcd7efe783d0b725a13d527e287da1ccc406f7daa5700f2ec8e5'
7
+ data.tar.gz: 47a0f2d46fe8a9bc328241fb0fc90ea1c4285b0e46460998e40650a930bd96743051911e62b790d4502b39a007a5a04e260affe42d584bb73442ab414b51f1e3
@@ -207,9 +207,7 @@ module Net::HTTPHeader
207
207
  # or +nil+ if there is no such key;
208
208
  # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
209
209
  #
210
- # res = Net::HTTP.start(hostname) do |http|
211
- # http.get('/todos/1')
212
- # end
210
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
213
211
  # res['Connection'] # => "keep-alive"
214
212
  # res['Nosuch'] # => nil
215
213
  #
@@ -293,9 +291,7 @@ module Net::HTTPHeader
293
291
  # or +nil+ if there is no such field;
294
292
  # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
295
293
  #
296
- # res = Net::HTTP.start(hostname) do |http|
297
- # http.get('/todos/1')
298
- # end
294
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
299
295
  # res.get_fields('Connection') # => ["keep-alive"]
300
296
  # res.get_fields('Nosuch') # => nil
301
297
  #
@@ -305,7 +301,7 @@ module Net::HTTPHeader
305
301
  @header[stringified_downcased_key].dup
306
302
  end
307
303
 
308
- # :call-seq
304
+ # call-seq:
309
305
  # fetch(key, default_val = nil) {|key| ... } -> object
310
306
  # fetch(key, default_val = nil) -> value or default_val
311
307
  #
@@ -314,9 +310,7 @@ module Net::HTTPHeader
314
310
  # ignores the +default_val+;
315
311
  # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
316
312
  #
317
- # res = Net::HTTP.start(hostname) do |http|
318
- # http.get('/todos/1')
319
- # end
313
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
320
314
  #
321
315
  # # Field exists; block not called.
322
316
  # res.fetch('Connection') do |value|
@@ -343,9 +337,7 @@ module Net::HTTPHeader
343
337
 
344
338
  # Calls the block with each key/value pair:
345
339
  #
346
- # res = Net::HTTP.start(hostname) do |http|
347
- # http.get('/todos/1')
348
- # end
340
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
349
341
  # res.each_header do |key, value|
350
342
  # p [key, value] if key.start_with?('c')
351
343
  # end
@@ -372,20 +364,18 @@ module Net::HTTPHeader
372
364
 
373
365
  # Calls the block with each field key:
374
366
  #
375
- # res = Net::HTTP.start(hostname) do |http|
376
- # http.get('/todos/1')
377
- # end
367
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
378
368
  # res.each_key do |key|
379
369
  # p key if key.start_with?('c')
380
370
  # end
381
371
  #
382
372
  # Output:
383
373
  #
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"
389
379
  #
390
380
  # Returns an enumerator if no block is given.
391
381
  #
@@ -399,9 +389,7 @@ module Net::HTTPHeader
399
389
 
400
390
  # Calls the block with each capitalized field name:
401
391
  #
402
- # res = Net::HTTP.start(hostname) do |http|
403
- # http.get('/todos/1')
404
- # end
392
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
405
393
  # res.each_capitalized_name do |key|
406
394
  # p key if key.start_with?('C')
407
395
  # end
@@ -427,9 +415,7 @@ module Net::HTTPHeader
427
415
 
428
416
  # Calls the block with each string field value:
429
417
  #
430
- # res = Net::HTTP.start(hostname) do |http|
431
- # http.get('/todos/1')
432
- # end
418
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
433
419
  # res.each_value do |value|
434
420
  # p value if value.start_with?('c')
435
421
  # end
@@ -554,7 +540,7 @@ module Net::HTTPHeader
554
540
  result
555
541
  end
556
542
 
557
- # :call-seq:
543
+ # call-seq:
558
544
  # set_range(length) -> length
559
545
  # set_range(offset, length) -> range
560
546
  # set_range(begin..length) -> range
@@ -610,8 +596,15 @@ module Net::HTTPHeader
610
596
 
611
597
  alias range= set_range
612
598
 
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
+ #
615
608
  def content_length
616
609
  return nil unless key?('Content-Length')
617
610
  len = self['Content-Length'].slice(/\d+/) or
@@ -619,6 +612,20 @@ module Net::HTTPHeader
619
612
  len.to_i
620
613
  end
621
614
 
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
+ #
622
629
  def content_length=(len)
623
630
  unless len
624
631
  @header.delete 'content-length'
@@ -627,20 +634,31 @@ module Net::HTTPHeader
627
634
  @header['content-length'] = [len.to_i.to_s]
628
635
  end
629
636
 
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
+ #
634
646
  def chunked?
635
647
  return false unless @header['transfer-encoding']
636
648
  field = self['Transfer-Encoding']
637
649
  (/(?:\A|[^\-\w])chunked(?![\-\w])/i =~ field) ? true : false
638
650
  end
639
651
 
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
+ #
644
662
  def content_range
645
663
  return nil unless @header['content-range']
646
664
  m = %r<\A\s*(\w+)\s+(\d+)-(\d+)/(\d+|\*)>.match(self['Content-Range']) or
@@ -649,14 +667,29 @@ module Net::HTTPHeader
649
667
  m[2].to_i .. m[3].to_i
650
668
  end
651
669
 
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
+ #
653
679
  def range_length
654
680
  r = content_range() or return nil
655
681
  r.end - r.begin + 1
656
682
  end
657
683
 
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
+ #
660
693
  def content_type
661
694
  return nil unless main_type()
662
695
  if sub_type()
@@ -665,16 +698,31 @@ module Net::HTTPHeader
665
698
  end
666
699
  end
667
700
 
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
+ #
670
711
  def main_type
671
712
  return nil unless @header['content-type']
672
713
  self['Content-Type'].split(';').first.to_s.split('/')[0].to_s.strip
673
714
  end
674
715
 
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
+ #
678
726
  def sub_type
679
727
  return nil unless @header['content-type']
680
728
  _, sub = *self['Content-Type'].split(';').first.to_s.split('/')
@@ -682,9 +730,14 @@ module Net::HTTPHeader
682
730
  sub.strip
683
731
  end
684
732
 
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
+ #
688
741
  def type_params
689
742
  result = {}
690
743
  list = self['Content-Type'].to_s.split(';')
@@ -696,10 +749,12 @@ module Net::HTTPHeader
696
749
  result
697
750
  end
698
751
 
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"]
703
758
  #
704
759
  # Net::HTTPHeader#content_type= is an alias for Net::HTTPHeader#set_content_type.
705
760
  def set_content_type(type, params = {})
@@ -717,6 +772,7 @@ module Net::HTTPHeader
717
772
  # application/x-www-form-urlencoded
718
773
  #
719
774
  # Example:
775
+ #
720
776
  # http.form_data = {"q" => "ruby", "lang" => "en"}
721
777
  # http.form_data = {"q" => ["ruby", "perl"], "lang" => "en"}
722
778
  # http.set_form_data({"q" => "ruby", "lang" => "en"}, ';')
@@ -746,12 +802,14 @@ module Net::HTTPHeader
746
802
  #
747
803
  # Each item of params should respond to +each+ and yield 2-3 arguments,
748
804
  # or an array of 2-3 elements. The arguments yielded should be:
749
- # * The name of the field.
750
- # * The value of the field, it should be a String or a File or IO-like.
751
- # * An options hash, supporting the following options, only
752
- # used for file uploads:
753
- # :filename :: The name of the file to use.
754
- # :content_type :: The content type of the uploaded file.
805
+ #
806
+ # - The name of the field.
807
+ # - The value of the field, it should be a String or a File or IO-like.
808
+ # - An options hash, supporting the following options
809
+ # (used only for file uploads); entries:
810
+ #
811
+ # - +:filename+: The name of the file to use.
812
+ # - +:content_type+: The content type of the uploaded file.
755
813
  #
756
814
  # Each item is a file field or a normal field.
757
815
  # If +value+ is a File object or the +opt+ hash has a :filename key,
@@ -763,6 +821,7 @@ module Net::HTTPHeader
763
821
  # chunked encoding.
764
822
  #
765
823
  # Example:
824
+ #
766
825
  # req.set_form([["q", "ruby"], ["lang", "en"]])
767
826
  #
768
827
  # req.set_form({"f"=>File.open('/path/to/filename')},