class Roda::RodaPlugins::Streaming::Stream

  1. lib/roda/plugins/streaming.rb
Superclass: Object

Class of the response body in case you use stream.

Methods

Public Class

  1. new

Public Instance

  1. <<
  2. close
  3. closed?
  4. each
  5. write

Included modules

  1. Enumerable

Public Class methods

new(opts=OPTS, &block)

Handle streaming options, see Streaming for details.

[show source]
   # File lib/roda/plugins/streaming.rb
51 def initialize(opts=OPTS, &block)
52   @block = block
53   @out = nil
54   @callback = opts[:callback]
55   @closed = false
56 end

Public Instance methods

<<(data)

Add output to the streaming response body. Returns self.

[show source]
   # File lib/roda/plugins/streaming.rb
66 def <<(data)
67   write(data)
68   self
69 end
close()

If not already closed, close the connection, and call any callbacks.

[show source]
   # File lib/roda/plugins/streaming.rb
73 def close
74   return if closed?
75   @closed = true
76   @callback.call if @callback
77 end
closed?()

Whether the connection has already been closed.

[show source]
   # File lib/roda/plugins/streaming.rb
80 def closed?
81   @closed
82 end
each(&out)

Yield values to the block as they are passed in via <<.

[show source]
   # File lib/roda/plugins/streaming.rb
85 def each(&out)
86   @out = out
87   @block.call(self)
88 ensure
89   close
90 end
write(data)

Add output to the streaming response body. Returns number of bytes written.

[show source]
   # File lib/roda/plugins/streaming.rb
59 def write(data)
60   data = data.to_s
61   @out.call(data)
62   data.bytesize
63 end