Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
Add Comment
Please, Sign In to add comment