Class: Typhoeus::Easy

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

Constant Summary collapse

CURLINFO_STRING =
1048576
OPTION_VALUES =
{
  :CURLOPT_URL            => 10002,
  :CURLOPT_HTTPGET        => 80,
  :CURLOPT_HTTPPOST       => 10024,
  :CURLOPT_UPLOAD         => 46,
  :CURLOPT_CUSTOMREQUEST  => 10036,
  :CURLOPT_POSTFIELDS     => 10015,
  :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
}
INFO_VALUES =
{
  :CURLINFO_RESPONSE_CODE => 2097154,
  :CURLINFO_TOTAL_TIME    => 3145731,
  :CURLINFO_HTTPAUTH_AVAIL => 0x200000 + 23
}
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.



41
42
43
44
45
# File 'lib/typhoeus/easy.rb', line 41

def initialize
  @method = :get
  @post_dat_set = nil
  @headers = {}
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

#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



55
56
57
58
# File 'lib/typhoeus/easy.rb', line 55

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



60
61
62
# File 'lib/typhoeus/easy.rb', line 60

def auth_methods
  get_info_long(INFO_VALUES[:CURLINFO_HTTPAUTH_AVAIL])
end

#curl_versionObject



249
250
251
# File 'lib/typhoeus/easy.rb', line 249

def curl_version
  version
end

#disable_ssl_peer_verificationObject



118
119
120
# File 'lib/typhoeus/easy.rb', line 118

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

#failureObject

gets called when finished and response code is 300-599



200
201
202
# File 'lib/typhoeus/easy.rb', line 200

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

#follow_location=(boolean) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/typhoeus/easy.rb', line 76

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



245
246
247
# File 'lib/typhoeus/easy.rb', line 245

def get_info_double(option)
  easy_getinfo_double(option)
end

#get_info_long(option) ⇒ Object



241
242
243
# File 'lib/typhoeus/easy.rb', line 241

def get_info_long(option)
  easy_getinfo_long(option)
end

#get_info_string(option) ⇒ Object



237
238
239
# File 'lib/typhoeus/easy.rb', line 237

def get_info_string(option)
  easy_getinfo_string(option)
end

#increment_retriesObject



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

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

#max_redirects=(redirects) ⇒ Object



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

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

#max_retriesObject



221
222
223
# File 'lib/typhoeus/easy.rb', line 221

def max_retries
  @max_retries ||= 40
end

#max_retries?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/typhoeus/easy.rb', line 225

def max_retries?
  retries >= max_retries
end

#on_failure(&block) ⇒ Object



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

def on_failure(&block)
  @failure = block
end

#on_failure=(block) ⇒ Object



208
209
210
# File 'lib/typhoeus/easy.rb', line 208

def on_failure=(block)
  @failure = block
end

#on_success(&block) ⇒ Object



191
192
193
# File 'lib/typhoeus/easy.rb', line 191

def on_success(&block)
  @success = block
end

#on_success=(block) ⇒ Object



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

def on_success=(block)
  @success = block
end

#params=(params) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/typhoeus/easy.rb', line 145

def params=(params)
  params_string = params.keys.collect do |k|
    value = params[k]
    if value.is_a? Hash
      value.keys.collect {|sk| Rack::Utils.escape("#{k}[#{sk}]") + "=" + Rack::Utils.escape(value[sk].to_s)}
    elsif value.is_a? Array
      key = Rack::Utils.escape(k.to_s)
      value.collect { |v| "#{key}=#{Rack::Utils.escape(v.to_s)}" }.join('&')
    else
      "#{Rack::Utils.escape(k.to_s)}=#{Rack::Utils.escape(params[k].to_s)}"
    end
  end.flatten.join("&")

  if method == :post
    self.post_data = params_string
  else
    self.url = "#{url}?#{params_string}"
  end
end

#performObject



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

def perform
  set_headers()
  easy_perform()
  response_code()
end

#post_data=(data) ⇒ Object



139
140
141
142
143
# File 'lib/typhoeus/easy.rb', line 139

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

#proxy=(proxy) ⇒ Object



51
52
53
# File 'lib/typhoeus/easy.rb', line 51

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

#request_body=(request_body) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/typhoeus/easy.rb', line 98

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



229
230
231
232
233
234
235
# File 'lib/typhoeus/easy.rb', line 229

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

#response_codeObject



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

def response_code
  get_info_long(INFO_VALUES[:CURLINFO_RESPONSE_CODE])
end

#retriesObject



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

def retries
  @retries ||= 0
end

#set_headersObject



179
180
181
182
183
184
# File 'lib/typhoeus/easy.rb', line 179

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



165
166
167
168
169
170
171
# File 'lib/typhoeus/easy.rb', line 165

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

#successObject

gets called when finished and response code is 200-299



187
188
189
# File 'lib/typhoeus/easy.rb', line 187

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

#timed_out?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/typhoeus/easy.rb', line 94

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

#timeout=(milliseconds) ⇒ Object



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

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

#total_time_takenObject



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

def total_time_taken
  get_info_double(INFO_VALUES[:CURLINFO_TOTAL_TIME])
end

#user_agent=(user_agent) ⇒ Object



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

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

#verbose=(boolean) ⇒ Object



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

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