Class: Typhoeus::Easy

Inherits:
Object
  • Object
show all
Defined in:
lib/typhoeus/easy.rb,
ext/typhoeus/typhoeus_easy.c

Constant Summary collapse

CURLINFO_STRING =

These integer codes are available in curl/curl.h

1048576
OPTION_VALUES =
{
  :CURLOPT_URL            => 10002,
  :CURLOPT_HTTPGET        => 80,
  :CURLOPT_HTTPPOST       => 10024,
  :CURLOPT_UPLOAD         => 46,
  :CURLOPT_CUSTOMREQUEST  => 10036,
  :CURLOPT_POSTFIELDS     => 10015,
  :CURLOPT_COPYPOSTFIELDS     => 10165,
  :CURLOPT_POSTFIELDSIZE  => 60,
  :CURLOPT_USERAGENT      => 10018,
  :CURLOPT_TIMEOUT_MS     => 155,
  :CURLOPT_NOSIGNAL       => 99,
  :CURLOPT_HTTPHEADER     => 10023,
  :CURLOPT_FOLLOWLOCATION => 52,
  :CURLOPT_MAXREDIRS      => 68,
  :CURLOPT_HTTPAUTH       => 107,
  :CURLOPT_USERPWD        => 10000 + 5,
  :CURLOPT_VERBOSE        => 41,
  :CURLOPT_PROXY          => 10004,
  :CURLOPT_VERIFYPEER     => 64,
  :CURLOPT_NOBODY         => 44,
  :CURLOPT_ENCODING       => 102,
  :CURLOPT_SSLCERT        => 10025,
  :CURLOPT_SSLCERTTYPE    => 10086,
  :CURLOPT_SSLKEY         => 10087,
  :CURLOPT_SSLKEYTYPE     => 10088,
  :CURLOPT_KEYPASSWD      => 10026,
  :CURLOPT_CAINFO         => 10065,
  :CURLOPT_CAPATH         => 10097
}
INFO_VALUES =
{
  :CURLINFO_RESPONSE_CODE => 2097154,
  :CURLINFO_TOTAL_TIME    => 3145731,
  :CURLINFO_HTTPAUTH_AVAIL => 0x200000 + 23,
  :CURLINFO_EFFECTIVE_URL => 0x100000 + 1
}
AUTH_TYPES =
{
  :CURLAUTH_BASIC         => 1,
  :CURLAUTH_DIGEST        => 2,
  :CURLAUTH_GSSNEGOTIATE  => 4,
  :CURLAUTH_NTLM          => 8,
  :CURLAUTH_DIGEST_IE     => 16
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEasy

Returns a new instance of Easy.



52
53
54
55
56
57
# File 'lib/typhoeus/easy.rb', line 52

def initialize
  @method = :get
  @headers = {}

  set_option(OPTION_VALUES[:CURLOPT_ENCODING], 'zlib') if supports_zlib?
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



3
4
5
# File 'lib/typhoeus/easy.rb', line 3

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



3
4
5
# File 'lib/typhoeus/easy.rb', line 3

def method
  @method
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/typhoeus/easy.rb', line 3

def params
  @params
end

#response_bodyObject (readonly)

Returns the value of attribute response_body.



3
4
5
# File 'lib/typhoeus/easy.rb', line 3

def response_body
  @response_body
end

#response_headerObject (readonly)

Returns the value of attribute response_header.



3
4
5
# File 'lib/typhoeus/easy.rb', line 3

def response_header
  @response_header
end

#start_timeObject

Returns the value of attribute start_time.



4
5
6
# File 'lib/typhoeus/easy.rb', line 4

def start_time
  @start_time
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/typhoeus/easy.rb', line 3

def url
  @url
end

Instance Method Details

#auth=(authinfo) ⇒ Object



67
68
69
70
# File 'lib/typhoeus/easy.rb', line 67

def auth=(authinfo)
  set_option(OPTION_VALUES[:CURLOPT_USERPWD], "#{authinfo[:username]}:#{authinfo[:password]}")
  set_option(OPTION_VALUES[:CURLOPT_HTTPAUTH], authinfo[:method]) if authinfo[:method]
end

#auth_methodsObject



72
73
74
# File 'lib/typhoeus/easy.rb', line 72

def auth_methods
  get_info_long(INFO_VALUES[:CURLINFO_HTTPAUTH_AVAIL])
end

#curl_versionObject



324
325
326
# File 'lib/typhoeus/easy.rb', line 324

def curl_version
  version
end

#disable_ssl_peer_verificationObject



138
139
140
# File 'lib/typhoeus/easy.rb', line 138

def disable_ssl_peer_verification
  set_option(OPTION_VALUES[:CURLOPT_VERIFYPEER], 0)
end

#effective_urlObject



84
85
86
# File 'lib/typhoeus/easy.rb', line 84

def effective_url
  get_info_string(INFO_VALUES[:CURLINFO_EFFECTIVE_URL])
end

#failureObject

gets called when finished and response code is 300-599



275
276
277
# File 'lib/typhoeus/easy.rb', line 275

def failure
  @failure.call(self) if @failure
end

#follow_location=(boolean) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/typhoeus/easy.rb', line 92

def follow_location=(boolean)
  if boolean
    set_option(OPTION_VALUES[:CURLOPT_FOLLOWLOCATION], 1)
  else
    set_option(OPTION_VALUES[:CURLOPT_FOLLOWLOCATION], 0)
  end
end

#get_info_double(option) ⇒ Object



320
321
322
# File 'lib/typhoeus/easy.rb', line 320

def get_info_double(option)
  easy_getinfo_double(option)
end

#get_info_long(option) ⇒ Object



316
317
318
# File 'lib/typhoeus/easy.rb', line 316

def get_info_long(option)
  easy_getinfo_long(option)
end

#get_info_string(option) ⇒ Object



312
313
314
# File 'lib/typhoeus/easy.rb', line 312

def get_info_string(option)
  easy_getinfo_string(option)
end

#increment_retriesObject



291
292
293
294
# File 'lib/typhoeus/easy.rb', line 291

def increment_retries
  @retries ||= 0
  @retries += 1
end

#max_redirects=(redirects) ⇒ Object



100
101
102
# File 'lib/typhoeus/easy.rb', line 100

def max_redirects=(redirects)
  set_option(OPTION_VALUES[:CURLOPT_MAXREDIRS], redirects)
end

#max_retriesObject



296
297
298
# File 'lib/typhoeus/easy.rb', line 296

def max_retries
  @max_retries ||= 40
end

#max_retries?Boolean

Returns:

  • (Boolean)


300
301
302
# File 'lib/typhoeus/easy.rb', line 300

def max_retries?
  retries >= max_retries
end

#on_failure(&block) ⇒ Object



279
280
281
# File 'lib/typhoeus/easy.rb', line 279

def on_failure(&block)
  @failure = block
end

#on_failure=(block) ⇒ Object



283
284
285
# File 'lib/typhoeus/easy.rb', line 283

def on_failure=(block)
  @failure = block
end

#on_success(&block) ⇒ Object



266
267
268
# File 'lib/typhoeus/easy.rb', line 266

def on_success(&block)
  @success = block
end

#on_success=(block) ⇒ Object



270
271
272
# File 'lib/typhoeus/easy.rb', line 270

def on_success=(block)
  @success = block
end

#performObject



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/typhoeus/easy.rb', line 242

def perform
  set_headers()
  easy_perform()
  resp_code = response_code() 
  if resp_code >= 200 && resp_code <= 299
    success
  else
    failure
  end
  resp_code
end

#post_data=(data) ⇒ Object



159
160
161
162
163
# File 'lib/typhoeus/easy.rb', line 159

def post_data=(data)
  @post_data_set = true
  set_option(OPTION_VALUES[:CURLOPT_POSTFIELDSIZE], data.length)
  set_option(OPTION_VALUES[:CURLOPT_COPYPOSTFIELDS], data)
end

#proxy=(proxy) ⇒ Object



63
64
65
# File 'lib/typhoeus/easy.rb', line 63

def proxy=(proxy)
  set_option(OPTION_VALUES[:CURLOPT_PROXY], proxy)
end

#request_body=(request_body) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/typhoeus/easy.rb', line 118

def request_body=(request_body)
  @request_body = request_body
  if @method == :put
    easy_set_request_body(@request_body)
    headers["Transfer-Encoding"] = ""
    headers["Expect"] = ""
  else
    self.post_data = request_body
  end
end

#resetObject



304
305
306
307
308
309
310
# File 'lib/typhoeus/easy.rb', line 304

def reset
  @retries = 0
  @response_code = 0
  @response_header = ""
  @response_body = ""
  easy_reset()
end

#response_codeObject



88
89
90
# File 'lib/typhoeus/easy.rb', line 88

def response_code
  get_info_long(INFO_VALUES[:CURLINFO_RESPONSE_CODE])
end

#retriesObject



287
288
289
# File 'lib/typhoeus/easy.rb', line 287

def retries
  @retries ||= 0
end

#set_headersObject



254
255
256
257
258
259
# File 'lib/typhoeus/easy.rb', line 254

def set_headers
  headers.each_pair do |key, value|
    easy_add_header("#{key}: #{value}")
  end
  easy_set_headers() unless headers.empty?
end

#set_option(option, value) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/typhoeus/easy.rb', line 234

def set_option(option, value)
  if value.class == String
    easy_setopt_string(option, value)
  elsif value
    easy_setopt_long(option, value)
  end
end

#ssl_cacert=(cacert) ⇒ Object

Set SSL CACERT “ File holding one or more certificates to verify the peer with. ”



223
224
225
# File 'lib/typhoeus/easy.rb', line 223

def ssl_cacert=(cacert)
  set_option(OPTION_VALUES[:CURLOPT_CAINFO], cacert)
end

#ssl_capath=(capath) ⇒ Object

Set CAPATH “ directory holding multiple CA certificates to verify the peer with. The certificate directory must be prepared using the openssl c_rehash utility. ”



230
231
232
# File 'lib/typhoeus/easy.rb', line 230

def ssl_capath=(capath)
  set_option(OPTION_VALUES[:CURLOPT_CAPATH], capath)
end

#ssl_cert=(cert) ⇒ Object

Set SSL certificate “ The string should be the file name of your certificate. ” The default format is “PEM” and can be changed with ssl_cert_type=



189
190
191
# File 'lib/typhoeus/easy.rb', line 189

def ssl_cert=(cert)
  set_option(OPTION_VALUES[:CURLOPT_SSLCERT], cert)
end

#ssl_cert_type=(cert_type) ⇒ Object

Set SSL certificate type “ The string should be the format of your certificate. Supported formats are ”PEM“ and ”DER“ ”



195
196
197
198
# File 'lib/typhoeus/easy.rb', line 195

def ssl_cert_type=(cert_type)
  raise "Invalid ssl cert type : '#{cert_type}'..." if cert_type and !%w(PEM DER).include?(cert_type) 
  set_option(OPTION_VALUES[:CURLOPT_SSLCERTTYPE], cert_type)
end

#ssl_key=(key) ⇒ Object

Set SSL Key file “ The string should be the file name of your private key. ” The default format is “PEM” and can be changed with ssl_key_type=



204
205
206
# File 'lib/typhoeus/easy.rb', line 204

def ssl_key=(key)
  set_option(OPTION_VALUES[:CURLOPT_SSLKEY], key)
end

#ssl_key_password=(key_password) ⇒ Object



216
217
218
# File 'lib/typhoeus/easy.rb', line 216

def ssl_key_password=(key_password)
  set_option(OPTION_VALUES[:CURLOPT_KEYPASSWD], key_password)
end

#ssl_key_type=(key_type) ⇒ Object

Set SSL Key type “ The string should be the format of your private key. Supported formats are ”PEM“, ”DER“ and ”ENG“. ”



211
212
213
214
# File 'lib/typhoeus/easy.rb', line 211

def ssl_key_type=(key_type)
  raise "Invalid ssl key type : '#{key_type}'..." if key_type and !%w(PEM DER ENG).include?(key_type)
  set_option(OPTION_VALUES[:CURLOPT_SSLKEYTYPE], key_type)
end

#successObject

gets called when finished and response code is 200-299



262
263
264
# File 'lib/typhoeus/easy.rb', line 262

def success
  @success.call(self) if @success
end

#supports_zlib?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/typhoeus/easy.rb', line 114

def supports_zlib?
  !!(curl_version.match(/zlib/))
end

#timed_out?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/typhoeus/easy.rb', line 110

def timed_out?
  @timeout && total_time_taken > @timeout && response_code == 0
end

#timeout=(milliseconds) ⇒ Object



104
105
106
107
108
# File 'lib/typhoeus/easy.rb', line 104

def timeout=(milliseconds)
  @timeout = milliseconds
  set_option(OPTION_VALUES[:CURLOPT_NOSIGNAL], 1)
  set_option(OPTION_VALUES[:CURLOPT_TIMEOUT_MS], milliseconds)
end

#total_time_takenObject



80
81
82
# File 'lib/typhoeus/easy.rb', line 80

def total_time_taken
  get_info_double(INFO_VALUES[:CURLINFO_TOTAL_TIME])
end

#user_agent=(user_agent) ⇒ Object



129
130
131
# File 'lib/typhoeus/easy.rb', line 129

def user_agent=(user_agent)
  set_option(OPTION_VALUES[:CURLOPT_USERAGENT], user_agent)
end

#verbose=(boolean) ⇒ Object



76
77
78
# File 'lib/typhoeus/easy.rb', line 76

def verbose=(boolean)
  set_option(OPTION_VALUES[:CURLOPT_VERBOSE], !!boolean ? 1 : 0)
end