module Sinatra::Helpers

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

Public Instance Methods

attachment(filename = nil, disposition = 'attachment') 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
340 def attachment(filename = nil, disposition = 'attachment')
341   response['Content-Disposition'] = disposition.to_s
342   if filename
343     params = '; filename="%s"' % File.basename(filename)
344     response['Content-Disposition'] << params
345     ext = File.extname(filename)
346     content_type(ext) unless response['Content-Type'] or ext.empty?
347   end
348 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
236 def body(value = nil, &block)
237   if block_given?
238     def block.each; yield(call) end
239     response.body = block
240   elsif value
241     headers.delete 'Content-Length' unless request.head? || value.is_a?(Rack::File) || value.is_a?(Stream)
242     response.body = value
243   else
244     response.body
245   end
246 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
318 def content_type(type = nil, params = {})
319   return response['Content-Type'] unless type
320   default = params.delete :default
321   mime_type = mime_type(type) || default
322   fail "Unknown media type: %p" % type if mime_type.nil?
323   mime_type = mime_type.dup
324   unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type }
325     params[:charset] = params.delete('charset') || settings.default_encoding
326   end
327   params.delete :charset if mime_type.include? 'charset'
328   unless params.empty?
329     mime_type << (mime_type.include?(';') ? ', ' : ';')
330     mime_type << params.map do |key, val|
331       val = val.inspect if val =~ /[";,]/
332       "#{key}=#{val}"
333     end.join(', ')
334   end
335   response['Content-Type'] = mime_type
336 end
error(code, body = nil) click to toggle source

Halt processing and return the error status provided.

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

Set multiple response headers with Hash.

    # File lib/sinatra/base.rb
296 def headers(hash = nil)
297   response.headers.merge! hash if hash
298   response.headers
299 end
logger() click to toggle source

Access shared logger object.

    # File lib/sinatra/base.rb
307 def logger
308   request.logger
309 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
312 def mime_type(type)
313   Base.mime_type(type)
314 end
not_found(body = nil) click to toggle source

Halt processing and return a 404 Not Found.

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

Halt processing and redirect to the URI provided.

    # File lib/sinatra/base.rb
249 def redirect(uri, *args)
250   if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET'
251     status 303
252   else
253     status 302
254   end
255 
256   # According to RFC 2616 section 14.30, "the field value consists of a
257   # single absolute URI"
258   response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?)
259   halt(*args)
260 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
351 def send_file(path, opts = {})
352   if opts[:type] or not response['Content-Type']
353     content_type opts[:type] || File.extname(path), :default => 'application/octet-stream'
354   end
355 
356   disposition = opts[:disposition]
357   filename    = opts[:filename]
358   disposition = 'attachment' if disposition.nil? and filename
359   filename    = path         if filename.nil?
360   attachment(filename, disposition) if disposition
361 
362   last_modified opts[:last_modified] if opts[:last_modified]
363 
364   file      = Rack::File.new nil
365   file.path = path
366   result    = file.serving env
367   result[1].each { |k,v| headers[k] ||= v }
368   headers['Content-Length'] = result[1]['Content-Length']
369   opts[:status] &&= Integer(opts[:status])
370   halt opts[:status] || result[0], result[2]
371 rescue Errno::ENOENT
372   not_found
373 end
session() click to toggle source

Access the underlying Rack session.

    # File lib/sinatra/base.rb
302 def session
303   request.session
304 end
status(value = nil) click to toggle source

Set or retrieve the response status code.

    # File lib/sinatra/base.rb
229 def status(value = nil)
230   response.status = value if value
231   response.status
232 end
to(addr = nil, absolute = true, add_script_name = true)
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
264 def uri(addr = nil, absolute = true, add_script_name = true)
265   return addr if addr =~ /\A[A-z][A-z0-9\+\.\-]*:/
266   uri = [host = ""]
267   if absolute
268     host << "http#{'s' if request.secure?}://"
269     if request.forwarded? or request.port != (request.secure? ? 443 : 80)
270       host << request.host_with_port
271     else
272       host << request.host
273     end
274   end
275   uri << request.script_name.to_s if add_script_name
276   uri << (addr ? addr : request.path_info).to_s
277   File.join uri
278 end
Also aliased as: url, to
url(addr = nil, absolute = true, add_script_name = true)
Alias for: uri