Class: Typhoeus::Easy
- Inherits:
-
Object
- Object
- Typhoeus::Easy
- 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, # Time-out connect operations after this amount of milliseconds. # [Only works on unix-style/SIGALRM operating systems. IOW, does # not work on Windows. :CURLOPT_CONNECTTIMEOUT_MS => 156, :CURLOPT_INTERFACE => 10000 + 62, :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_PROXYUSERPWD => 10000 + 6, :CURLOPT_PROXYTYPE => 101, :CURLOPT_PROXYAUTH => 111, :CURLOPT_VERIFYPEER => 64, :CURLOPT_VERIFYHOST => 81, :CURLOPT_NOBODY => 44, :CURLOPT_ENCODING => 10000 + 102, :CURLOPT_SSLCERT => 10025, :CURLOPT_SSLCERTTYPE => 10086, :CURLOPT_SSLKEY => 10087, :CURLOPT_SSLKEYTYPE => 10088, :CURLOPT_SSLVERSION => 32, :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, :CURLINFO_PRIMARY_IP => 0x100000 + 32, :CURLINFO_NAMELOOKUP_TIME => 0x300000 + 4, :CURLINFO_CONNECT_TIME => 0x300000 + 5, :CURLINFO_PRETRANSFER_TIME => 0x300000 + 6, :CURLINFO_STARTTRANSFER_TIME => 0x300000 + 17, :CURLINFO_APPCONNECT_TIME => 0x300000 + 33, }
- AUTH_TYPES =
{ :CURLAUTH_BASIC => 1, :CURLAUTH_DIGEST => 2, :CURLAUTH_GSSNEGOTIATE => 4, :CURLAUTH_NTLM => 8, :CURLAUTH_DIGEST_IE => 16, :CURLAUTH_AUTO => 16 | 8 | 4 | 2 | 1 }
- PROXY_TYPES =
{ :CURLPROXY_HTTP => 0, :CURLPROXY_HTTP_1_0 => 1, :CURLPROXY_SOCKS4 => 4, :CURLPROXY_SOCKS5 => 5, :CURLPROXY_SOCKS4A => 6, }
- SSL_VERSIONS =
{ :CURL_SSLVERSION_DEFAULT => 0, :CURL_SSLVERSION_TLSv1 => 1, :CURL_SSLVERSION_SSLv2 => 2, :CURL_SSLVERSION_SSLv3 => 3, :default => 0, :tlsv1 => 1, :sslv2 => 2, :sslv3 => 3 }
Instance Attribute Summary collapse
-
#curl_return_code ⇒ Object
readonly
Returns the value of attribute curl_return_code.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#response_body ⇒ Object
readonly
Returns the value of attribute response_body.
-
#response_header ⇒ Object
readonly
Returns the value of attribute response_header.
-
#ssl_version ⇒ Object
Returns the value of attribute ssl_version.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #app_connect_time ⇒ Object
- #auth=(authinfo) ⇒ Object
- #auth_methods ⇒ Object
- #connect_time ⇒ Object
- #connect_timeout=(milliseconds) ⇒ Object
- #curl_version ⇒ Object
- #disable_ssl_host_verification ⇒ Object
- #disable_ssl_peer_verification ⇒ Object
- #effective_url ⇒ Object
- #encoding=(encoding) ⇒ Object
-
#failure ⇒ Object
gets called when finished and response code is 300-599 or curl returns an error code.
- #follow_location=(boolean) ⇒ Object
- #get_info_double(option) ⇒ Object
- #get_info_long(option) ⇒ Object
- #get_info_string(option) ⇒ Object
-
#initialize ⇒ Easy
constructor
A new instance of Easy.
- #interface=(interface) ⇒ Object
- #max_redirects=(redirects) ⇒ Object
- #name_lookup_time ⇒ Object
- #on_failure(&block) ⇒ Object
- #on_failure=(block) ⇒ Object
- #on_success(&block) ⇒ Object
- #on_success=(block) ⇒ Object
- #perform ⇒ Object
- #post_data=(data) ⇒ Object
- #pretransfer_time ⇒ Object
- #primary_ip ⇒ Object
- #proxy=(proxy) ⇒ Object
- #proxy_auth=(authinfo) ⇒ Object
- #request_body=(request_body) ⇒ Object
- #reset ⇒ Object
- #response_code ⇒ Object
- #set_defaults ⇒ Object
- #set_headers ⇒ Object
- #set_option(option, value) ⇒ Object
-
#ssl_cacert=(cacert) ⇒ Object
Set SSL CACERT “ File holding one or more certificates to verify the peer with.
-
#ssl_capath=(capath) ⇒ Object
Set CAPATH “ directory holding multiple CA certificates to verify the peer with.
-
#ssl_cert=(cert) ⇒ Object
Set SSL certificate “ The string should be the file name of your certificate.
-
#ssl_cert_type=(cert_type) ⇒ Object
Set SSL certificate type “ The string should be the format of your certificate.
-
#ssl_key=(key) ⇒ Object
Set SSL Key file “ The string should be the file name of your private key.
- #ssl_key_password=(key_password) ⇒ Object
-
#ssl_key_type=(key_type) ⇒ Object
Set SSL Key type “ The string should be the format of your private key.
- #start_transfer_time ⇒ Object
-
#success ⇒ Object
gets called when finished and response code is not 2xx, or curl returns an error code.
- #supports_zlib? ⇒ Boolean
- #timed_out? ⇒ Boolean
- #timeout=(milliseconds) ⇒ Object
- #total_time_taken ⇒ Object
- #user_agent=(user_agent) ⇒ Object
- #verbose=(boolean) ⇒ Object
Constructor Details
#initialize ⇒ Easy
Returns a new instance of Easy.
88 89 90 91 92 93 |
# File 'lib/typhoeus/easy.rb', line 88 def initialize @method = :get @headers = {} set_defaults end |
Instance Attribute Details
#curl_return_code ⇒ Object (readonly)
Returns the value of attribute curl_return_code.
3 4 5 |
# File 'lib/typhoeus/easy.rb', line 3 def curl_return_code @curl_return_code end |
#headers ⇒ Object
Returns the value of attribute headers.
3 4 5 |
# File 'lib/typhoeus/easy.rb', line 3 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
3 4 5 |
# File 'lib/typhoeus/easy.rb', line 3 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
3 4 5 |
# File 'lib/typhoeus/easy.rb', line 3 def params @params end |
#response_body ⇒ Object (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_header ⇒ Object (readonly)
Returns the value of attribute response_header.
3 4 5 |
# File 'lib/typhoeus/easy.rb', line 3 def response_header @response_header end |
#ssl_version ⇒ Object
Returns the value of attribute ssl_version.
3 4 5 |
# File 'lib/typhoeus/easy.rb', line 3 def ssl_version @ssl_version end |
#start_time ⇒ Object
Returns the value of attribute start_time.
4 5 6 |
# File 'lib/typhoeus/easy.rb', line 4 def start_time @start_time end |
#url ⇒ Object
Returns the value of attribute url.
3 4 5 |
# File 'lib/typhoeus/easy.rb', line 3 def url @url end |
Instance Method Details
#app_connect_time ⇒ Object
153 154 155 |
# File 'lib/typhoeus/easy.rb', line 153 def app_connect_time get_info_double(INFO_VALUES[:CURLINFO_APPCONNECT_TIME]) end |
#auth=(authinfo) ⇒ Object
132 133 134 135 |
# File 'lib/typhoeus/easy.rb', line 132 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_methods ⇒ Object
137 138 139 |
# File 'lib/typhoeus/easy.rb', line 137 def auth_methods get_info_long(INFO_VALUES[:CURLINFO_HTTPAUTH_AVAIL]) end |
#connect_time ⇒ Object
161 162 163 |
# File 'lib/typhoeus/easy.rb', line 161 def connect_time get_info_double(INFO_VALUES[:CURLINFO_CONNECT_TIME]) end |
#connect_timeout=(milliseconds) ⇒ Object
193 194 195 196 197 |
# File 'lib/typhoeus/easy.rb', line 193 def connect_timeout=(milliseconds) @connect_timeout = milliseconds set_option(OPTION_VALUES[:CURLOPT_NOSIGNAL], 1) set_option(OPTION_VALUES[:CURLOPT_CONNECTTIMEOUT_MS], milliseconds) end |
#curl_version ⇒ Object
408 409 410 |
# File 'lib/typhoeus/easy.rb', line 408 def curl_version version end |
#disable_ssl_host_verification ⇒ Object
235 236 237 |
# File 'lib/typhoeus/easy.rb', line 235 def disable_ssl_host_verification set_option(OPTION_VALUES[:CURLOPT_VERIFYHOST], 0) end |
#disable_ssl_peer_verification ⇒ Object
231 232 233 |
# File 'lib/typhoeus/easy.rb', line 231 def disable_ssl_peer_verification set_option(OPTION_VALUES[:CURLOPT_VERIFYPEER], 0) end |
#effective_url ⇒ Object
169 170 171 |
# File 'lib/typhoeus/easy.rb', line 169 def effective_url get_info_string(INFO_VALUES[:CURLINFO_EFFECTIVE_URL]) end |
#encoding=(encoding) ⇒ Object
101 102 103 104 |
# File 'lib/typhoeus/easy.rb', line 101 def encoding=(encoding) # Enable encoding/compression support set_option(OPTION_VALUES[:CURLOPT_ENCODING], encoding) end |
#failure ⇒ Object
gets called when finished and response code is 300-599 or curl returns an error code
375 376 377 |
# File 'lib/typhoeus/easy.rb', line 375 def failure @failure.call(self) if @failure end |
#follow_location=(boolean) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/typhoeus/easy.rb', line 181 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
404 405 406 |
# File 'lib/typhoeus/easy.rb', line 404 def get_info_double(option) easy_getinfo_double(option) end |
#get_info_long(option) ⇒ Object
400 401 402 |
# File 'lib/typhoeus/easy.rb', line 400 def get_info_long(option) easy_getinfo_long(option) end |
#get_info_string(option) ⇒ Object
396 397 398 |
# File 'lib/typhoeus/easy.rb', line 396 def get_info_string(option) easy_getinfo_string(option) end |
#interface=(interface) ⇒ Object
117 118 119 120 |
# File 'lib/typhoeus/easy.rb', line 117 def interface=(interface) @interface = interface set_option(OPTION_VALUES[:CURLOPT_INTERFACE], interface) end |
#max_redirects=(redirects) ⇒ Object
189 190 191 |
# File 'lib/typhoeus/easy.rb', line 189 def max_redirects=(redirects) set_option(OPTION_VALUES[:CURLOPT_MAXREDIRS], redirects) end |
#name_lookup_time ⇒ Object
165 166 167 |
# File 'lib/typhoeus/easy.rb', line 165 def name_lookup_time get_info_double(INFO_VALUES[:CURLINFO_NAMELOOKUP_TIME]) end |
#on_failure(&block) ⇒ Object
379 380 381 |
# File 'lib/typhoeus/easy.rb', line 379 def on_failure(&block) @failure = block end |
#on_failure=(block) ⇒ Object
383 384 385 |
# File 'lib/typhoeus/easy.rb', line 383 def on_failure=(block) @failure = block end |
#on_success(&block) ⇒ Object
365 366 367 |
# File 'lib/typhoeus/easy.rb', line 365 def on_success(&block) @success = block end |
#on_success=(block) ⇒ Object
369 370 371 |
# File 'lib/typhoeus/easy.rb', line 369 def on_success=(block) @success = block end |
#perform ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/typhoeus/easy.rb', line 340 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
256 257 258 259 260 |
# File 'lib/typhoeus/easy.rb', line 256 def post_data=(data) @post_data_set = true set_option(OPTION_VALUES[:CURLOPT_POSTFIELDSIZE], data.bytesize) set_option(OPTION_VALUES[:CURLOPT_COPYPOSTFIELDS], data) end |
#pretransfer_time ⇒ Object
157 158 159 |
# File 'lib/typhoeus/easy.rb', line 157 def pretransfer_time get_info_double(INFO_VALUES[:CURLINFO_PRETRANSFER_TIME]) end |
#primary_ip ⇒ Object
173 174 175 |
# File 'lib/typhoeus/easy.rb', line 173 def primary_ip get_info_string(INFO_VALUES[:CURLINFO_PRIMARY_IP]) end |
#proxy=(proxy) ⇒ Object
122 123 124 125 |
# File 'lib/typhoeus/easy.rb', line 122 def proxy=(proxy) set_option(OPTION_VALUES[:CURLOPT_PROXY], proxy[:server]) set_option(OPTION_VALUES[:CURLOPT_PROXYTYPE], proxy[:type]) if proxy[:type] end |
#proxy_auth=(authinfo) ⇒ Object
127 128 129 130 |
# File 'lib/typhoeus/easy.rb', line 127 def proxy_auth=(authinfo) set_option(OPTION_VALUES[:CURLOPT_PROXYUSERPWD], "#{authinfo[:username]}:#{authinfo[:password]}") set_option(OPTION_VALUES[:CURLOPT_PROXYAUTH], authinfo[:method]) if authinfo[:method] end |
#request_body=(request_body) ⇒ Object
213 214 215 216 217 218 219 220 |
# File 'lib/typhoeus/easy.rb', line 213 def request_body=(request_body) @request_body = request_body if @method == :put easy_set_request_body(@request_body.to_s) else self.post_data = request_body end end |
#reset ⇒ Object
387 388 389 390 391 392 393 394 |
# File 'lib/typhoeus/easy.rb', line 387 def reset @response_code = 0 @response_header = "" @response_body = "" @request_body = "" easy_reset() set_defaults end |
#response_code ⇒ Object
177 178 179 |
# File 'lib/typhoeus/easy.rb', line 177 def response_code get_info_long(INFO_VALUES[:CURLINFO_RESPONSE_CODE]) end |
#set_defaults ⇒ Object
95 96 97 98 99 |
# File 'lib/typhoeus/easy.rb', line 95 def set_defaults # Enable encoding/compression support self.encoding = '' self.ssl_version = :default end |
#set_headers ⇒ Object
352 353 354 355 356 357 |
# File 'lib/typhoeus/easy.rb', line 352 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
329 330 331 332 333 334 335 336 337 338 |
# File 'lib/typhoeus/easy.rb', line 329 def set_option(option, value) case value when String easy_setopt_string(option, value) when Typhoeus::Form easy_setopt_form(option, value) else easy_setopt_long(option, value) if value end end |
#ssl_cacert=(cacert) ⇒ Object
Set SSL CACERT “ File holding one or more certificates to verify the peer with. ”
318 319 320 |
# File 'lib/typhoeus/easy.rb', line 318 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. ”
325 326 327 |
# File 'lib/typhoeus/easy.rb', line 325 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=
284 285 286 |
# File 'lib/typhoeus/easy.rb', line 284 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“ ”
290 291 292 293 |
# File 'lib/typhoeus/easy.rb', line 290 def ssl_cert_type=(cert_type) raise "Invalid ssl cert type : '#{cert_type}'..." if cert_type and !%w(PEM DER p12).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=
299 300 301 |
# File 'lib/typhoeus/easy.rb', line 299 def ssl_key=(key) set_option(OPTION_VALUES[:CURLOPT_SSLKEY], key) end |
#ssl_key_password=(key_password) ⇒ Object
311 312 313 |
# File 'lib/typhoeus/easy.rb', line 311 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“. ”
306 307 308 309 |
# File 'lib/typhoeus/easy.rb', line 306 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 |
#start_transfer_time ⇒ Object
149 150 151 |
# File 'lib/typhoeus/easy.rb', line 149 def start_transfer_time get_info_double(INFO_VALUES[:CURLINFO_STARTTRANSFER_TIME]) end |
#success ⇒ Object
gets called when finished and response code is not 2xx, or curl returns an error code.
361 362 363 |
# File 'lib/typhoeus/easy.rb', line 361 def success @success.call(self) if @success end |
#supports_zlib? ⇒ Boolean
209 210 211 |
# File 'lib/typhoeus/easy.rb', line 209 def supports_zlib? !!(curl_version.match(/zlib/)) end |
#timed_out? ⇒ Boolean
205 206 207 |
# File 'lib/typhoeus/easy.rb', line 205 def timed_out? curl_return_code == 28 end |
#timeout=(milliseconds) ⇒ Object
199 200 201 202 203 |
# File 'lib/typhoeus/easy.rb', line 199 def timeout=(milliseconds) @timeout = milliseconds set_option(OPTION_VALUES[:CURLOPT_NOSIGNAL], 1) set_option(OPTION_VALUES[:CURLOPT_TIMEOUT_MS], milliseconds) end |
#total_time_taken ⇒ Object
145 146 147 |
# File 'lib/typhoeus/easy.rb', line 145 def total_time_taken get_info_double(INFO_VALUES[:CURLINFO_TOTAL_TIME]) end |
#user_agent=(user_agent) ⇒ Object
222 223 224 |
# File 'lib/typhoeus/easy.rb', line 222 def user_agent=(user_agent) set_option(OPTION_VALUES[:CURLOPT_USERAGENT], user_agent) end |
#verbose=(boolean) ⇒ Object
141 142 143 |
# File 'lib/typhoeus/easy.rb', line 141 def verbose=(boolean) set_option(OPTION_VALUES[:CURLOPT_VERBOSE], !!boolean ? 1 : 0) end |