Class: Zipstream::Body

Inherits:
Object
  • Object
show all
Defined in:
lib/zipstream/body.rb

Overview

Stream a zipfile as a rack response body

We use Fibers to deep-yield data being written to the zip stream directly to Rack

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Body

Returns a new instance of Body.



6
7
8
9
10
11
12
13
# File 'lib/zipstream/body.rb', line 6

def initialize &block
  @stream = Zipstream::FiberYieldingStream.new
  @fiber = Zipstream::Fiber.new do
    Zipstream.new(@stream).tap(&block).close
    # Make sure this returns nil as a sentinel
    nil
  end
end

Instance Method Details

#eachObject



15
16
17
18
19
20
# File 'lib/zipstream/body.rb', line 15

def each
  # Yield fiber yielded data until we hit our nil sentinel
  until (yielded = @fiber.resume).nil?
    yield yielded
  end
end