Class: Cramp::Body
- Inherits:
-
Object
- Object
- Cramp::Body
- Includes:
- EventMachine::Deferrable
- Defined in:
- lib/cramp/body.rb
Instance Method Summary collapse
- #call(body) ⇒ Object
- #closed? ⇒ Boolean
- #each(&blk) ⇒ Object
- #flush ⇒ Object
-
#initialize ⇒ Body
constructor
A new instance of Body.
- #schedule_dequeue ⇒ Object
Constructor Details
#initialize ⇒ Body
Returns a new instance of Body.
7 8 9 10 11 12 |
# File 'lib/cramp/body.rb', line 7 def initialize @queue = [] # Make sure to flush out the queue before closing the connection callback { flush } end |
Instance Method Details
#call(body) ⇒ Object
14 15 16 17 |
# File 'lib/cramp/body.rb', line 14 def call(body) @queue << body schedule_dequeue end |
#closed? ⇒ Boolean
24 25 26 |
# File 'lib/cramp/body.rb', line 24 def closed? @deferred_status != :unknown end |
#each(&blk) ⇒ Object
19 20 21 22 |
# File 'lib/cramp/body.rb', line 19 def each &blk @body_callback = blk schedule_dequeue end |
#flush ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/cramp/body.rb', line 28 def flush return unless @body_callback until @queue.empty? Array(@queue.shift).each {|chunk| @body_callback.call(chunk) } end end |
#schedule_dequeue ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cramp/body.rb', line 36 def schedule_dequeue return unless @body_callback EventMachine.next_tick do next unless body = @queue.shift Array(body).each {|chunk| @body_callback.call(chunk) } schedule_dequeue unless @queue.empty? end end |