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
}
INFO_VALUES =
{
  :CURLINFO_RESPONSE_CODE => 2097154,
  :CURLINFO_TOTAL_TIME    => 3145731
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEasy

Returns a new instance of Easy.



26
27
28
29
30
# File 'lib/typhoeus/easy.rb', line 26

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

#curl_versionObject



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

def curl_version
  version
end

#failureObject

gets called when finished and response code is 300-599



158
159
160
# File 'lib/typhoeus/easy.rb', line 158

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

#follow_location=(boolean) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/typhoeus/easy.rb', line 44

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



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

def get_info_double(option)
  easy_getinfo_double(option)
end

#get_info_long(option) ⇒ Object



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

def get_info_long(option)
  easy_getinfo_long(option)
end

#get_info_string(option) ⇒ Object



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

def get_info_string(option)
  easy_getinfo_string(option)
end

#increment_retriesObject



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

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

#max_retriesObject



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

def max_retries
  @max_retries ||= 40
end

#max_retries?Boolean

Returns:

  • (Boolean)


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

def max_retries?
  retries >= max_retries
end

#on_failure(&block) ⇒ Object



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

def on_failure(&block)
  @failure = block
end

#on_failure=(block) ⇒ Object



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

def on_failure=(block)
  @failure = block
end

#on_success(&block) ⇒ Object



149
150
151
# File 'lib/typhoeus/easy.rb', line 149

def on_success(&block)
  @success = block
end

#on_success=(block) ⇒ Object



153
154
155
# File 'lib/typhoeus/easy.rb', line 153

def on_success=(block)
  @success = block
end

#params=(params) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/typhoeus/easy.rb', line 103

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



131
132
133
134
135
# File 'lib/typhoeus/easy.rb', line 131

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

#post_data=(data) ⇒ Object



97
98
99
100
101
# File 'lib/typhoeus/easy.rb', line 97

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



62
63
64
65
66
67
68
69
70
71
# File 'lib/typhoeus/easy.rb', line 62

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



187
188
189
190
191
192
193
# File 'lib/typhoeus/easy.rb', line 187

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

#response_codeObject



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

def response_code
  get_info_long(INFO_VALUES[:CURLINFO_RESPONSE_CODE])
end

#retriesObject



170
171
172
# File 'lib/typhoeus/easy.rb', line 170

def retries
  @retries ||= 0
end

#set_headersObject



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

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



123
124
125
126
127
128
129
# File 'lib/typhoeus/easy.rb', line 123

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



145
146
147
# File 'lib/typhoeus/easy.rb', line 145

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

#timed_out?Boolean

Returns:

  • (Boolean)


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

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

#timeout=(milliseconds) ⇒ Object



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

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

#total_time_takenObject



36
37
38
# File 'lib/typhoeus/easy.rb', line 36

def total_time_taken
  get_info_double(INFO_VALUES[:CURLINFO_TOTAL_TIME])
end

#user_agent=(user_agent) ⇒ Object



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

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