Skip to content

Instantly share code, notes, and snippets.

@pauldix
Created August 27, 2009 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pauldix/176497 to your computer and use it in GitHub Desktop.
Save pauldix/176497 to your computer and use it in GitHub Desktop.
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