Class: Grack::IOStreamer

Inherits:
Object
  • Object
show all
Defined in:
lib/grack/io_streamer.rb

Overview

A Rack body implementation that streams a given IO object in chunks for a Rack response.

Direct Known Subclasses

FileStreamer

Constant Summary collapse

READ_SIZE =

The number of bytes to read at a time from IO streams.

32768

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, mtime) ⇒ IOStreamer

Creates a new instance of this object.

Parameters:

  • io (#read)

    a readable, IO-like object.

  • mtime (Time)

    a timestamp to use for the last modified header in the response.



16
17
18
19
# File 'lib/grack/io_streamer.rb', line 16

def initialize(io, mtime)
  @io = io
  @mtime = mtime
end

Instance Attribute Details

#mtimeObject (readonly)

The last modified time to report for the Rack response.



23
24
25
# File 'lib/grack/io_streamer.rb', line 23

def mtime
  @mtime
end

Instance Method Details

#each {|chunk| ... } ⇒ Object

Iterates over the wrapped IO object in chunks, yielding each one.

Yield Parameters:

  • chunk (String)

    a chunk read from the wrapped IO object.



29
30
31
32
33
34
35
# File 'lib/grack/io_streamer.rb', line 29

def each
  with_io do |io|
    while chunk = io.read(READ_SIZE) do
      yield(chunk)
    end
  end
end

#with_io {|io| ... } ⇒ Object (private)

Yield Parameters:

  • io (#read)

    the wrapped IO object.



41
42
43
# File 'lib/grack/io_streamer.rb', line 41

def with_io
  yield(@io)
end