module Roda::RodaPlugins::SinatraHelpers::ResponseMethods

def attachment(filename = nil, disposition='attachment')

instructing the user agents to prompt to save.
Set the Content-Disposition to "attachment" with the specified filename,
def attachment(filename = nil, disposition='attachment')
  if filename
    param_filename = File.basename(filename)
    encoding = param_filename.encoding
    needs_encoding = param_filename.gsub!(/[^ 0-9a-zA-Z!\#$&\+\.\^_`\|~]+/, '-')
    params = "; filename=#{param_filename.inspect}"
    if needs_encoding && (encoding == UTF8_ENCODING || encoding == ISO88591_ENCODING)
      # File name contains non attr-char characters from RFC 5987 Section 3.2.1
      encoded_filename = File.basename(filename).force_encoding(BINARY_ENCODING)
      # Similar regexp as above, but treat each byte separately, and encode
      # space characters, since those aren't allowed in attr-char
      encoded_filename.gsub!(/[^0-9a-zA-Z!\#$&\+\.\^_`\|~]/) do |c|
        "%%%X" % c.ord
      end
      encoded_params = "; filename*=#{encoding.to_s}''#{encoded_filename}"
    end
    unless @headers[RodaResponseHeaders::CONTENT_TYPE]
      ext = File.extname(filename)
      unless ext.empty?
        content_type(ext)
      end
    end
  end
  @headers[RodaResponseHeaders::CONTENT_DISPOSITION] = "#{disposition}#{params}#{encoded_params}"
end

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

evaluation is deferred until the body is needed.
Set or retrieve the response body. When a block is given,
def body(value = (return @body unless defined?(yield); nil), &block)
  if block
    @body = DelayedBody.new(&block)
  else
    self.body = value
  end
end

def body=(body)

Set the body to the given value.
def body=(body)
  @body = DelayedBody.new{body}
end

def client_error?

Whether or not the status is set to 4xx. Returns nil if status not yet set.
def client_error?
  @status.between?(400, 499) if @status
end

def content_type(type = nil || (return @headers[RodaResponseHeaders::CONTENT_TYPE]), opts = OPTS)

extension. See plugin documentation for options.
Set the Content-Type of the response body given a media type or file
def content_type(type = nil || (return @headers[RodaResponseHeaders::CONTENT_TYPE]), opts = OPTS)
  unless (mime_type = mime_type(type) || opts[:default])
    raise RodaError, "Unknown media type: #{type}"
  end
  unless opts.empty?
    opts.each do |key, val|
      next if key == :default || (key == :charset && mime_type.include?('charset'))
      val = val.inspect if val =~ /[";,]/
      mime_type += "#{mime_type.include?(';') ? ', ' : ';'}#{key}=#{val}"
    end
  end
  @headers[RodaResponseHeaders::CONTENT_TYPE] = mime_type
end

def finish

If the body is a DelayedBody, set the appropriate length for it.
def finish
  @length = @body.length if @body.is_a?(DelayedBody) && !@headers[RodaResponseHeaders::CONTENT_LENGTH]
  super
end

def headers(hash = nil || (return @headers))

argument is given.
Set multiple response headers with Hash, or return the headers if no
def headers(hash = nil || (return @headers))
  @headers.merge!(hash)
end

def informational?

Whether or not the status is set to 1xx. Returns nil if status not yet set.
def informational?
  @status.between?(100, 199) if @status
end

def mime_type(type)

Look up a media type by file extension in Rack's mime registry.
def mime_type(type)
  roda_class.mime_type(type)
end

def not_found?

Whether or not the status is set to 404. Returns nil if status not yet set.
def not_found?
  @status == 404 if @status
end

def redirect?

Whether or not the status is set to 3xx. Returns nil if status not yet set.
def redirect?
  @status.between?(300, 399) if @status
end

def server_error?

Whether or not the status is set to 5xx. Returns nil if status not yet set.
def server_error?
  @status.between?(500, 599) if @status
end

def status(value = nil || (return @status))

Set or retrieve the response status code.
def status(value = nil || (return @status))
  @status = value
end

def success?

Whether or not the status is set to 2xx. Returns nil if status not yet set.
def success?
  @status.between?(200, 299) if @status
end