Module: Ethon::Easies::Callbacks

Included in:
Ethon::Easy
Defined in:
lib/ethon/easies/callbacks.rb

Overview

This module contains all the logic around the callbacks, which are needed to interact with libcurl.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



9
10
11
# File 'lib/ethon/easies/callbacks.rb', line 9

def self.included(base)
  base.send(:attr_accessor, *[:response_body, :response_header])
end

Instance Method Details

#body_write_callbackProc

Returns the body write callback.

Examples:

Return the callback.

easy.body_write_callback

Returns:

  • (Proc)

    The callback.



32
33
34
35
36
37
# File 'lib/ethon/easies/callbacks.rb', line 32

def body_write_callback
  @body_write_callback ||= proc {|stream, size, num, object|
    @response_body << stream.read_string(size * num)
    size * num
  }
end

#header_write_callbackProc

Returns the header write callback.

Examples:

Return the callback.

easy.header_write_callback

Returns:

  • (Proc)

    The callback.



45
46
47
48
49
50
# File 'lib/ethon/easies/callbacks.rb', line 45

def header_write_callback
  @header_write_callback ||= proc {|stream, size, num, object|
    @response_header << stream.read_string(size * num)
    size * num
  }
end

#read_callbackProc

Returns the body read callback.

Examples:

Return the callback.

easy.read_callback

Returns:

  • (Proc)

    The callback.



82
83
84
# File 'lib/ethon/easies/callbacks.rb', line 82

def read_callback
  @read_callback
end

#set_callbacksObject

Set writefunction and headerfunction callback. They are called by libcurl in order to provide the header and the body from the request.

Examples:

Set callbacks.

easy.set_callbacks


19
20
21
22
23
24
# File 'lib/ethon/easies/callbacks.rb', line 19

def set_callbacks
  Curl.set_option(:writefunction, body_write_callback, handle)
  Curl.set_option(:headerfunction, header_write_callback, handle)
  @response_body = ""
  @response_header = ""
end

#set_read_callback(body) ⇒ Object

Set the read callback. This callback is used by libcurl to read data when performing a PUT request.

Examples:

Set the callback.

easy.set_read_callback("a=1")

Parameters:

  • body (String)

    The body.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ethon/easies/callbacks.rb', line 59

def set_read_callback(body)
  @request_body_read = 0
  @read_callback = proc {|stream, size, num, object|
    size = size * num
    left = body.bytesize - @request_body_read
    size = left if size > left
    if size > 0
      stream.write_string(
        body.respond_to?(:byteslice) ? body.byteslice(@request_body_read, size) : body[@request_body_read, size], size
      )
      @request_body_read += size
    end
    size
  }
  self.readfunction = read_callback
end