Skip to content

Instantly share code, notes, and snippets.

Created Jun 21, 2012
What would you like to do?
hydra =
hydra.cache_setter do |request|
@cache.set(request.cache_key, request.response, request.cache_timeout)
hydra.cache_getter do |request|
@cache.get(request.cache_key) rescue nil
hydra =
response = => 200, :headers => "", :body => "{'name' : 'paul'}", :time => 0.3)
hydra.stub(:get, "http://localhost:3000/users/1").and_return(response)
request ="http://localhost:3000/users/1")
request.on_complete do |response|
hydra.queue request
hydra.stub(:get, /http\:\/\/localhost\:3000\/users\/.*/).and_return(response)
# any requests for a user will be stubbed out with the pre built response.
response ="http://localhost:3000/posts",
:body => {
:title => "test post", :content => "this is my test",
:file =>"thesis.txt","r")
request.on_complete do |response|
if response.success?
# hell yeah
elsif response.timed_out?
# aw hell no
log("got a time out")
elsif response.code == 0
# Could not get an http response, something's wrong.
# Received a non-successful http response.
log("HTTP request failed: " + response.code.to_s)
# Generally, you should be running requests through hydra. Here is how that looks
hydra =
first_request ="http://localhost:3000/posts/1.json")
first_request.on_complete do |response|
post = JSON.parse(response.body)
third_request = # get the first url in the post
third_request.on_complete do |response|
# do something with that
hydra.queue third_request
return post
second_request ="http://localhost:3000/users/1.json")
second_request.on_complete do |response|
hydra.queue first_request
hydra.queue second_request # this is a blocking call that returns once all requests are complete
first_request.handled_response # the value returned from the on_complete block
second_request.handled_response # the value returned from the on_complete block (parsed JSON)
hydra =
2.times do
r ="http://localhost/3000/users/1")
hydra.queue r
end # this will result in a single request being issued. However, the on_complete handlers of both will be called.
2.times do
r ="http://localhost/3000/users/1")
hydra.queue r
end # this will result in a two requests.
response = Typhoeus::Request.get("")
response = Typhoeus::Request.head("")
response = Typhoeus::Request.put("http://localhost:3000/posts/1", :body => "whoo, a body")
response ="http://localhost:3000/posts", :body => {:title => "test post", :content => "this is my test"})
response = Typhoeus::Request.delete("http://localhost:3000/posts/1")
hydra = => 20) # keep from killing some servers
require 'rubygems'
require 'typhoeus'
require 'json'
# the request object
request ="",
:body => "this is a request body",
:method => :post,
:headers => {:Accept => "text/html"},
:timeout => 100, # milliseconds
:cache_timeout => 60, # seconds
:params => {:field1 => "a field"})
# we can see from this that the first argument is the url. the second is a set of options.
# the options are all optional. The default for :method is :get. Timeout is measured in milliseconds.
# cache_timeout is measured in seconds.
# Run the request via Hydra.
hydra =
# the response object will be set after the request is run
response = request.response
response.code # http status code
response.time # time in seconds the request took
response.headers # the http headers
response.headers_hash # http headers put into a hash
response.body # the response body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment