require 'typhoeus'
###
# Making Quick Requests
response = Typhoeus::Request.get("http://www.google.com/search?q=paul%20dix")
response = Typhoeus::Request.head("http://www.google.com/")
response = Typhoeus::Request.put("http://localhost:3000/posts/1",
:body => "whoo, a body")
response = Typhoeus::Request.post("http://localhost:3000/posts",
:params => {
:title => "test post", :content => "this is my test"
} )
response = Typhoeus::Request.delete("http://localhost:3000/posts/1")
###
# More Request Options
response = Typhoeus::Request.get("http://localhost:3000/",
:headers => {
'User-Agent' => "Some User Agent", # UA
},
:connect_timeout => 1000, # milliseconds
:timeout => 1000, # milliseconds
:interface => "en0", # Ethernet interface on my MBP
:follow_location => true, # Following redirects
:max_redirects => 5, # If prev, # of redirects
:proxy => "127.0.0.1:8080", # Proxy IP:Port
:proxy_username => "user", # Proxy Username
:proxy_password => "password" ) # Proxy Password
###
# Handling Responses
response = Typhoeus::Request.get("http://httpstat.us/200")
puts "This was a total success!" if response.success? && response.code == 200
response = Typhoeus::Request.get("http://httpstat.us/404")
puts "This was not a success!" unless response.success? &&
response.status_message == "Not Found"
response = Typhoeus::Request.get("http://httpstat.us/408", :timeout => -1)
puts "This request timed out!" if response.timed_out? &&
response.curl_return_code == 28
response = Typhoeus::Request.get("http://www.google.com/")
puts response.body if response.success?