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