typhoeus 1.3.0 → 1.3.1

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
- SHA1:
3
- metadata.gz: df7045af2adbad5bf0b5bc883e8bc259a2f63544
4
- data.tar.gz: 16d738d8b00c774fe54a78c32becde597ad69569
2
+ SHA256:
3
+ metadata.gz: 417ef8dd5903c12a896c84db243131ec364aec235a44ac32d2590d95f919c10a
4
+ data.tar.gz: 62fca0f2cf069282d5c6cdbed018e395b6b40f379cb2d8e45129a156e7aed69a
5
5
  SHA512:
6
- metadata.gz: c5f8427b91d383cd94bc48c4f944b3bc7dbce619e0eccda0f84d428eb575ff9f7b65530f18789cb1aa99f02e4f03293af9987cffb12686542af13b9fb49bba51
7
- data.tar.gz: e01422dc678ff40ac31b695bc1dea8d0c3b843e0ecd2ad61fa386d9b290a0950db93e37289215aedaee3aad60a7cc2acd48efc4baf59ae4fc96f3198d5df460a
6
+ metadata.gz: 9a8e291edbdb7e635f6ffbf085783752e6a0688e342dbec333e2fd85f6d4ac841a4bfca128fe36d4f30d5e0f87a8d124447d77cfd2d15e77c6f29bec4a55f197
7
+ data.tar.gz: cf6d163bfb31f1cf77c2faf068601b6c65685d5afaf0f582dadd9505882fb8c567583fd1dad7a47f27210018e992ac300a7803f3870250e1f7a1461111bcab80
data/README.md CHANGED
@@ -191,6 +191,19 @@ end
191
191
  request.run
192
192
  ```
193
193
 
194
+ If you need to interrupt the stream halfway,
195
+ you can return the `:abort` symbol from the `on_body` block, example:
196
+
197
+ ```ruby
198
+ request.on_body do |chunk|
199
+ buffer << chunk
200
+ :abort if buffer.size > 1024 * 1024
201
+ end
202
+ ```
203
+
204
+ This will properly stop the stream internally and avoid any memory leak which
205
+ may happen if you interrupt with something like a `return`, `throw` or `raise`.
206
+
194
207
  ### Making Parallel Requests
195
208
 
196
209
  Generally, you should be running requests through hydra. Here is how that looks:
@@ -242,6 +255,33 @@ end
242
255
  hydra.run
243
256
  ```
244
257
 
258
+ ### Making Parallel Requests with Faraday + Typhoeus
259
+
260
+ ```ruby
261
+ require 'faraday'
262
+
263
+ conn = Faraday.new(:url => 'http://httppage.com') do |builder|
264
+ builder.request :url_encoded
265
+ builder.response :logger
266
+ builder.adapter :typhoeus
267
+ end
268
+
269
+ conn.in_parallel do
270
+ response1 = conn.get('/first')
271
+ response2 = conn.get('/second')
272
+
273
+ # these will return nil here since the
274
+ # requests have not been completed
275
+ response1.body
276
+ response2.body
277
+ end
278
+
279
+ # after it has been completed the response information is fully available
280
+ # response1.status, etc
281
+ response1.body
282
+ response2.body
283
+ ```
284
+
245
285
  ### Specifying Max Concurrency
246
286
 
247
287
  Hydra will also handle how many requests you can make in parallel. Things will get flakey if you try to make too many requests at the same time. The built in limit is 200. When more requests than that are queued up, hydra will save them for later and start the requests as others are finished. You can raise or lower the concurrency limit through the Hydra constructor.
@@ -327,6 +367,14 @@ Typhoeus::Config.cache = Typhoeus::Cache::Redis.new(redis)
327
367
  All three of these adapters take an optional keyword argument `default_ttl`, which sets a default
328
368
  TTL on cached responses (in seconds), for requests which do not have a cache TTL set.
329
369
 
370
+ You may also selectively choose not to cache by setting `cache` to `false` on a request or to use
371
+ a different adapter.
372
+
373
+ ```ruby
374
+ cache = Cache.new
375
+ Typhoeus.get("www.example.com", cache: cache)
376
+ ```
377
+
330
378
  ### Direct Stubbing
331
379
 
332
380
  Hydra allows you to stub out specific urls and patterns to avoid hitting
@@ -99,7 +99,7 @@ module Faraday # :nodoc:
99
99
  unless parallel?(env)
100
100
  raise Faraday::Error::TimeoutError, "request timed out"
101
101
  end
