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 237 def chunked(template, opts=OPTS, &block) 238 if @_chunked.nil? 239 @_chunked = !self.class.force_chunked_encoding? || @_request.http_version == "HTTP/1.1" 240 end 241 242 if block 243 delay(&block) 244 end 245 246 unless @_chunked 247 # If chunking is disabled, do a normal rendering of the view. 248 run_delayed_blocks 249 return view(template, opts) 250 end 251 252 if template.is_a?(Hash) 253 if opts.empty? 254 opts = template 255 else 256 opts = opts.merge(template) 257 end 258 end 259 260 # Hack so that the arguments don't need to be passed 261 # through the response and body objects. 262 @_each_chunk_args = [template, opts] 263 264 res = response 265 headers = res.headers 266 if chunk_headers = self.class.chunk_headers 267 headers.merge!(chunk_headers) 268 end 269 if self.class.force_chunked_encoding? 270 res[RodaResponseHeaders::TRANSFER_ENCODING] = 'chunked' 271 body = Body.new(self) 272 else 273 body = StreamBody.new(self) 274 end 275 276 throw :halt, res.finish_with_body(body) 277 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 281 def delay(&block) 282 raise RodaError, "must pass a block to Roda#delay" unless block 283 (@_delays ||= []) << block 284 end
each_chunk()
Yield each chunk of the template rendering separately.
[show source]
# File lib/roda/plugins/chunked.rb 287 def each_chunk 288 response.body.each{|s| yield s} 289 290 template, opts = @_each_chunk_args 291 292 # Use a lambda for the flusher, so that a call to flush 293 # by a template can result in this method yielding a chunk 294 # of the response. 295 @_flusher = lambda do 296 yield @_out_buf 297 @_out_buf = String.new 298 end 299 300 if layout_opts = view_layout_opts(opts) 301 @_out_buf = render_template(layout_opts) do 302 flush 303 run_delayed_blocks 304 yield opts[:content] || render_template(template, opts) 305 nil 306 end 307 else 308 run_delayed_blocks 309 yield view(template, opts) 310 end 311 312 flush 313 rescue => e 314 handle_chunk_error(e) 315 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 325 def flush 326 @_flusher.call if @_flusher 327 end
handle_chunk_error(e)
By default, raise the exception.
[show source]
# File lib/roda/plugins/chunked.rb 318 def handle_chunk_error(e) 319 raise e 320 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 221 def no_chunk! 222 @_chunked = false 223 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 227 def view(*a) 228 if self.class.chunk_by_default? && @_chunked != false && !defined?(yield) 229 chunked(*a) 230 else 231 super 232 end 233 end