daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Oct 19th, 2018 70 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top