Class: Roda::RodaPlugins::Streaming::Stream

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/roda/plugins/streaming.rb

Overview

Class of the response body in case you use #stream.

Instance Method Summary collapse

Constructor Details

#initialize(opts = OPTS, &block) ⇒ Stream

Handle streaming options, see Streaming for details.



49
50
51
52
53
54
# File 'lib/roda/plugins/streaming.rb', line 49

def initialize(opts=OPTS, &block)
  @block = block
  @out = nil
  @callback = opts[:callback]
  @closed = false
end

Instance Method Details

#closeObject

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



65
66
67
68
69
# File 'lib/roda/plugins/streaming.rb', line 65

def close
  return if closed?
  @closed = true
  @callback.call if @callback
end

#closed?Boolean

Whether the connection has already been closed.

Returns:

  • (Boolean)


72
73
74
# File 'lib/roda/plugins/streaming.rb', line 72

def closed?
  @closed
end

#each(&out) ⇒ Object

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



77
78
79
80
81
82
# File 'lib/roda/plugins/streaming.rb', line 77

def each(&out)
  @out = out
  @block.call(self)
ensure
  close
end

#write(data) ⇒ Object Also known as: <<

Add output to the streaming response body.



57
58
59
60
# File 'lib/roda/plugins/streaming.rb', line 57

def write(data)
  @out.call(data.to_s)
  self
end