Module: Roda::RodaPlugins::Chunked::InstanceMethods
- Defined in:
- lib/roda/plugins/chunked.rb
Instance Method Summary collapse
-
#chunked(template, opts = OPTS, &block) ⇒ Object
Render a response to the user in chunks.
-
#delay(&block) ⇒ Object
Delay the execution of the block until right before the content template is to be rendered.
-
#each_chunk ⇒ Object
Yield each chunk of the template rendering separately.
-
#flush ⇒ Object
Call the flusher if one is defined.
-
#handle_chunk_error(e) ⇒ Object
By default, raise the exception.
-
#no_chunk! ⇒ Object
Disable chunking for the current request.
-
#view(*a) ⇒ Object
If chunking by default, call chunked if it hasn’t yet been called and chunking is not specifically disabled.
Instance Method Details
#chunked(template, opts = OPTS, &block) ⇒ Object
Render a response to the user in chunks. See Chunked for an overview. If a block is given, it is passed to #delay.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/roda/plugins/chunked.rb', line 215 def chunked(template, opts=OPTS, &block) unless defined?(@_chunked) @_chunked = env[HTTP_VERSION] == HTTP11 end if block delay(&block) end unless @_chunked # If chunking is disabled, do a normal rendering of the view. run_delayed_blocks return view(template, opts) end if template.is_a?(Hash) if opts.empty? opts = template else opts = Hash[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] 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 |
#delay(&block) ⇒ Object
Delay the execution of the block until right before the content template is to be rendered.
254 255 256 257 |
# File 'lib/roda/plugins/chunked.rb', line 254 def delay(&block) raise RodaError, "must pass a block to Roda#delay" unless block (@_delays ||= []) << block end |
#each_chunk ⇒ Object
Yield each chunk of the template rendering separately.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/roda/plugins/chunked.rb', line 260 def each_chunk response.body.each{|s| yield s} template, opts = @_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 = String.new end if layout_opts = view_layout_opts(opts) @_out_buf = render_template(layout_opts) do flush run_delayed_blocks yield opts[:content] || render_template(template, opts) nil end else run_delayed_blocks yield view(template, opts) end flush rescue => e handle_chunk_error(e) end |
#flush ⇒ Object
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.
298 299 300 |
# File 'lib/roda/plugins/chunked.rb', line 298 def flush @_flusher.call if @_flusher end |
#handle_chunk_error(e) ⇒ Object
By default, raise the exception.
291 292 293 |
# File 'lib/roda/plugins/chunked.rb', line 291 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.
199 200 201 |
# File 'lib/roda/plugins/chunked.rb', line 199 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.
205 206 207 208 209 210 211 |
# File 'lib/roda/plugins/chunked.rb', line 205 def view(*a) if opts[:chunk_by_default] && !defined?(@_chunked) chunked(*a) else super end end |