class Agouti::Rack::PackageLimiter::GzipTruncatedStream

Public: class responsible for truncating the gzip stream to a given number of bytes.

Public Class Methods

new(body, mtime, byte_limit) click to toggle source

Public: Constructor.

body - response body. mtime - last-modified time. byte_limit - byte limit.

Returns an instance of Agouti::Rack::PackageLimiter::GzipTruncatedStream.

Calls superclass method
# File lib/agouti/rack/package_limiter.rb, line 113
def initialize body, mtime, byte_limit
  super body, mtime
  @byte_limit = byte_limit
  @total_sent_bytes = 0
end

Public Instance Methods

write(data) click to toggle source

Public: Writes data to stream.

data - data.

If total sent bytes reaches bytes limit, data is sliced.

# File lib/agouti/rack/package_limiter.rb, line 124
def write(data)
  if @total_sent_bytes + data.bytesize > @byte_limit
    data = data.byteslice(0, @byte_limit - @total_sent_bytes)
  end

  @total_sent_bytes += data.bytesize
  @writer.call(data)
end