Skip to content

Instantly share code, notes, and snippets.

@ncr
Created April 22, 2010 21:28
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 ncr/375843 to your computer and use it in GitHub Desktop.
Save ncr/375843 to your computer and use it in GitHub Desktop.
# Net::HTTPResponse streaming for use in Rack apps with Fibers!
def read_body # Net::HTTPResponse#read_body
yield "foo"; yield "bar"; yield "baz"
end
class R # A Rack Streaming response
def initialize
@f = Fiber.new do
read_body do |chunk| # Net::HTTPResponse#read_body
sleep(0.5)
Fiber.yield(chunk)
end
end
end
def each
while x = @f.resume
yield x
end
end
end
R.new.each do |chunk|
puts chunk # Streaming!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment