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
330 def body(value = (return @body unless defined?(yield); nil), &block)
331   if block
332     @body = DelayedBody.new(&block)
333   else
334     self.body = value
335   end
336 end
body=(body)

Set the body to the given value.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
339 def body=(body)
340   @body = DelayedBody.new{body}
341 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
394 def client_error?
395   @status.between?(400, 499) if @status
396 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
362 def content_type(type = nil || (return @headers[RodaResponseHeaders::CONTENT_TYPE]), opts = OPTS)
363   unless (mime_type = mime_type(type) || opts[:default])
364     raise RodaError, "Unknown media type: #{type}"
365   end
366 
367   unless opts.empty?
368     opts.each do |key, val|
369       next if key == :default || (key == :charset && mime_type.include?('charset'))
370       val = val.inspect if val =~ /[";,]/
371       mime_type += "#{mime_type.include?(';') ? ', ' : ';'}#{key}=#{val}"
372     end
373   end
374 
375   @headers[RodaResponseHeaders::CONTENT_TYPE] = mime_type
376 end
finish()

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

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
344 def finish
345   @length = @body.length if @body.is_a?(DelayedBody) && !@headers[RodaResponseHeaders::CONTENT_LENGTH]
346   super
347 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
351 def headers(hash = nil || (return @headers))
352   @headers.merge!(hash)
353 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
379 def informational?
380   @status.between?(100, 199) if @status
381 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
356 def mime_type(type)
357   roda_class.mime_type(type)
358 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
404 def not_found?
405   @status == 404 if @status
406 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
389 def redirect?
390   @status.between?(300, 399) if @status
391 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
399 def server_error?
400   @status.between?(500, 599) if @status
401 end
status(value = nil || (return @status))

Set or retrieve the response status code.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
324 def status(value = nil || (return @status))
325   @status = value
326 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
384 def success?
385   @status.between?(200, 299) if @status
386 end