class Curl::Easy

Public Class Methods

Curl::Easy.download(url, filename = url.split(/\?/).first.split(/\//).last) { |curl| ... } click to toggle source

Stream the specified url (via perform) and save the data directly to the supplied filename (defaults to the last component of the URL path, which will usually be the filename most simple urls).

If a block is supplied, it will be passed the curl instance prior to the perform call.

Note that the semantics of the on_body handler are subtly changed when using download, to account for the automatic routing of data to the specified file: The data string is passed to the handler before it is written to the file, allowing the handler to perform mutative operations where necessary. As usual, the transfer will be aborted if the on_body handler returns a size that differs from the data chunk size - in this case, the offending chunk will not be written to the file, the file will be closed, and a Curl::Err::AbortedByCallbackError will be raised.

# File lib/curl/easy.rb, line 425
def download(url, filename = url.split(%r\?/).first.split(%r\//).last, &blk)
  curl = Curl::Easy.new(url, &blk)
  
  output = if filename.is_a? IO
    filename.binmode if filename.respond_to?(:binmode)
    filename
  else
    File.open(filename, 'wb')
  end
  
  begin
    old_on_body = curl.on_body do |data|
      result = old_on_body ?  old_on_body.call(data) : data.length
      output << data if result == data.length
      result
    end
    curl.perform
  ensure
    output.close rescue IOError
  end
  
  return curl
end
Curl::Easy.http_delete(url) { |easy| ... } → #&lt;Curl::Easy...&gt; click to toggle source

Convenience method that creates a new Curl::Easy instance with the specified URL and calls http_delete, before returning the new instance.

If a block is supplied, the new instance will be yielded just prior to the http_delete call.

# File lib/curl/easy.rb, line 400
def http_delete(*args)
  c = Curl::Easy.new *args
  yield c if block_given?
  c.http_delete
  c
end
Curl::Easy.http_get(url) { |easy| ... } → #&lt;Curl::Easy...&gt; click to toggle source

Convenience method that creates a new Curl::Easy instance with the specified URL and calls http_get, before returning the new instance.

If a block is supplied, the new instance will be yielded just prior to the http_get call.

# File lib/curl/easy.rb, line 329
def http_get(*args)
  c = Curl::Easy.new *args
  yield c if block_given?
  c.http_get
  c
end
Curl::Easy.http_head(url) { |easy| ... } → #&lt;Curl::Easy...&gt; click to toggle source

Convenience method that creates a new Curl::Easy instance with the specified URL and calls http_head, before returning the new instance.

If a block is supplied, the new instance will be yielded just prior to the http_head call.

# File lib/curl/easy.rb, line 346
def http_head(*args)
  c = Curl::Easy.new *args
  yield c if block_given?
  c.http_head
  c
end
Curl::Easy.http_post(url, "some=urlencoded%20form%20data&and=so%20on") → true click to toggle source
Curl::Easy.http_post(url, "some=urlencoded%20form%20data", "and=so%20on", ...) → true
Curl::Easy.http_post(url, "some=urlencoded%20form%20data", Curl::PostField, "and=so%20on", ...) → true
Curl::Easy.http_post(url, Curl::PostField, Curl::PostField ..., Curl::PostField) → true

POST the specified formdata to the currently configured URL using the current options set for this Curl::Easy instance. This method always returns true, or raises an exception (defined under Curl::Err) on error.

If you wish to use multipart form encoding, you’ll need to supply a block in order to set multipart_form_post true. See http_post for more information.

# File lib/curl/easy.rb, line 382
def http_post(*args)
  url = args.shift
  c = Curl::Easy.new url
  yield c if block_given?
  c.http_post *args
  c
end
Curl::Easy.http_put(url, data) {|c| ... } click to toggle source

see easy.http_put

# File lib/curl/easy.rb, line 359
def http_put(url, data)
  c = Curl::Easy.new url
  yield c if block_given?
  c.http_put data
  c
end
Curl::Easy.perform(url) { |easy| ... } → #&lt;Curl::Easy...&gt; click to toggle source

Convenience method that creates a new Curl::Easy instance with the specified URL and calls the general perform method, before returning the new instance. For HTTP URLs, this is equivalent to calling http_get.

If a block is supplied, the new instance will be yielded just prior to the http_get call.

# File lib/curl/easy.rb, line 312
def perform(*args)
  c = Curl::Easy.new *args
  yield c if block_given?
  c.perform
  c
end

Public Instance Methods

cert=(cert_file) click to toggle source
# File lib/curl/easy.rb, line 455
def cert=(cert_file)
  pos = cert_file.rindex(':')
  if pos && pos > 1
    self.native_cert= cert_file[0..pos-1]
    self.certpassword= cert_file[pos+1..-1]
  else
    self.native_cert= cert_file
  end
  self.cert
end
cookiefile = string → string click to toggle source

Set a file that contains cookies to be sent in subsequent requests by this Curl::Easy instance.

Note that you must set enable_cookies true to enable the cookie engine, or this option will be ignored.

# File lib/curl/easy.rb, line 215
def cookiefile=(value)
  set :cookiefile, value
end
cookiejar = string → string click to toggle source

Set a cookiejar file to use for this Curl::Easy instance. Cookies from the response will be written into this file.

Note that you must set enable_cookies true to enable the cookie engine, or this option will be ignored.

# File lib/curl/easy.rb, line 229
def cookiejar=(value)
  set :cookiejar, value
end
cookies = "name1=content1; name2=content2;" → string click to toggle source

Set cookies to be sent by this Curl::Easy instance. The format of the string should be NAME=CONTENTS, where NAME is the cookie name and CONTENTS is what the cookie should contain. Set multiple cookies in one string like this: “name1=content1; name2=content2;” etc.

# File lib/curl/easy.rb, line 202
def cookies=(value)
  set :cookie, value
end
delete() click to toggle source
Alias for: http_delete
easy = Curl::Easy.new("url") do|c| click to toggle source
delete = true
end
perform
# File lib/curl/easy.rb, line 83
def delete=(onoff)
  set :customrequest, onoff ? 'delete' : nil
  onoff
end
follow_location = boolean → boolean click to toggle source

Configure whether this Curl instance will follow Location: headers in HTTP responses. Redirects will only be followed to the extent specified by max_redirects.

# File lib/curl/easy.rb, line 252
def follow_location=(onoff)
  set :followlocation, onoff
end
get() click to toggle source
Alias for: http_get
easy = Curl::Easy.new("url") do|c| click to toggle source
head = true
end
perform
# File lib/curl/easy.rb, line 240
def head=(onoff)
  set :nobody, onoff
end
http_delete click to toggle source

DELETE the currently configured URL using the current options set for this Curl::Easy instance. This method always returns true, or raises an exception (defined under Curl::Err) on error.

# File lib/curl/easy.rb, line 294
def http_delete
  self.http :DELETE
end
Also aliased as: delete
http_get → true click to toggle source

GET the currently configured URL using the current options set for this Curl::Easy instance. This method always returns true, or raises an exception (defined under Curl::Err) on error.

# File lib/curl/easy.rb, line 280
def http_get
  set :httpget, true
  http :GET
end
Also aliased as: get
http_head → true click to toggle source

Request headers from the currently configured URL using the HEAD method and current options set for this Curl::Easy instance. This method always returns true, or raises an exception (defined under Curl::Err) on error.

# File lib/curl/easy.rb, line 265
def http_head
  set :nobody, true
  ret = self.perform
  set :nobody, false
  ret
end
interface = string → string click to toggle source

Set the interface name to use as the outgoing network interface. The name can be an interface name, an IP address or a host name.

# File lib/curl/easy.rb, line 167
def interface=(value)
  set :interface, value
end

easy = Curl::Easy.new easy.nosignal = true

# File lib/curl/easy.rb, line 72
def nosignal=(onoff)
  set :nosignal, !!onoff
end
perform → true click to toggle source

Transfer the currently configured URL using the options set for this Curl::Easy instance. If this is an HTTP URL, it will be transferred via the configured HTTP Verb.

# File lib/curl/easy.rb, line 53
def perform
  self.multi = Curl::Multi.new if self.multi.nil?
  self.multi.add self
  ret = self.multi.perform

  if self.last_result != 0 && self.on_failure.nil?
    error = Curl::Easy.error(self.last_result) 
    raise error.first
  end

  ret
end
proxy_url = string → string click to toggle source

Set the URL of the HTTP proxy to use for subsequent calls to perform. The URL should specify the the host name or dotted IP address. To specify port number in this string, append :[port] to the end of the host name. The proxy string may be prefixed with [protocol]:// since any such prefix will be ignored. The proxy’s port number may optionally be specified with the separate option proxy_port .

When you tell the library to use an HTTP proxy, libcurl will transparently convert operations to HTTP even if you specify an FTP URL etc. This may have an impact on what other features of the library you can use, such as FTP specifics that don’t work unless you tunnel through the HTTP proxy. Such tunneling is activated with proxy_tunnel = true.

libcurl respects the environment variables http_proxy, ftp_proxy, all_proxy etc, if any of those is set. The proxy_url option does however override any possibly set environment variables.

Starting with libcurl 7.14.1, the proxy host string given in environment variables can be specified the exact same way as the proxy can be set with proxy_url, including protocol prefix (http://) and embedded user + password.

# File lib/curl/easy.rb, line 136
def proxy_url=(url)
  set :proxy, url
end
proxypwd = string → string click to toggle source

Set the username/password string to use for proxy connection during subsequent calls to perform. The supplied string should have the form “username:password”

# File lib/curl/easy.rb, line 190
def proxypwd=(value)
  set :proxyuserpwd, value
end
set :sym|Fixnum, value click to toggle source

set options on the curl easy handle see curl.haxx.se/libcurl/c/curl_easy_setopt.html

# File lib/curl/easy.rb, line 27
def set(opt,val)
  if opt.is_a?(Symbol)
    setopt(sym2curl(opt), val)
  else
    setopt(opt.to_i, val)
  end
end
ssl_verify_host=(value) click to toggle source
# File lib/curl/easy.rb, line 140
def ssl_verify_host=(value)
  value = 1 if value.class == TrueClass
  value = 0 if value.class == FalseClass
  self.ssl_verify_host_integer=value
end
ssl_verify_host? → boolean click to toggle source

Deprecated: call easy.ssl_verify_host instead can be one of [0,1,2]

Determine whether this Curl instance will verify that the server cert is for the server it is known as.

# File lib/curl/easy.rb, line 156
def ssl_verify_host?
  ssl_verify_host.nil? ? false : (ssl_verify_host > 0)
end
status → String click to toggle source
# File lib/curl/easy.rb, line 11
def status
  parts = self.header_str.split(%r\s/)
  status = []
  parts.shift
  while parts.size > 0 && parts.first != ''
    status << parts.shift
  end
  status.join(' ')
end
sym2curl :symbol → Fixnum click to toggle source

translates ruby symbols to libcurl options

# File lib/curl/easy.rb, line 41
def sym2curl(opt)
  Curl.const_get("CURLOPT_#{opt.to_s.upcase}")
end
url = "http://some.url/" → "http://some.url/" click to toggle source

Set the URL for subsequent calls to perform. It is acceptable (and even recommended) to reuse Curl::Easy instances by reassigning the URL between calls to perform.

# File lib/curl/easy.rb, line 107
def url=(u)
  set :url, u
end
userpwd = string → string click to toggle source

Set the username/password string to use for subsequent calls to perform. The supplied string should have the form “username:password”

# File lib/curl/easy.rb, line 178
def userpwd=(value)
  set :userpwd, value
end

easy = Curl::Easy.new(“url”) easy.version = Curl::HTTP_1_1 easy.version = Curl::HTTP_1_0 easy.version = Curl::HTTP_NONE

# File lib/curl/easy.rb, line 95
def version=(http_version)
  set :http_version, http_version
end