102
- elsif resp.response_code == 0
102
+ elsif (resp.response_code == 0) || ((resp.return_code != :ok) && !resp.mock?)
103
103
  env[:typhoeus_connection_failed] = true
104
104
  env[:typhoeus_return_message] = resp.return_message
105
105
  unless parallel?(env)
@@ -35,7 +35,7 @@ module Typhoeus
35
35
 
36
36
  REMOVED_OPTIONS = Set.new([:cache_key_basis, :cache_timeout, :user_agent])
37
37
 
38
- SANITIZE_IGNORE = Set.new([:method, :cache_ttl])
38
+ SANITIZE_IGNORE = Set.new([:method, :cache_ttl, :cache])
39
39
  SANITIZE_TIMEOUT = Set.new([:timeout_ms, :connecttimeout_ms])
40
40
 
41
41
  # Returns the request provided.
@@ -155,6 +155,11 @@ module Typhoeus
155
155
  request.execute_headers_callbacks(Response.new(Ethon::Easy::Mirror.from_easy(easy).options))
156
156
  end
157
157
  end
158
+ request.on_progress.each do |callback|
159
+ easy.on_progress do |dltotal, dlnow, ultotal, ulnow, easy|
160
+ callback.call(dltotal, dlnow, ultotal, ulnow, response)
161
+ end
162
+ end
158
163
  easy.on_complete do |easy|
159
164
  request.finish(Response.new(easy.mirror.options))
160
165
  Typhoeus::Pool.release(easy)
@@ -15,7 +15,7 @@ module Typhoeus
15
15
  #
16
16
  # @option (see Typhoeus::Request#initialize)
17
17
  #
18
- # @return (see Typhoeus::Request#initialize)
18
+ # @return (see Typhoeus::Response#initialize)
19
19
  #
20
20
  # @note (see Typhoeus::Request#initialize)
21
21
  def get(base_url, options = {})
@@ -31,7 +31,7 @@ module Typhoeus
31
31
  #
32
32
  # @option (see Typhoeus::Request#initialize)
33
33
  #
34
- # @return (see Typhoeus::Request#initialize)
34
+ # @return (see Typhoeus::Response#initialize)
35
35
  #
36
36
  # @note (see Typhoeus::Request#initialize)
37
37
  def post(base_url, options = {})
@@ -50,7 +50,7 @@ module Typhoeus
50
50
  # @option options :body [ Hash ] Body hash which
51
51
  # becomes a PUT request body.
52
52
  #
53
- # @return (see Typhoeus::Request#initialize)
53
+ # @return (see Typhoeus::Response#initialize)
54
54
  #
55
55
  # @note (see Typhoeus::Request#initialize)
56
56
  def put(base_url, options = {})
@@ -66,7 +66,7 @@ module Typhoeus
66
66
  #
67
67
  # @option (see Typhoeus::Request#initialize)
68
68
  #
69
- # @return (see Typhoeus::Request#initialize)
69
+ # @return (see Typhoeus::Response#initialize)
70
70
  #
71
71
  # @note (see Typhoeus::Request#initialize)
72
72
  def delete(base_url, options = {})
@@ -82,7 +82,7 @@ module Typhoeus
82
82
  #
83
83
  # @option (see Typhoeus::Request#initialize)
84
84
  #
85
- # @return (see Typhoeus::Request#initialize)
85
+ # @return (see Typhoeus::Response#initialize)
86
86
  #
87
87
  # @note (see Typhoeus::Request#initialize)
88
88
  def head(base_url, options = {})
@@ -98,7 +98,7 @@ module Typhoeus
98
98
  #
99
99
  # @option (see Typhoeus::Request#initialize)
100
100
  #
101
- # @return (see Typhoeus::Request#initialize)
101
+ # @return (see Typhoeus::Response#initialize)
102
102
  #
103
103
  # @note (see Typhoeus::Request#initialize)
104
104
  def patch(base_url, options = {})
@@ -114,7 +114,7 @@ module Typhoeus
114
114
  #
115
115
  # @option (see Typhoeus::Request#initialize)
116
116
  #
117
- # @return (see Typhoeus::Request#initialize)
117
+ # @return (see Typhoeus::Response#initialize)
118
118
  #
119
119
  # @note (see Typhoeus::Request#initialize)
120
120
  def options(base_url, options = {})
@@ -2,16 +2,16 @@ module Typhoeus
2
2
  class Request
3
3
  module Cacheable
4
4
  def response=(response)
