Created
August 27, 2009 19:34
-
-
Save pauldix/176497 to your computer and use it in GitHub Desktop.
This file contains 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
class User | |
def initialize(attrs = {}) | |
# do stuff here | |
end | |
def self.find_request(id) | |
request = Typhoeus::Request.new(:method => :get, | |
:host => "localhost:3000", | |
:path => "/users/#{id}") | |
request.on_complete do |response| | |
new JSON.parse(response.body) | |
end | |
end | |
end | |
# Comment model looks pretty much the same | |
hydra = Typhoeus::Hydra.new(20) # number is the size of the easy object pool | |
user_request = User.find_request(1) # where 1 is the id of the user | |
user_request.after_complete do |user| | |
comment_requests = user.comment_ids.map {|id| Comment.find_request(id)} | |
comment_requests.each do |comment_request| | |
comment_request.after_complete do |comment| | |
user.comments << comment | |
end | |
hydra.queue << comment_request | |
end | |
end | |
hydra.queue << user_request | |
hydra.run | |
user_request.handled_response # this is the parsed user object with the comments set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment