Public Instance methods
chunked(template, opts=OPTS, &block)
Render a response to the user in chunks. See Chunked for an overview. If a block is given, it is passed to delay.
[show source]
# File lib/roda/plugins/chunked.rb 229 def chunked(template, opts=OPTS, &block) 230 if @_chunked.nil? 231 @_chunked = !self.opts[:force_chunked_encoding] || @_request.http_version == "HTTP/1.1" 232 end 233 234 if block 235 delay(&block) 236 end 237 238 unless @_chunked 239 # If chunking is disabled, do a normal rendering of the view. 240 run_delayed_blocks 241 return view(template, opts) 242 end 243 244 if template.is_a?(Hash) 245 if opts.empty? 246 opts = template 247 else 248 opts = Hash[opts].merge!(template) 249 end 250 end 251 252 # Hack so that the arguments don't need to be passed 253 # through the response and body objects. 254 @_each_chunk_args = [template, opts] 255 256 res = response 257 headers = res.headers 258 if chunk_headers = self.opts[:chunk_headers] 259 headers.merge!(chunk_headers) 260 end 261 if self.opts[:force_chunked_encoding] 262 res[RodaResponseHeaders::TRANSFER_ENCODING] = 'chunked' 263 body = Body.new(self) 264 else 265 body = StreamBody.new(self) 266 end 267 268 throw :halt, res.finish_with_body(body) 269 end
delay(&block)
Delay the execution of the block until right before the content template is to be rendered.
[show source]
# File lib/roda/plugins/chunked.rb 273 def delay(&block) 274 raise RodaError, "must pass a block to Roda#delay" unless block 275 (@_delays ||= []) << block 276 end
each_chunk()
Yield each chunk of the template rendering separately.
[show source]
# File lib/roda/plugins/chunked.rb 279 def each_chunk 280 response.body.each{|s| yield s} 281 282 template, opts = @_each_chunk_args 283 284 # Use a lambda for the flusher, so that a call to flush 285 # by a template can result in this method yielding a chunk 286 # of the response. 287 @_flusher = lambda do 288 yield @_out_buf 289 @_out_buf = String.new 290 end 291 292 if layout_opts = view_layout_opts(opts) 293 @_out_buf = render_template(layout_opts) do 294 flush 295 run_delayed_blocks 296 yield opts[:content] || render_template(template, opts) 297 nil 298 end 299 else 300 run_delayed_blocks 301 yield view(template, opts) 302 end 303 304 flush 305 rescue => e 306 handle_chunk_error(e) 307 end
flush()
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.
[show source]
# File lib/roda/plugins/chunked.rb 317 def flush 318 @_flusher.call if @_flusher 319 end
handle_chunk_error(e)
By default, raise the exception.
[show source]
# File lib/roda/plugins/chunked.rb 310 def handle_chunk_error(e) 311 raise e 312 end
no_chunk!()
Disable chunking for the current request. Mostly useful when chunking is turned on by default.
[show source]
# File lib/roda/plugins/chunked.rb 213 def no_chunk! 214 @_chunked = false 215 end
view(*a)
If chunking by default, call chunked if it hasn’t yet been called and chunking is not specifically disabled.
[show source]
# File lib/roda/plugins/chunked.rb 219 def view(*a) 220 if opts[:chunk_by_default] && @_chunked != false && !defined?(yield) 221 chunked(*a) 222 else 223 super 224 end 225 end