Module: Roda::RodaPlugins::Streaming::InstanceMethods

Defined in:
lib/roda/plugins/streaming.rb

Instance Method Summary collapse

Instance Method Details

#handle_stream_error(e, out) ⇒ Object

Handle exceptions raised while streaming when using :loop



162
163
164
# File 'lib/roda/plugins/streaming.rb', line 162

def handle_stream_error(e, out)
  raise e
end

#stream(opts = OPTS, &block) ⇒ Object

Immediately return a streaming response using the current response status and headers, calling the block to get the streaming response. See Streaming for details.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/roda/plugins/streaming.rb', line 143

def stream(opts=OPTS, &block)
  if opts[:loop]
    block = proc do |out|
      until out.closed?
        begin
          yield(out)
        rescue => e
          handle_stream_error(e, out)
        end
      end
    end
  end

  stream_class = (opts[:async] && RUBY_VERSION >= '2.3') ? AsyncStream : Stream

  throw :halt, @_response.finish_with_body(stream_class.new(opts, &block))
end