Skip to content

Instantly share code, notes, and snippets.

@danwrong
Created June 9, 2012 00:55
Show Gist options
  • Save danwrong/2898919 to your computer and use it in GitHub Desktop.
Save danwrong/2898919 to your computer and use it in GitHub Desktop.
# Usage:
def streaming
chunked_response do |resp|
resp.chunk do
"before" * 5000
end
resp.chunk do
sleep(1)
"\nsleeping...\n"
end
resp.chunk do
"after" * 1000
end
end
end
# Code:
class ChunkedResponse::Body
def initialize
@chunks = []
end
def chunk(&block)
@chunks << block
end
def each
@chunks.each do |chunk|
stuff = chunk.call
size = stuff.bytesize
if size > 0
yield [size.to_s(16), TERM, stuff, TERM].join
end
end
yield ["0", TERM, "", TERM].join
end
def size
0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment