Class: Ethon::Easy

Overview

This is the class representing the libcurl easy interface See curl.haxx.se/libcurl/c/libcurl-easy.html for more informations.

Constant Summary

Constants included from Ethon::Easies::Informations

Ethon::Easies::Informations::AVAILABLE_INFORMATIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ethon::Easies::ResponseCallbacks

#complete, #on_complete

Methods included from Ethon::Easies::Operations

#perform, #prepare

Methods included from Ethon::Easies::Http

#http_request

Methods included from Ethon::Easies::Header

#compose_header, #header_list, #headers, #headers=, #set_headers

Methods included from Ethon::Easies::Options

included, #set_options, #value_for

Methods included from Ethon::Easies::Callbacks

#body_write_callback, #header_write_callback, included, #read_callback, #set_callbacks, #set_read_callback

Methods included from Ethon::Easies::Informations

#supports_zlib?

Constructor Details

#initialize(options = {}) ⇒ Easy

Initialize a new Easy. It initializes curl, if not already done and applies the provided options.

Examples:

Create a new Easy.

Easy.new(:url => "www.google.de")

Parameters:

  • options (Hash) (defaults to: {})

    The options to set.

Options Hash (options):



123
124
125
126
127
# File 'lib/ethon/easy.rb', line 123

def initialize(options = {})
  Curl.init
  ObjectSpace.define_finalizer(self, self.class.finalizer(self))
  set_attributes(options)
end

Instance Attribute Details

#return_codeObject

Returns the value of attribute return_code.



25
26
27
# File 'lib/ethon/easy.rb', line 25

def return_code
  @return_code
end

Class Method Details

.finalizer(easy) ⇒ Object

Free libcurl representation from an easy handle.

Examples:

Free easy handle.

Easy.finalizer(easy)

Parameters:

  • easy (Easy)

    The easy to free.



35
36
37
38
39
40
# File 'lib/ethon/easy.rb', line 35

def finalizer(easy)
  proc {
    Curl.slist_free_all(easy.header_list) if easy.header_list
    Curl.easy_cleanup(easy.handle)
  }
end

Instance Method Details

#handleFFI::Pointer

Returns a pointer to the curl easy handle.

Examples:

Return the handle.

easy.handle

Returns:

  • (FFI::Pointer)

    A pointer to the curl easy handle.



161
162
163
# File 'lib/ethon/easy.rb', line 161

def handle
  @handle ||= Curl.easy_init
end

#resetObject

Reset easy. This means resetting all options and instance variables. Also the easy handle is resetted.

Examples:

Reset.

easy.reset


148
149
150
151
152
153
# File 'lib/ethon/easy.rb', line 148

def reset
  (instance_variables - [:@handle, :@header_list]).each do |ivar|
    instance_variable_set(ivar, nil)
  end
  Curl.easy_reset(handle)
end

#set_attributes(options) ⇒ Object

Set given options.

Examples:

Set options.

easy.set_attributes(options)

Parameters:

  • options (Hash)

    The options.

See Also:



137
138
139
140
141
# File 'lib/ethon/easy.rb', line 137

def set_attributes(options)
  options.each_pair do |key, value|
    method("#{key}=").call(value) if respond_to?("#{key}=")
  end
end

#to_hashHash

Returns the informations available through libcurl as a hash.

Returns:

  • (Hash)

    The informations hash.



169
170
171
172
173
174
175
176
177
178
# File 'lib/ethon/easy.rb', line 169

def to_hash
  hash = {}
  hash[:return_code] = return_code
  hash[:response_header] = response_header
  hash[:response_body] = response_body
  Easies::Informations::AVAILABLE_INFORMATIONS.keys.each do |info|
    hash[info] = method(info).call
  end
  hash
end