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

Instance Method Details

#body(chunk) ⇒ Object

Execute on_body callbacks.

Examples:

Execute on_body.

request.body("This data came from HTTP.")

Returns:

  • (Object)

    If there are no on_body callbacks, returns the symbol :unyielded.


116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ethon/easy/response_callbacks.rb', line 116

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

#completeObject

Execute on_complete callbacks.

Examples:

Execute on_completes.

request.complete

65
66
67
68
69
70
# File 'lib/ethon/easy/response_callbacks.rb', line 65

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

#headersObject

Execute on_headers callbacks.

Examples:

Execute on_headers.

request.headers

41
42
43
44
45
46
47
# File 'lib/ethon/easy/response_callbacks.rb', line 41

def headers
  return if @headers_called
  @headers_called = true
  if defined?(@on_headers) and not @on_headers.nil?
    @on_headers.each{ |callback| callback.call(self) }
  end
end

#on_body(&block) ⇒ Object

Set on_body callback.

Examples:

Set on_body.

request.on_body { |chunk| p "yay" }

Parameters:

  • block (Block)

    The block to execute.


104
105
106
107
108
# File 'lib/ethon/easy/response_callbacks.rb', line 104

def on_body(&block)
  @on_body ||= []
  @on_body << block if block_given?
  @on_body
end

#on_complete(&block) ⇒ Object

Set on_complete callback.

Examples:

Set on_complete.

request.on_complete { p "yay" }

Parameters:

  • block (Block)

    The block to execute.


55
56
57
58
59
# File 'lib/ethon/easy/response_callbacks.rb', line 55

def on_complete(&block)
  @on_complete ||= []
  @on_complete << block if block_given?
  @on_complete
end

#on_headers(&block) ⇒ Object

Set on_headers callback.

Examples:

Set on_headers.

request.on_headers { p "yay" }

Parameters:

  • block (Block)

    The block to execute.


31
32
33
34
35
# File 'lib/ethon/easy/response_callbacks.rb', line 31

def on_headers(&block)
  @on_headers ||= []
  @on_headers << block if block_given?
  @on_headers
end

#on_progress(&block) ⇒ Object

Set on_progress callback.

Examples:

Set on_progress.

request.on_progress {|dltotal, dlnow, ultotal, ulnow| p "#{dltotal} #{dlnow} #{ultotal} #{ulnow}" }

Parameters:

  • block (Block)

    The block to execute.


78
79
80
81
82
83
84
85
86
# File 'lib/ethon/easy/response_callbacks.rb', line 78

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.

Examples:

Execute on_progress.

request.body(1, 1, 1, 1)

92
93
94
95
96
# File 'lib/ethon/easy/response_callbacks.rb', line 92

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