Class: ZipTricks::RackBody

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_tricks/rack_body.rb

Overview

Can be used as a Rack response body directly. Will yield a Streamer for adding entries to the archive and writing zip entry bodies.

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ RackBody

Prepares a new Rack response body with a Zip output stream. The block given to the constructor will be called when the response body will be read by the webserver, and will receive a Streamer as it's block argument. You can then add entries to the Streamer as usual. The archive will be automatically closed at the end of the block.

# Precompute the Content-Length ahead of time
content_length = ZipTricks::SizeEstimator.estimate do | estimator |
  estimator.add_stored_entry(filename: 'large.tif', size: 1289894)
end

# Prepare the response body. The block will only be called when the response starts to be written.
body = ZipTricks::RackBody.new do | streamer |
  streamer.add_stored_entry(filename: 'large.tif', size: 1289894, crc32: 198210)
  streamer << large_file.read(1024*1024) until large_file.eof?
  ...
end

return [200, {'Content-Type' => 'binary/octet-stream', 'Content-Length' => content_length.to_s}, body]


24
25
26
# File 'lib/zip_tricks/rack_body.rb', line 24

def initialize(&blk)
  @archiving_block = blk
end

Instance Method Details

#closeObject

Does nothing because nothing has to be deallocated or canceled even if the zip output is incomplete. The archive gets closed automatically as part of Streamer.open



39
40
# File 'lib/zip_tricks/rack_body.rb', line 39

def close
end

#each(&body_chunk_block) ⇒ Object

Connects a BlockWrite to the Rack webserver output, and calls the proc given to the constructor with a Streamer for archive writing.



31
32
33
34
# File 'lib/zip_tricks/rack_body.rb', line 31

def each(&body_chunk_block)
  fake_io = ZipTricks::BlockWrite.new(&body_chunk_block)
  ZipTricks::Streamer.open(fake_io, &@archiving_block)
end