module Sinatra::Helpers

Methods available to routes, before/after filters, and views.

Public Instance Methods

attachment(filename=nil) click to toggle source

Set the Content-Disposition to "attachment" with the specified filename, instructing the user agents to prompt to save.

# File lib/sinatra/base.rb, line 204
def attachment(filename=nil)
  response['Content-Disposition'] = 'attachment'
  if filename
    params = '; filename="%s"' % File.basename(filename)
    response['Content-Disposition'] << params
    ext = File.extname(filename)
    content_type(ext) unless response['Content-Type'] or ext.empty?
  end
end
body(value=nil, &block) click to toggle source

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

# File lib/sinatra/base.rb, line 104
def body(value=nil, &block)
  if block_given?
    def block.each; yield(call) end
    response.body = block
  elsif value
    response.body = value
  else
    response.body
  end
end
content_type(type = nil, params={}) click to toggle source

Set the Content-Type of the response body given a media type or file extension.

# File lib/sinatra/base.rb, line 185
def content_type(type = nil, params={})
  return response['Content-Type'] unless type
  default = params.delete :default
  mime_type = mime_type(type) || default
  fail "Unknown media type: %p" % type if mime_type.nil?
  mime_type = mime_type.dup
  unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type }
    params[:charset] = params.delete('charset') || settings.default_encoding
  end
  params.delete :charset if mime_type.include? 'charset'
  unless params.empty?
    mime_type << (mime_type.include?(';') ? ', ' : ';')
    mime_type << params.map { |kv| kv.join('=') }.join(', ')
  end
  response['Content-Type'] = mime_type
end
error(code, body=nil) click to toggle source

Halt processing and return the error status provided.

# File lib/sinatra/base.rb, line 151
def error(code, body=nil)
  code, body    = 500, code.to_str if code.respond_to? :to_str
  response.body = body unless body.nil?
  halt code
end
headers(hash=nil) click to toggle source

Set multiple response headers with Hash.

# File lib/sinatra/base.rb, line 163
def headers(hash=nil)
  response.headers.merge! hash if hash
  response.headers
end
logger() click to toggle source

Access shared logger object.

# File lib/sinatra/base.rb, line 174
def logger
  request.logger
end
mime_type(type) click to toggle source

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

# File lib/sinatra/base.rb, line 179
def mime_type(type)
  Base.mime_type(type)
end
not_found(body=nil) click to toggle source

Halt processing and return a 404 Not Found.

# File lib/sinatra/base.rb, line 158
def not_found(body=nil)
  error 404, body
end
redirect(uri, *args) click to toggle source

Halt processing and redirect to the URI provided.

# File lib/sinatra/base.rb, line 116
def redirect(uri, *args)
  if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET'
    status 303
  else
    status 302
  end

  # According to RFC 2616 section 14.30, "the field value consists of a
  # single absolute URI"
  response['Location'] = uri(uri, settings.absolute_redirects?, settings.prefixed_redirects?)
  halt(*args)
end
send_file(path, opts={}) click to toggle source

Use the contents of the file at path as the response body.

# File lib/sinatra/base.rb, line 215
def send_file(path, opts={})
  if opts[:type] or not response['Content-Type']
    content_type opts[:type] || File.extname(path), :default => 'application/octet-stream'
  end

  if opts[:disposition] == 'attachment' || opts[:filename]
    attachment opts[:filename] || path
  elsif opts[:disposition] == 'inline'
    response['Content-Disposition'] = 'inline'
  end

  last_modified opts[:last_modified] if opts[:last_modified]

  file      = Rack::File.new nil
  file.path = path
  result    = file.serving env
  result[1].each { |k,v| headers[k] ||= v }
  halt result[0], result[2]
rescue Errno::ENOENT
  not_found
end
session() click to toggle source

Access the underlying Rack session.

# File lib/sinatra/base.rb, line 169
def session
  request.session
end
status(value=nil) click to toggle source

Set or retrieve the response status code.

# File lib/sinatra/base.rb, line 97
def status(value=nil)
  response.status = value if value
  response.status
end
to(addr = nil, absolute = true, add_script_name = true) click to toggle source
Alias for: uri
uri(addr = nil, absolute = true, add_script_name = true) click to toggle source

Generates the absolute URI for a given path in the app. Takes Rack routers and reverse proxies into account.

# File lib/sinatra/base.rb, line 131
def uri(addr = nil, absolute = true, add_script_name = true)
  return addr if addr =~ %r\A[A-z][A-z0-9\+\.\-]*:/
  uri = [host = ""]
  if absolute
    host << "http#{'s' if request.secure?}://"
    if request.forwarded? or request.port != (request.secure? ? 443 : 80)
      host << request.host_with_port
    else
      host << request.host
    end
  end
  uri << request.script_name.to_s if add_script_name
  uri << (addr ? addr : request.path_info).to_s
  File.join uri
end
Also aliased as: url, to
url(addr = nil, absolute = true, add_script_name = true) click to toggle source
Alias for: uri