Module: Ethon::Easy::ResponseCallbacks
- Included in:
- Ethon::Easy
- Defined in:
- lib/ethon/easy/response_callbacks.rb
Overview
This module contains the logic for the response callbacks. The on_complete callback is the only one at the moment.
You can set multiple callbacks, which are then executed in the same order.
easy.on_complete { p 1 }
easy.on_complete { p 2 }
easy.complete
#=> 1
#=> 2
You can clear the callbacks:
easy.on_complete { p 1 }
easy.on_complete { p 2 }
easy.on_complete.clear
easy.on_complete
#=> []
Instance Method Summary collapse
-
#body(chunk) ⇒ Object
Execute on_body callbacks.
-
#complete ⇒ Object
Execute on_complete callbacks.
-
#headers ⇒ Object
Execute on_headers callbacks.
-
#on_body(&block) ⇒ Object
Set on_body callback.
-
#on_complete(&block) ⇒ Object
Set on_complete callback.
-
#on_headers(&block) ⇒ Object
Set on_headers callback.
-
#on_progress(&block) ⇒ Object
Set on_progress callback.
-
#progress(dltotal, dlnow, ultotal, ulnow) ⇒ Object
Execute on_progress callbacks.
Instance Method Details
#body(chunk) ⇒ Object
Execute on_body callbacks.
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ethon/easy/response_callbacks.rb', line 122 def body(chunk) if defined?(@on_body) and not @on_body.nil? result = nil @on_body.each do |callback| result = callback.call(chunk, self) break if result == :abort end result else :unyielded end end |
#complete ⇒ Object
Execute on_complete callbacks.
71 72 73 74 75 76 |
# File 'lib/ethon/easy/response_callbacks.rb', line 71 def complete headers unless @response_headers.empty? if defined?(@on_complete) and not @on_complete.nil? @on_complete.each{ |callback| callback.call(self) } end end |
#headers ⇒ Object
Execute on_headers callbacks.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ethon/easy/response_callbacks.rb', line 42 def headers return if @headers_called @headers_called = true if defined?(@on_headers) and not @on_headers.nil? result = nil @on_headers.each do |callback| result = callback.call(self) break if result == :abort end result end end |
#on_body(&block) ⇒ Object
Set on_body callback.
110 111 112 113 114 |
# File 'lib/ethon/easy/response_callbacks.rb', line 110 def on_body(&block) @on_body ||= [] @on_body << block if block_given? @on_body end |
#on_complete(&block) ⇒ Object
Set on_complete callback.
61 62 63 64 65 |
# File 'lib/ethon/easy/response_callbacks.rb', line 61 def on_complete(&block) @on_complete ||= [] @on_complete << block if block_given? @on_complete end |
#on_headers(&block) ⇒ Object
Set on_headers callback.
32 33 34 35 36 |
# File 'lib/ethon/easy/response_callbacks.rb', line 32 def on_headers(&block) @on_headers ||= [] @on_headers << block if block_given? @on_headers end |
#on_progress(&block) ⇒ Object
Set on_progress callback.
84 85 86 87 88 89 90 91 92 |
# File 'lib/ethon/easy/response_callbacks.rb', line 84 def on_progress(&block) @on_progress ||= [] if block_given? @on_progress << block set_progress_callback self.noprogress = 0 end @on_progress end |
#progress(dltotal, dlnow, ultotal, ulnow) ⇒ Object
Execute on_progress callbacks.
98 99 100 101 102 |
# File 'lib/ethon/easy/response_callbacks.rb', line 98 def progress(dltotal, dlnow, ultotal, ulnow) if defined?(@on_progress) and not @on_progress.nil? @on_progress.each{ |callback| callback.call(dltotal, dlnow, ultotal, ulnow) } end end |