module Typhoeus::Request::Callbacks

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.

request.on_complete { |response| p 1 }
request.on_complete { |response| p 2 }
request.execute_callbacks
#=> 1
#=> 2

You can clear the callbacks:

request.on_complete { |response| p 1 }
request.on_complete { |response| p 2 }
request.on_complete.clear
request.execute_callbacks
#=> []

@note If you're using the Hydra to execute multiple

requests, then callbacks are delaying the
request execution.

Public Instance Methods

execute_callbacks() click to toggle source

Execute necessary callback and yields response. This include in every case on_complete, on_success if successful and on_failure if not.

@example Execute callbacks.

request.execute_callbacks

@return [ void ]

@api private

# File lib/typhoeus/request/callbacks.rb, line 119
def execute_callbacks
  callbacks = Typhoeus.on_complete + on_complete

  if response && response.success?
    callbacks += Typhoeus.on_success + on_success
  elsif response
    callbacks += Typhoeus.on_failure + on_failure
  end

  callbacks.map do |callback|
    self.response.handled_response = callback.call(self.response)
  end
end
execute_headers_callbacks(response) click to toggle source

Execute the headers callbacks and yields response.

@example Execute callbacks.

request.execute_headers_callbacks

@return [ Array<Object> ] The results of the on_headers callbacks.

@api private

# File lib/typhoeus/request/callbacks.rb, line 103
def execute_headers_callbacks(response)
  (Typhoeus.on_headers + on_headers).map do |callback|
    callback.call(response)
  end
end