From c790ca00e7c4ef2269fee0b30184a45a651b6ab4 Mon Sep 17 00:00:00 2001 From: Zach Walton Date: Wed, 4 Apr 2018 13:17:53 -0700 Subject: [PATCH] add on_progress callback --- lib/typhoeus/easy_factory.rb | 5 +++++ lib/typhoeus/request/callbacks.rb | 24 +++++++++++++++++++++--- spec/typhoeus/easy_factory_spec.rb | 6 ++++++ spec/typhoeus/request/callbacks_spec.rb | 4 ++-- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/typhoeus/easy_factory.rb b/lib/typhoeus/easy_factory.rb index b9e21f28..a28e3058 100644 --- a/lib/typhoeus/easy_factory.rb +++ b/lib/typhoeus/easy_factory.rb @@ -155,6 +155,11 @@ def set_callback request.execute_headers_callbacks(Response.new(Ethon::Easy::Mirror.from_easy(easy).options)) end end + request.on_progress.each do |callback| + easy.on_progress do |dltotal, dlnow, ultotal, ulnow, easy| + callback.call(dltotal, dlnow, ultotal, ulnow, response) + end + end easy.on_complete do |easy| request.finish(Response.new(easy.mirror.options)) Typhoeus::Pool.release(easy) diff --git a/lib/typhoeus/request/callbacks.rb b/lib/typhoeus/request/callbacks.rb index c8420dee..cc2cc1ea 100644 --- a/lib/typhoeus/request/callbacks.rb +++ b/lib/typhoeus/request/callbacks.rb @@ -89,6 +89,24 @@ def on_headers(&block) @on_headers << block if block_given? @on_headers end + + # Set on_progress callback. + # + # @example Set on_progress. + # request.on_progress do |dltotal, dlnow, ultotal, ulnow| + # puts "dltotal (#{dltotal}), dlnow (#{dlnow}), ultotal (#{ultotal}), ulnow (#{ulnow})" + # end + # + # @param [ Block ] block The block to execute. + # + # @yield [ Typhoeus::Response ] + # + # @return [ Array ] All on_progress blocks. + def on_progress(&block) + @on_progress ||= [] + @on_progress << block if block_given? + @on_progress + end end # Execute the headers callbacks and yields response. @@ -106,8 +124,8 @@ def execute_headers_callbacks(response) end # Execute necessary callback and yields response. This - # include in every case on_complete, on_success if - # successful and on_failure if not. + # include in every case on_complete and on_progress, on_success + # if successful and on_failure if not. # # @example Execute callbacks. # request.execute_callbacks @@ -116,7 +134,7 @@ def execute_headers_callbacks(response) # # @api private def execute_callbacks - callbacks = Typhoeus.on_complete + on_complete + callbacks = Typhoeus.on_complete + Typhoeus.on_progress + on_complete + on_progress if response && response.success? callbacks += Typhoeus.on_success + on_success diff --git a/spec/typhoeus/easy_factory_spec.rb b/spec/typhoeus/easy_factory_spec.rb index 7d4aeae7..cc1e9bc0 100644 --- a/spec/typhoeus/easy_factory_spec.rb +++ b/spec/typhoeus/easy_factory_spec.rb @@ -104,6 +104,12 @@ end describe "#set_callback" do + it "sets easy.on_progress callback when an on_progress callback is provided" do + request.on_progress { 1 } + expect(easy_factory.easy).to receive(:on_progress) + easy_factory.send(:set_callback) + end + it "sets easy.on_complete callback" do expect(easy_factory.easy).to receive(:on_complete) easy_factory.send(:set_callback) diff --git a/spec/typhoeus/request/callbacks_spec.rb b/spec/typhoeus/request/callbacks_spec.rb index db860983..9b30e74b 100644 --- a/spec/typhoeus/request/callbacks_spec.rb +++ b/spec/typhoeus/request/callbacks_spec.rb @@ -3,7 +3,7 @@ describe Typhoeus::Request::Callbacks do let(:request) { Typhoeus::Request.new("fubar") } - [:on_complete, :on_success, :on_failure].each do |callback| + [:on_complete, :on_success, :on_failure, :on_progress].each do |callback| describe "##{callback}" do it "responds" do expect(request).to respond_to(callback) @@ -33,7 +33,7 @@ end describe "#execute_callbacks" do - [:on_complete, :on_success, :on_failure].each do |callback| + [:on_complete, :on_success, :on_failure, :on_progress].each do |callback| context "when #{callback}" do context "when local callback" do before do