Class: Cramp::Body

Inherits:
Object
  • Object
show all
Includes:
EventMachine::Deferrable
Defined in:
lib/cramp/body.rb

Instance Method Summary collapse

Constructor Details

#initializeBody

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

Returns:

  • (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

#flushObject



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_dequeueObject



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