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
362 def attachment(filename = nil, disposition = :attachment)
363   response['Content-Disposition'] = disposition.to_s
364   if filename
365     params = '; filename="%s"' % File.basename(filename)
366     response['Content-Disposition'] << params
367     ext = File.extname(filename)
368     content_type(ext) unless response['Content-Type'] or ext.empty?
369   end
370 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
254 def body(value = nil, &block)
255   if block_given?
256     def block.each; yield(call) end
257     response.body = block
258   elsif value
259     # Rack 2.0 returns a Rack::File::Iterator here instead of
260     # Rack::File as it was in the previous API.
261     unless request.head? || value.is_a?(Rack::File::Iterator) || value.is_a?(Stream)
262       headers.delete 'Content-Length'
263     end
264     response.body = value
265   else
266     response.body
267   end
268 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
340 def content_type(type = nil, params = {})
341   return response['Content-Type'] unless type
342   default = params.delete :default
343   mime_type = mime_type(type) || default
344   fail "Unknown media type: %p" % type if mime_type.nil?
345   mime_type = mime_type.dup
346   unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type }
347     params[:charset] = params.delete('charset') || settings.default_encoding
348   end
349   params.delete :charset if mime_type.include? 'charset'
350   unless params.empty?
351     mime_type << (mime_type.include?(';') ? ', ' : ';')
352     mime_type << params.map do |key, val|
353       val = val.inspect if val =~ /[";,]/
354       "#{key}=#{val}"
355     end.join(', ')
356   end
357   response['Content-Type'] = mime_type
358 end
error(code, body = nil) click to toggle source

Halt processing and return the error status provided.

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

Set multiple response headers with Hash.

    # File lib/sinatra/base.rb
318 def headers(hash = nil)
319   response.headers.merge! hash if hash
320   response.headers
321 end
logger() click to toggle source

Access shared logger object.

    # File lib/sinatra/base.rb
329 def logger
330   request.logger
331 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
334 def mime_type(type)
335   Base.mime_type(type)
336 end
not_found(body = nil) click to toggle source

Halt processing and return a 404 Not Found.

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

Halt processing and redirect to the URI provided.

    # File lib/sinatra/base.rb
271 def redirect(uri, *args)
272   if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET'
273     status 303
274   else
275     status 302
276   end
277 
278   # According to RFC 2616 section 14.30, "the field value consists of a
279   # single absolute URI"
280   response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?)
281   halt(*args)
282 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
373 def send_file(path, opts = {})
374   if opts[:type] or not response['Content-Type']
375     content_type opts[:type] || File.extname(path), :default => 'application/octet-stream'
376   end
377 
378   disposition = opts[:disposition]
379   filename    = opts[:filename]
380   disposition = :attachment if disposition.nil? and filename
381   filename    = path        if filename.nil?
382   attachment(filename, disposition) if disposition
383 
384   last_modified opts[:last_modified] if opts[:last_modified]
385 
386   file   = Rack::File.new(File.dirname(settings.app_file))
387   result = file.serving(request, path)
388 
389   result[1].each { |k,v| headers[k] ||= v }
390   headers['Content-Length'] = result[1]['Content-Length']
391   opts[:status] &&= Integer(opts[:status])
392   halt (opts[:status] || result[0]), result[2]
393 rescue Errno::ENOENT
394   not_found
395 end
session() click to toggle source

Access the underlying Rack session.

    # File lib/sinatra/base.rb
324 def session
325   request.session
326 end
status(value = nil) click to toggle source

Set or retrieve the response status code.

    # File lib/sinatra/base.rb
247 def status(value = nil)
248   response.status = Rack::Utils.status_code(value) if value
249   response.status
250 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
286 def uri(addr = nil, absolute = true, add_script_name = true)
287   return addr if addr =~ /\A[a-z][a-z0-9\+\.\-]*:/i
288   uri = [host = String.new]
289   if absolute
290     host << "http#{'s' if request.secure?}://"
291     if request.forwarded? or request.port != (request.secure? ? 443 : 80)
292       host << request.host_with_port
293     else
294       host << request.host
295     end
296   end
297   uri << request.script_name.to_s if add_script_name
298   uri << (addr ? addr : request.path_info).to_s
299   File.join uri
300 end
Also aliased as: url, to
url(addr = nil, absolute = true, add_script_name = true)
Alias for: uri