public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.