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



38
39
40
41
42
# File 'lib/typhoeus/easy.rb', line 38

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



48
49
50
51
# File 'lib/typhoeus/easy.rb', line 48

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



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

def auth_methods
  get_info_long(INFO_VALUES[:CURLINFO_HTTPAUTH_AVAIL])
end

#curl_versionObject



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

def curl_version
  version
end

#failureObject

gets called when finished and response code is 300-599



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

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

#follow_location=(boolean) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/typhoeus/easy.rb', line 69

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



232
233
234
# File 'lib/typhoeus/easy.rb', line 232

def get_info_double(option)
  easy_getinfo_double(option)
end

#get_info_long(option) ⇒ Object



228
229
230
# File 'lib/typhoeus/easy.rb', line 228

def get_info_long(option)
  easy_getinfo_long(option)
end

#get_info_string(option) ⇒ Object



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

def get_info_string(option)
  easy_getinfo_string(option)
end

#increment_retriesObject



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

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

#max_redirects=(redirects) ⇒ Object



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

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

#max_retriesObject



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

def max_retries
  @max_retries ||= 40
end

#max_retries?Boolean

Returns:

  • (Boolean)


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

def max_retries?
  retries >= max_retries
end

#on_failure(&block) ⇒ Object



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

def on_failure(&block)
  @failure = block
end

#on_failure=(block) ⇒ Object



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

def on_failure=(block)
  @failure = block
end

#on_success(&block) ⇒ Object



178
179
180
# File 'lib/typhoeus/easy.rb', line 178

def on_success(&block)
  @success = block
end

#on_success=(block) ⇒ Object



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

def on_success=(block)
  @success = block
end

#params=(params) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/typhoeus/easy.rb', line 132

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



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

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

#post_data=(data) ⇒ Object



126
127
128
129
130
# File 'lib/typhoeus/easy.rb', line 126

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

#request_body=(request_body) ⇒ Object



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

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



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

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

#response_codeObject



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

def response_code
  get_info_long(INFO_VALUES[:CURLINFO_RESPONSE_CODE])
end

#retriesObject



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

def retries
  @retries ||= 0
end

#set_headersObject



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

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



152
153
154
155
156
157
158
# File 'lib/typhoeus/easy.rb', line 152

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



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

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

#timed_out?Boolean

Returns:

  • (Boolean)


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

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

#timeout=(milliseconds) ⇒ Object



81
82
83
84
85
# File 'lib/typhoeus/easy.rb', line 81

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

#total_time_takenObject



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

def total_time_taken
  get_info_double(INFO_VALUES[:CURLINFO_TOTAL_TIME])
end

#user_agent=(user_agent) ⇒ Object



102
103
104
# File 'lib/typhoeus/easy.rb', line 102

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

#verbose=(boolean) ⇒ Object



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

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