Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 18th, 2012  |  syntax: None  |  size: 0.71 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def http_get( url )
  2.   # get the fiber our browser request is being processed in
  3.   calling_fiber = Fiber.current
  4.  
  5.   # make an async request
  6.   # EventMachine queues the http.callback block and executes it at some point after the http request returns
  7.   http = EventMachine::HttpRequest.new( url ).get
  8.   http.callback { calling_fiber.resume( http ) }
  9.  
  10.   # the calling_fiber yields control back to the EM reactor, which can continue processing other browser requests etc
  11.   # When the async http request returns, the callback block is queued for EM to execute when it received control again.
  12.   # The callback resumes the calling fiber, and the get method returns the result of the http get request.
  13.   return calling_fiber.yield
  14. end