5
- Typhoeus::Config.cache.set(self, response) if cacheable? && !response.cached?
5
+ cache.set(self, response) if cacheable? && !response.cached?
6
6
  super
7
7
  end
8
8
 
9
9
  def cacheable?
10
- Typhoeus::Config.cache
10
+ cache
11
11
  end
12
12
 
13
13
  def run
14
- if cacheable? && response = Typhoeus::Config.cache.get(self)
14
+ if cacheable? && response = cache.get(self)
15
15
  response.cached = true
16
16
  finish(response)
17
17
  else
@@ -22,6 +22,13 @@ module Typhoeus
22
22
  def cache_ttl
23
23
  options[:cache_ttl]
24
24
  end
25
+
26
+ private
27
+
28
+ def cache
29
+ return nil if options[:cache] === false
30
+ options[:cache] || Typhoeus::Config.cache
31
+ end
25
32
  end
26
33
  end
27
34
  end
@@ -89,6 +89,24 @@ module Typhoeus
89
89
  @on_headers << block if block_given?
90
90
  @on_headers
91
91
  end
92
+
93
+ # Set on_progress callback.
94
+ #
95
+ # @example Set on_progress.
96
+ # request.on_progress do |dltotal, dlnow, ultotal, ulnow|
97
+ # puts "dltotal (#{dltotal}), dlnow (#{dlnow}), ultotal (#{ultotal}), ulnow (#{ulnow})"
98
+ # end
99
+ #
100
+ # @param [ Block ] block The block to execute.
101
+ #
102
+ # @yield [ Typhoeus::Response ]
103
+ #
104
+ # @return [ Array<Block> ] All on_progress blocks.
105
+ def on_progress(&block)
106
+ @on_progress ||= []
107
+ @on_progress << block if block_given?
108
+ @on_progress
109
+ end
92
110
  end
93
111
 
94
112
  # Execute the headers callbacks and yields response.
@@ -106,8 +124,8 @@ module Typhoeus
106
124
  end
107
125
 
108
126
  # Execute necessary callback and yields response. This
109
- # include in every case on_complete, on_success if
110
- # successful and on_failure if not.
127
+ # include in every case on_complete and on_progress, on_success
128
+ # if successful and on_failure if not.
111
129
  #
112
130
  # @example Execute callbacks.
113
131
  # request.execute_callbacks
@@ -116,7 +134,7 @@ module Typhoeus
116
134
  #
117
135
  # @api private
118
136
  def execute_callbacks
119
- callbacks = Typhoeus.on_complete + on_complete
137
+ callbacks = Typhoeus.on_complete + Typhoeus.on_progress + on_complete + on_progress
120
138
 
121
139
  if response && response.success?
122
140
  callbacks += Typhoeus.on_success + on_success
@@ -5,9 +5,9 @@ module Typhoeus
5
5
  module Marshal
6
6
 
7
7
  # Return the important data needed to serialize this Request, except the
8
- # `on_complete`, `on_success`, `on_failure`, and `hydra`, since they cannot be marshalled.
8
+ # request callbacks and `hydra`, since they cannot be marshalled.
9
9
  def marshal_dump
10
- unmarshallable = %w(@on_complete @on_success @on_failure @on_headers @on_body @hydra)
10
+ unmarshallable = %w(@on_complete @on_success @on_failure @on_progress @on_headers @on_body @hydra)
11
11
  (instance_variables - unmarshallable - unmarshallable.map(&:to_sym)).map do |name|
12
12
  [name, instance_variable_get(name)]
13
13
  end
@@ -10,7 +10,7 @@ module Typhoeus
10
10
  # Setting an on_body callback will cause the response body to be empty.
11
11
  #
12
12
  # @example Set on_body.
13
- # request.on_body { |response, body_chunk| puts "Got #{body_chunk.bytesize} bytes" }
13
+ # request.on_body { |body_chunk, response| puts "Got #{body_chunk.bytesize} bytes" }
14
14
  #
15
15
  # @param [ Block ] block The block to execute.
16
16
  #
@@ -14,7 +14,7 @@ module Typhoeus
14
14
  # Remembers the corresponding request.
15
15
  #
16
16
  # @example Get request.
17
- # request = Typhoeus::Request.get("www.example.com")
17
+ # request = Typhoeus::Request.new("www.example.com")
18
18
  # response = request.run
19
19
  # request == response.request
20
20
  # #=> true
@@ -1,3 +1,5 @@
1
+ require 'delegate'
2
+
1
3
  module Typhoeus
