Module: Roda::RodaPlugins::Chunked::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#chunked(template, opts = OPTS, &block) ⇒ Object

Render a response to the user in chunks. See Chunked for an overview.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/roda/plugins/chunked.rb', line 197

def chunked(template, opts=OPTS, &block)
  unless defined?(@_chunked)
    @_chunked = env[HTTP_VERSION] == HTTP11
  end

  unless @_chunked
    # If chunking is disabled, do a normal rendering of the view.
    yield if block
    return view(template, opts)
  end

  if template.is_a?(Hash)
    if opts.empty?
      opts = template
    else
      opts = opts.merge(template)
    end
  end
  
  # Hack so that the arguments don't need to be passed
  # through the response and body objects.
  @_each_chunk_args = [template, opts, block]

  res = response
  headers = res.headers
  if chunk_headers = self.opts[:chunk_headers]
    headers.merge!(chunk_headers)
  end
  headers[TRANSFER_ENCODING] = CHUNKED

  throw :halt, res.finish_with_body(Body.new(self))
end

#each_chunkObject

Yield each chunk of the template rendering separately.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/roda/plugins/chunked.rb', line 231

def each_chunk
  response.body.each{|s| yield s}

  template, opts, block = @_each_chunk_args

  # Use a lambda for the flusher, so that a call to flush
  # by a template can result in this method yielding a chunk
  # of the response.
  @_flusher = lambda do
    yield @_out_buf
    @_out_buf = ''
  end

  if layout =  opts.fetch(:layout, render_opts[:layout])
    if layout_opts = opts[:layout_opts]
      layout_opts = render_opts[:layout_opts].merge(layout_opts)
    end

    @_out_buf = render(layout, layout_opts||OPTS) do
      flush
      block.call if block
      yield opts[:content] || render(template, opts)
      nil
    end
  else
    yield if block
    yield view(template, opts)
  end

  flush
rescue => e
  handle_chunk_error(e)
end

#flushObject

Call the flusher if one is defined. If one is not defined, this is a no-op, so flush can be used inside views without breaking things if chunking is not used.



273
274
275
# File 'lib/roda/plugins/chunked.rb', line 273

def flush
  @_flusher.call if @_flusher
end

#handle_chunk_error(e) ⇒ Object

By default, raise the exception.



266
267
268
# File 'lib/roda/plugins/chunked.rb', line 266

def handle_chunk_error(e)
  raise e
end

#no_chunk!Object

Disable chunking for the current request. Mostly useful when chunking is turned on by default.



181
182
183
# File 'lib/roda/plugins/chunked.rb', line 181

def no_chunk!
  @_chunked = false
end

#view(*a) ⇒ Object

If chunking by default, call chunked if it hasn’t yet been called and chunking is not specifically disabled.



187
188
189
190
191
192
193
# File 'lib/roda/plugins/chunked.rb', line 187

def view(*a)
  if opts[:chunk_by_default] && !defined?(@_chunked)
    chunked(*a)
  else
    super
  end
end