module Roda::RodaPlugins::SinatraHelpers::ResponseMethods

  1. lib/roda/plugins/sinatra_helpers.rb

Public Instance methods

body(value = (return @body unless defined?(yield); nil), &block)

Set or retrieve the response body. When a block is given, evaluation is deferred until the body is needed.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
335 def body(value = (return @body unless defined?(yield); nil), &block)
336   if block
337     @body = DelayedBody.new(&block)
338   else
339     self.body = value
340   end
341 end
body=(body)

Set the body to the given value.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
344 def body=(body)
345   @body = DelayedBody.new{body}
346 end
client_error?()

Whether or not the status is set to 4xx. Returns nil if status not yet set.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
399 def client_error?
400   @status.between?(400, 499) if @status
401 end
content_type(type = nil || (return @headers[RodaResponseHeaders::CONTENT_TYPE]), opts = OPTS)

Set the Content-Type of the response body given a media type or file extension. See plugin documentation for options.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
367 def content_type(type = nil || (return @headers[RodaResponseHeaders::CONTENT_TYPE]), opts = OPTS)
368   unless (mime_type = mime_type(type) || opts[:default])
369     raise RodaError, "Unknown media type: #{type}"
370   end
371 
372   unless opts.empty?
373     opts.each do |key, val|
374       next if key == :default || (key == :charset && mime_type.include?('charset'))
375       val = val.inspect if val =~ /[";,]/
376       mime_type += "#{mime_type.include?(';') ? ', ' : ';'}#{key}=#{val}"
377     end
378   end
379 
380   @headers[RodaResponseHeaders::CONTENT_TYPE] = mime_type
381 end
finish()

If the body is a DelayedBody, set the appropriate length for it.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
349 def finish
350   @length = @body.length if @body.is_a?(DelayedBody) && !@headers[RodaResponseHeaders::CONTENT_LENGTH]
351   super
352 end
headers(hash = nil || (return @headers))

Set multiple response headers with Hash, or return the headers if no argument is given.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
356 def headers(hash = nil || (return @headers))
357   @headers.merge!(hash)
358 end
informational?()

Whether or not the status is set to 1xx. Returns nil if status not yet set.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
384 def informational?
385   @status.between?(100, 199) if @status
386 end
mime_type(type)

Look up a media type by file extension in Rack’s mime registry.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
361 def mime_type(type)
362   roda_class.mime_type(type)
363 end
not_found?()

Whether or not the status is set to 404. Returns nil if status not yet set.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
409 def not_found?
410   @status == 404 if @status
411 end
redirect?()

Whether or not the status is set to 3xx. Returns nil if status not yet set.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
394 def redirect?
395   @status.between?(300, 399) if @status
396 end
server_error?()

Whether or not the status is set to 5xx. Returns nil if status not yet set.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
404 def server_error?
405   @status.between?(500, 599) if @status
406 end
status(value = nil || (return @status))

Set or retrieve the response status code.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
329 def status(value = nil || (return @status))
330   @status = value
331 end
success?()

Whether or not the status is set to 2xx. Returns nil if status not yet set.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
389 def success?
390   @status.between?(200, 299) if @status
391 end