Skip to content

Commit

Permalink
add on_progress callback
Browse files Browse the repository at this point in the history
  • Loading branch information
zachwalton committed Jul 16, 2018
1 parent fc089ea commit c790ca0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/typhoeus/easy_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 21 additions & 3 deletions lib/typhoeus/request/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Block> ] 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.
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/typhoeus/easy_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions spec/typhoeus/request/callbacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c790ca0

Please sign in to comment.