Class: ZipTricks::BlockWrite

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

Overview

Stashes a block given by the Rack webserver when calling each() on a body, and calls that block every time it is written to using :<< (shovel). Poses as an IO for rubyzip.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ BlockWrite

The block is the block given to each() of the Rack body, or other block you want to receive the string chunks written by the zip compressor.



6
7
8
# File 'lib/zip_tricks/block_write.rb', line 6

def initialize(&block)
  @block = block
end

Instance Method Details

#<<(buf) ⇒ Object

Every time this object gets written to, call the Rack body each() block with the bytes given instead.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zip_tricks/block_write.rb', line 18

def <<(buf)
  return if buf.nil?

  # Ensure we ALWAYS write in binary encoding.
  encoded = if buf.encoding != Encoding::BINARY
    # If we got a frozen string we can't force_encoding on it
    buf.force_encoding(Encoding::BINARY) rescue buf.dup.force_encoding(Encoding::BINARY)
  else
    buf
  end

  #  buf.dup.force_encoding(Encoding::BINARY)
  return if encoded.bytesize.zero? # Zero-size output has a special meaning when using chunked encoding

  @block.call(encoded)
  self
end

#closeObject

Does nothing



37
38
39
# File 'lib/zip_tricks/block_write.rb', line 37

def close
  nil
end