2
4
  class Response
3
5
 
@@ -39,7 +39,7 @@ module Typhoeus
39
39
  @http_version ||= first_header_line ? first_header_line[/HTTP\/(\S+)/, 1] : nil
40
40
  end
41
41
 
42
- # Return wether the response is a success.
42
+ # Return whether the response is a success.
43
43
  #
44
44
  # @example Return if the response was successful.
45
45
  # response.success?
@@ -49,13 +49,12 @@ module Typhoeus
49
49
  (mock || return_code == :ok) && response_code && has_good_response_code?
50
50
  end
51
51
 
52
- # Return wether the response is a failure.
52
+ # Return whether the response is a failure.
53
53
  #
54
54
  # @example Return if the response was failed.
55
55
  # response.failure?
56
56
  #
57
57
  # @return [ Boolean ] Return true if failure, false else.
58
-
59
58
  def failure?
60
59
  (mock || return_code == :internal_server_error) && response_code && has_bad_response_code?
61
60
  end
@@ -1,5 +1,5 @@
1
1
  module Typhoeus
2
2
 
3
3
  # The current Typhoeus version.
4
- VERSION = '1.3.0'
4
+ VERSION = '1.3.1'
5
5
  end
@@ -104,6 +104,12 @@ describe Typhoeus::EasyFactory do
104
104
  end
105
105
 
106
106
  describe "#set_callback" do
107
+ it "sets easy.on_progress callback when an on_progress callback is provided" do
108
+ request.on_progress { 1 }
109
+ expect(easy_factory.easy).to receive(:on_progress)
110
+ easy_factory.send(:set_callback)
111
+ end
112
+
107
113
  it "sets easy.on_complete callback" do
108
114
  expect(easy_factory.easy).to receive(:on_complete)
109
115
  easy_factory.send(:set_callback)
@@ -55,6 +55,30 @@ describe Typhoeus::Request::Cacheable do
55
55
  request.run
56
56
  end
57
57
  end
58
+
59
+ context "when cache is specified on a request" do
60
+ before { Typhoeus::Config.cache = false }
61
+
62
+ context "when cache is false" do
63
+ let(:options) { { :cache => false } }
64
+
65
+ it "finishes request" do
66
+ expect(request.response).to_not be(response)
67
+ request.run
68
+ end
69
+ end
70
+
71
+ context "when cache is defined" do
72
+ let(:options) { { :cache => cache } }
73
+
74
+ before { cache.memory[request] = response }
75
+
76
+ it "finishes request" do
77
+ expect(request).to receive(:finish).with(response)
78
+ request.run
79
+ end
80
+ end
81
+ end
58
82
  end
59
83
  end
60
84
 
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Typhoeus::Request::Callbacks do
4
4
  let(:request) { Typhoeus::Request.new("fubar") }
5
5
 
6
- [:on_complete, :on_success, :on_failure].each do |callback|
6
+ [:on_complete, :on_success, :on_failure, :on_progress].each do |callback|
7
7
  describe "##{callback}" do
8
8
  it "responds" do
9
9
  expect(request).to respond_to(callback)
@@ -33,7 +33,7 @@ describe Typhoeus::Request::Callbacks do
33
33
  end
34
34
 
35
35
  describe "#execute_callbacks" do
36
- [:on_complete, :on_success, :on_failure].each do |callback|
36
+ [:on_complete, :on_success, :on_failure, :on_progress].each do |callback|
37
37
  context "when #{callback}" do
38
38
  context "when local callback" do
39
39
  before do
@@ -5,7 +5,7 @@ describe Typhoeus::Request::Marshal do
5
5
  let(:request) { Typhoeus::Request.new(base_url) }
6
6
 
7
7
  describe "#marshal_dump" do
8
- %w(on_complete on_success on_failure).each do |name|
8
+ %w(on_complete on_success on_failure on_progress).each do |name|
9
9
  context "when #{name} handler" do
10
10
  before { request.instance_variable_set("@#{name}", Proc.new{}) }
11
11
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typhoeus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Balatero
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-08-21 00:00:00.000000000 Z
13
+ date: 2018-11-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: ethon
@@ -150,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  version: 1.3.6
151
151
  requirements: []
152
152
  rubyforge_project:
153
- rubygems_version: 2.2.2
153
+ rubygems_version: 2.7.6
154
154
  signing_key:
155
155
  specification_version: 4
156
156
  summary: Parallel HTTP library on top of libcurl multi.