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

Set the body to the given value.

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

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

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

Set or retrieve the response status code.

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