Module: Typhoeus::Request::Callbacks

Included in:
Typhoeus::Request
Defined in:
lib/typhoeus/request/callbacks.rb

Overview

Note:

If you’re using the Hydra to execute multiple requests, then callbacks are delaying the request execution.

This module contains the logic for the response callbacks.

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
#=> nil

Since:

  • 0.5.0

Defined Under Namespace

Modules: Types

Instance Method Summary collapse

Instance Method Details

#execute_callbacksvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

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

Examples:

Execute callbacks.

request.execute_callbacks

Since:

  • 0.5.0



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/typhoeus/request/callbacks.rb', line 118

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.each do |callback|
    self.response.handled_response = callback.call(self.response)
  end
end

#execute_headers_callbacks(response) ⇒ Array<Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute the headers callbacks and yields response.

Examples:

Execute callbacks.

request.execute_headers_callbacks

Returns:

  • (Array<Object>)

    The results of the on_headers callbacks.

Since:

  • 0.5.0



102
103
104
105
106
# File 'lib/typhoeus/request/callbacks.rb', line 102

def execute_headers_callbacks(response)
  (Typhoeus.on_headers + on_headers).map do |callback|
    callback.call(response)
  end
end