Created
June 21, 2012 12:03
-
-
Save hanshasselberg/2965397 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hydra = Typhoeus::Hydra.new | |
hydra.cache_setter do |request| | |
@cache.set(request.cache_key, request.response, request.cache_timeout) | |
end | |
hydra.cache_getter do |request| | |
@cache.get(request.cache_key) rescue nil | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hydra = Typhoeus::Hydra.new | |
response = Response.new(:code => 200, :headers => "", :body => "{'name' : 'paul'}", :time => 0.3) | |
hydra.stub(:get, "http://localhost:3000/users/1").and_return(response) | |
request = Typhoeus::Request.new("http://localhost:3000/users/1") | |
request.on_complete do |response| | |
JSON.parse(response.body) | |
end | |
hydra.queue request | |
hydra.run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hydra.stub(:get, /http\:\/\/localhost\:3000\/users\/.*/).and_return(response) | |
# any requests for a user will be stubbed out with the pre built response. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
response = Typhoeus::Request.post("http://localhost:3000/posts", | |
:body => { | |
:title => "test post", :content => "this is my test", | |
:file => File.open("thesis.txt","r") | |
} | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
log(response.curl_error_message) | |
else | |
# Received a non-successful http response. | |
log("HTTP request failed: " + response.code.to_s) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generally, you should be running requests through hydra. Here is how that looks | |
hydra = Typhoeus::Hydra.new | |
first_request = Typhoeus::Request.new("http://localhost:3000/posts/1.json") | |
first_request.on_complete do |response| | |
post = JSON.parse(response.body) | |
third_request = Typhoeus::Request.new(post.links.first) # get the first url in the post | |
third_request.on_complete do |response| | |
# do something with that | |
end | |
hydra.queue third_request | |
return post | |
end | |
second_request = Typhoeus::Request.new("http://localhost:3000/users/1.json") | |
second_request.on_complete do |response| | |
JSON.parse(response.body) | |
end | |
hydra.queue first_request | |
hydra.queue second_request | |
hydra.run # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hydra = Typhoeus::Hydra.new | |
2.times do | |
r = Typhoeus::Request.new("http://localhost/3000/users/1") | |
hydra.queue r | |
end | |
hydra.run # this will result in a single request being issued. However, the on_complete handlers of both will be called. | |
hydra.disable_memoization | |
2.times do | |
r = Typhoeus::Request.new("http://localhost/3000/users/1") | |
hydra.queue r | |
end | |
hydra.run # this will result in a two requests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
response = Typhoeus::Request.get("http://www.pauldix.net") | |
response = Typhoeus::Request.head("http://www.pauldix.net") | |
response = Typhoeus::Request.put("http://localhost:3000/posts/1", :body => "whoo, a body") | |
response = Typhoeus::Request.post("http://localhost:3000/posts", :body => {:title => "test post", :content => "this is my test"}) | |
response = Typhoeus::Request.delete("http://localhost:3000/posts/1") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hydra = Typhoeus::Hydra.new(:max_concurrency => 20) # keep from killing some servers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'typhoeus' | |
require 'json' | |
# the request object | |
request = Typhoeus::Request.new("http://www.pauldix.net", | |
: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 = Typhoeus::Hydra.new | |
hydra.queue(request) | |
hydra.run | |
# 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