net-http 0.3.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,55 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
 
3
- # This class is the base class for \Net::HTTP request classes;
4
- # it wraps together the request path and the request headers.
5
- #
3
+ # This class is the base class for \Net::HTTP request classes.
6
4
  # The class should not be used directly;
7
- # instead you should use its subclasses.
5
+ # instead you should use its subclasses, listed below.
6
+ #
7
+ # == Creating a Request
8
+ #
9
+ # An request object may be created with either a URI or a string hostname:
10
+ #
11
+ # require 'net/http'
12
+ # uri = URI('https://jsonplaceholder.typicode.com/')
13
+ # req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET>
14
+ # req = Net::HTTP::Get.new(uri.hostname) # => #<Net::HTTP::Get GET>
15
+ #
16
+ # And with any of the subclasses:
17
+ #
18
+ # req = Net::HTTP::Head.new(uri) # => #<Net::HTTP::Head HEAD>
19
+ # req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
20
+ # req = Net::HTTP::Put.new(uri) # => #<Net::HTTP::Put PUT>
21
+ # # ...
22
+ #
23
+ # The new instance is suitable for use as the argument to Net::HTTP#request.
24
+ #
25
+ # == Request Headers
26
+ #
27
+ # A new request object has these header fields by default:
28
+ #
29
+ # req.to_hash
30
+ # # =>
31
+ # {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
32
+ # "accept"=>["*/*"],
33
+ # "user-agent"=>["Ruby"],
34
+ # "host"=>["jsonplaceholder.typicode.com"]}
35
+ #
36
+ # See:
37
+ #
38
+ # - {Request header Accept-Encoding}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Accept-Encoding]
39
+ # and {Compression and Decompression}[rdoc-ref:Net::HTTP@Compression+and+Decompression].
40
+ # - {Request header Accept}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#accept-request-header].
41
+ # - {Request header User-Agent}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#user-agent-request-header].
42
+ # - {Request header Host}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#host-request-header].
43
+ #
44
+ # You can add headers or override default headers:
45
+ #
46
+ # # res = Net::HTTP::Get.new(uri, {'foo' => '0', 'bar' => '1'})
47
+ #
48
+ # This class (and therefore its subclasses) also includes (indirectly)
49
+ # module Net::HTTPHeader, which gives access to its
50
+ # {methods for setting headers}[rdoc-ref:Net::HTTPHeader@Setters].
51
+ #
52
+ # == Request Subclasses
8
53
  #
9
54
  # Subclasses for HTTP requests:
10
55
  #
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
 
3
3
  # HTTP/1.1 methods --- RFC2616
4
4
 
@@ -13,6 +13,8 @@
13
13
  # http.request(req)
14
14
  # end
15
15
  #
16
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
17
+ #
16
18
  # Properties:
17
19
  #
18
20
  # - Request body: optional.
@@ -43,6 +45,8 @@ end
43
45
  # http.request(req)
44
46
  # end
45
47
  #
48
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
49
+ #
46
50
  # Properties:
47
51
  #
48
52
  # - Request body: optional.
@@ -75,6 +79,8 @@ end
75
79
  # http.request(req)
76
80
  # end
77
81
  #
82
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
83
+ #
78
84
  # Properties:
79
85
  #
80
86
  # - Request body: yes.
@@ -108,6 +114,8 @@ end
108
114
  # http.request(req)
109
115
  # end
110
116
  #
117
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
118
+ #
111
119
  # Properties:
112
120
  #
113
121
  # - Request body: yes.
@@ -116,6 +124,11 @@ end
116
124
  # - {Idempotent}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Idempotent_methods]: yes.
117
125
  # - {Cacheable}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Cacheable_methods]: no.
118
126
  #
127
+ # Related:
128
+ #
129
+ # - Net::HTTP.put: sends +PUT+ request, returns response object.
130
+ # - Net::HTTP#put: sends +PUT+ request, returns response object.
131
+ #
119
132
  class Net::HTTP::Put < Net::HTTPRequest
120
133
  METHOD = 'PUT'
121
134
  REQUEST_HAS_BODY = true
@@ -134,6 +147,8 @@ end
134
147
  # http.request(req)
135
148
  # end
136
149
  #
150
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
151
+ #
137
152
  # Properties:
138
153
  #
139
154
  # - Request body: optional.
@@ -163,6 +178,8 @@ end
163
178
  # http.request(req)
164
179
  # end
165
180
  #
181
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
182
+ #
166
183
  # Properties:
167
184
  #
168
185
  # - Request body: optional.
@@ -192,6 +209,8 @@ end
192
209
  # http.request(req)
193
210
  # end
194
211
  #
212
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
213
+ #
195
214
  # Properties:
196
215
  #
197
216
  # - Request body: no.
@@ -224,6 +243,8 @@ end
224
243
  # http.request(req)
225
244
  # end
226
245
  #
246
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
247
+ #
227
248
  # Properties:
228
249
  #
229
250
  # - Request body: yes.
@@ -257,6 +278,8 @@ end
257
278
  # http.request(req)
258
279
  # end
259
280
  #
281
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
282
+ #
260
283
  # Related:
261
284
  #
262
285
  # - Net::HTTP#propfind: sends +PROPFIND+ request, returns response object.
@@ -278,6 +301,8 @@ end
278
301
  # http.request(req)
279
302
  # end
280
303
  #
304
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
305
+ #
281
306
  # Related:
282
307
  #
283
308
  # - Net::HTTP#proppatch: sends +PROPPATCH+ request, returns response object.
@@ -299,6 +324,8 @@ end
299
324
  # http.request(req)
300
325
  # end
301
326
  #
327
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
328
+ #
302
329
  # Related:
303
330
  #
304
331
  # - Net::HTTP#mkcol: sends +MKCOL+ request, returns response object.
@@ -320,6 +347,8 @@ end
320
347
  # http.request(req)
321
348
  # end
322
349
  #
350
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
351
+ #
323
352
  # Related:
324
353
  #
325
354
  # - Net::HTTP#copy: sends +COPY+ request, returns response object.
@@ -341,6 +370,8 @@ end
341
370
  # http.request(req)
342
371
  # end
343
372
  #
373
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
374
+ #
344
375
  # Related:
345
376
  #
346
377
  # - Net::HTTP#move: sends +MOVE+ request, returns response object.
@@ -362,6 +393,8 @@ end
362
393
  # http.request(req)
363
394
  # end
364
395
  #
396
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
397
+ #
365
398
  # Related:
366
399
  #
367
400
  # - Net::HTTP#lock: sends +LOCK+ request, returns response object.
@@ -383,6 +416,8 @@ end
383
416
  # http.request(req)
384
417
  # end
385
418
  #
419
+ # See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers].
420
+ #
386
421
  # Related:
387
422
  #
388
423
  # - Net::HTTP#unlock: sends +UNLOCK+ request, returns response object.
@@ -1,6 +1,6 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
 
3
- # This class is the base class for \Net::HTTP request classes.
3
+ # This class is the base class for \Net::HTTP response classes.
4
4
  #
5
5
  # == About the Examples
6
6
  #
@@ -224,13 +224,32 @@ class Net::HTTPResponse
224
224
  # Accept-Encoding header from the user.
225
225
  attr_accessor :decode_content
226
226
 
227
- # The encoding to use for the response body. If Encoding, use that encoding.
228
- # If other true value, attempt to detect the appropriate encoding, and use
229
- # that.
227
+ # Returns the value set by body_encoding=, or +false+ if none;
228
+ # see #body_encoding=.
230
229
  attr_reader :body_encoding
231
230
 
232
- # Set the encoding to use for the response body. If given a String, find
233
- # the related Encoding.
231
+ # Sets the encoding that should be used when reading the body:
232
+ #
233
+ # - If the given value is an Encoding object, that encoding will be used.
234
+ # - Otherwise if the value is a string, the value of
235
+ # {Encoding#find(value)}[https://docs.ruby-lang.org/en/master/Encoding.html#method-c-find]
236
+ # will be used.
237
+ # - Otherwise an encoding will be deduced from the body itself.
238
+ #
239
+ # Examples:
240
+ #
241
+ # http = Net::HTTP.new(hostname)
242
+ # req = Net::HTTP::Get.new('/')
243
+ #
244
+ # http.request(req) do |res|
245
+ # p res.body.encoding # => #<Encoding:ASCII-8BIT>
246
+ # end
247
+ #
248
+ # http.request(req) do |res|
249
+ # res.body_encoding = "UTF-8"
250
+ # p res.body.encoding # => #<Encoding:UTF-8>
251
+ # end
252
+ #
234
253
  def body_encoding=(value)
235
254
  value = Encoding.find(value) if value.is_a?(String)
236
255
  @body_encoding = value
@@ -254,7 +273,7 @@ class Net::HTTPResponse
254
273
 
255
274
  def error! #:nodoc:
256
275
  message = @code
257
- message += ' ' + @message.dump if @message
276
+ message = "#{message} #{@message.dump}" if @message
258
277
  raise error_type().new(message, self)
259
278
  end
260
279
 
@@ -347,6 +366,7 @@ class Net::HTTPResponse
347
366
  @body = nil
348
367
  end
349
368
  @read = true
369
+ return if @body.nil?
350
370
 
351
371
  case enc = @body_encoding
352
372
  when Encoding, false, nil
@@ -362,26 +382,26 @@ class Net::HTTPResponse
362
382
  @body
363
383
  end
364
384
 
365
- # Returns the full entity body.
385
+ # Returns the string response body;
386
+ # note that repeated calls for the unmodified body return a cached string:
366
387
  #
367
- # Calling this method a second or subsequent time will return the
368
- # string already read.
388
+ # path = '/todos/1'
389
+ # Net::HTTP.start(hostname) do |http|
390
+ # res = http.get(path)
391
+ # p res.body
392
+ # p http.head(path).body # No body.
393
+ # end
369
394
  #
370
- # http.request_get('/index.html') {|res|
371
- # puts res.body
372
- # }
395
+ # Output:
373
396
  #
374
- # http.request_get('/index.html') {|res|
375
- # p res.body.object_id # 538149362
376
- # p res.body.object_id # 538149362
377
- # }
397
+ # "{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false\n}"
398
+ # nil
378
399
  #
379
400
  def body
380
401
  read_body()
381
402
  end
382
403
 
383
- # Because it may be necessary to modify the body, Eg, decompression
384
- # this method facilitates that.
404
+ # Sets the body of the response to the given value.
385
405
  def body=(value)
386
406
  @body = value
387
407
  end
@@ -620,7 +640,7 @@ class Net::HTTPResponse
620
640
  end
621
641
 
622
642
  def stream_check
623
- raise IOError, 'attempt to read body out of block' if @socket.closed?
643
+ raise IOError, 'attempt to read body out of block' if @socket.nil? || @socket.closed?
624
644
  end
625
645
 
626
646
  def procdest(dest, block)
@@ -629,7 +649,7 @@ class Net::HTTPResponse
629
649
  if block
630
650
  Net::ReadAdapter.new(block)
631
651
  else
632
- dest || ''
652
+ dest || +''
633
653
  end
634
654
  end
635
655