Guest User

Untitled

a guest
Oct 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. # Usage:
  2.  
  3. def streaming
  4. chunked_response do |resp|
  5. resp.chunk do
  6. "before" * 5000
  7. end
  8.  
  9. resp.chunk do
  10. sleep(1)
  11. "\nsleeping...\n"
  12. end
  13.  
  14. resp.chunk do
  15. "after" * 1000
  16. end
  17. end
  18. end
  19.  
  20. # Code:
  21.  
  22. class ChunkedResponse::Body
  23.  
  24. def initialize
  25. @chunks = []
  26. end
  27.  
  28. def chunk(&block)
  29. @chunks << block
  30. end
  31.  
  32. def each
  33. @chunks.each do |chunk|
  34. stuff = chunk.call
  35. size = stuff.bytesize
  36.  
  37. if size > 0
  38. yield [size.to_s(16), TERM, stuff, TERM].join
  39. end
  40. end
  41.  
  42. yield ["0", TERM, "", TERM].join
  43. end
  44.  
  45. def size
  46. 0
  47. end
  48. end
Add Comment
Please, Sign In to add comment