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
386 def attachment(filename = nil, disposition = :attachment)
387   response['Content-Disposition'] = disposition.to_s.dup
388   if filename
389     params = '; filename="%s"' % File.basename(filename)
390     response['Content-Disposition'] << params
391     ext = File.extname(filename)
392     content_type(ext) unless response['Content-Type'] or ext.empty?
393   end
394 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
278 def body(value = nil, &block)
279   if block_given?
280     def block.each; yield(call) end
281     response.body = block
282   elsif value
283     # Rack 2.0 returns a Rack::File::Iterator here instead of
284     # Rack::File as it was in the previous API.
285     unless request.head? || value.is_a?(Rack::File::Iterator) || value.is_a?(Stream)
286       headers.delete 'Content-Length'
287     end
288     response.body = value
289   else
290     response.body
291   end
292 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
364 def content_type(type = nil, params = {})
365   return response['Content-Type'] unless type
366   default = params.delete :default
367   mime_type = mime_type(type) || default
368   fail "Unknown media type: %p" % type if mime_type.nil?
369   mime_type = mime_type.dup
370   unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type }
371     params[:charset] = params.delete('charset') || settings.default_encoding
372   end
373   params.delete :charset if mime_type.include? 'charset'
374   unless params.empty?
375     mime_type << (mime_type.include?(';') ? ', ' : ';')
376     mime_type << params.map do |key, val|
377       val = val.inspect if val =~ /[";,]/
378       "#{key}=#{val}"
379     end.join(', ')
380   end
381   response['Content-Type'] = mime_type
382 end
error(code, body = nil) click to toggle source

Halt processing and return the error status provided.

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

Set multiple response headers with Hash.

    # File lib/sinatra/base.rb
342 def headers(hash = nil)
343   response.headers.merge! hash if hash
344   response.headers
345 end
logger() click to toggle source

Access shared logger object.

    # File lib/sinatra/base.rb
353 def logger
354   request.logger
355 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
358 def mime_type(type)
359   Base.mime_type(type)
360 end
not_found(body = nil) click to toggle source

Halt processing and return a 404 Not Found.

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

Halt processing and redirect to the URI provided.

    # File lib/sinatra/base.rb
295 def redirect(uri, *args)
296   if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET'
297     status 303
298   else
299     status 302
300   end
301 
302   # According to RFC 2616 section 14.30, "the field value consists of a
303   # single absolute URI"
304   response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?)
305   halt(*args)
306 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
397 def send_file(path, opts = {})
398   if opts[:type] or not response['Content-Type']
399     content_type opts[:type] || File.extname(path), :default => 'application/octet-stream'
400   end
401 
402   disposition = opts[:disposition]
403   filename    = opts[:filename]
404   disposition = :attachment if disposition.nil? and filename
405   filename    = path        if filename.nil?
406   attachment(filename, disposition) if disposition
407 
408   last_modified opts[:last_modified] if opts[:last_modified]
409 
410   file   = Rack::File.new(File.dirname(settings.app_file))
411   result = file.serving(request, path)
412 
413   result[1].each { |k,v| headers[k] ||= v }
414   headers['Content-Length'] = result[1]['Content-Length']
415   opts[:status] &&= Integer(opts[:status])
416   halt (opts[:status] || result[0]), result[2]
417 rescue Errno::ENOENT
418   not_found
419 end
session() click to toggle source

Access the underlying Rack session.

    # File lib/sinatra/base.rb
348 def session
349   request.session
350 end
status(value = nil) click to toggle source

Set or retrieve the response status code.

    # File lib/sinatra/base.rb
271 def status(value = nil)
272   response.status = Rack::Utils.status_code(value) if value
273   response.status
274 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
310 def uri(addr = nil, absolute = true, add_script_name = true)
311   return addr if addr =~ /\A[a-z][a-z0-9\+\.\-]*:/i
312   uri = [host = String.new]
313   if absolute
314     host << "http#{'s' if request.secure?}://"
315     if request.forwarded? or request.port != (request.secure? ? 443 : 80)
316       host << request.host_with_port
317     else
318       host << request.host
319     end
320   end
321   uri << request.script_name.to_s if add_script_name
322   uri << (addr ? addr : request.path_info).to_s
323   File.join uri
324 end
Also aliased as: url, to
url(addr = nil, absolute = true, add_script_name = true)
Alias for: uri