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.

Examples:

You can access the libcurl easy interface through this class, every request is based on it. The simplest setup looks like that:


e = Ethon::Easy.new(url: "www.example.com")
e.prepare
e.perform
#=> :ok

You can the reuse this Easy for the next request:


e.reset # reset easy handle
e.url = "www.google.com"
e.followlocation = true
e.prepare
e.perform
#=> :ok

See Also:

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):



143
144
145
146
147
# File 'lib/ethon/easy.rb', line 143

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.



43
44
45
# File 'lib/ethon/easy.rb', line 43

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.



53
54
55
56
57
58
# File 'lib/ethon/easy.rb', line 53

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.



186
187
188
# File 'lib/ethon/easy.rb', line 186

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


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

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.

Raises:

  • InvalidOption

See Also:



159
160
161
162
163
164
165
166
# File 'lib/ethon/easy.rb', line 159

def set_attributes(options)
  options.each_pair do |key, value|
    unless respond_to?("#{key}=")
      raise Errors::InvalidOption.new(key)
    end
    method("#{key}=").call(value)
  end
end

#to_hashHash

Returns the informations available through libcurl as a hash.

Returns:

  • (Hash)

    The informations hash.



194
195
196
197
198
199
200
201
202
203
# File 'lib/ethon/easy.rb', line 194

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