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



145
146
147
148
149
# File 'lib/ethon/easy.rb', line 145

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

#escape(value) ⇒ Object



182
183
184
# File 'lib/ethon/easy.rb', line 182

def escape(value)
  Curl.easy_escape(handle, value, 0)
end

#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.



192
193
194
# File 'lib/ethon/easy.rb', line 192

def handle
  @handle ||= Curl.easy_init
end

#log_inspectString

Return pretty log out.

Examples:

Return log out.

easy.log_inspect

Returns:

  • (String)

    The log out.



219
220
221
222
223
224
225
226
227
# File 'lib/ethon/easy.rb', line 219

def log_inspect
  hash = {
    :url => url,
    :response_code => response_code,
    :return_code => return_code,
    :total_time => total_time
  }
  "EASY #{hash.map{|k, v| "#{k}=#{v}"}.flatten.join(' ')}"
end

#resetObject

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

Examples:

Reset.

easy.reset


175
176
177
178
179
180
# File 'lib/ethon/easy.rb', line 175

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:



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

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.



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ethon/easy.rb', line 200

def to_hash
  return @hash if defined?(@hash) && @hash
  @hash = {
    :return_code => return_code,
    :response_header => response_header,
    :response_body => response_body
  }
  Easies::Informations::AVAILABLE_INFORMATIONS.keys.each do |info|
    @hash[info] = method(info).call
  end
  @hash
end