module Sinatra::Helpers

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

Constants

MULTIPART_FORM_DATA_REPLACEMENT_TABLE

html.spec.whatwg.org/#multipart-form-data

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
372 def attachment(filename = nil, disposition = :attachment)
373   response['Content-Disposition'] = disposition.to_s.dup
374   return unless filename
375 
376   params = format('; filename="%s"', File.basename(filename).gsub(/["\r\n]/, MULTIPART_FORM_DATA_REPLACEMENT_TABLE))
377   response['Content-Disposition'] << params
378   ext = File.extname(filename)
379   content_type(ext) unless response['Content-Type'] || ext.empty?
380 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
257 def body(value = nil, &block)
258   if block_given?
259     def block.each; yield(call) end
260     response.body = block
261   elsif value
262     # Rack 2.0 returns a Rack::File::Iterator here instead of
263     # Rack::File as it was in the previous API.
264     unless request.head? || value.is_a?(Rack::File::Iterator) || value.is_a?(Stream)
265       headers.delete 'Content-Length'
266     end
267     response.body = value
268   else
269     response.body
270   end
271 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
343 def content_type(type = nil, params = {})
344   return response['Content-Type'] unless type
345   default = params.delete :default
346   mime_type = mime_type(type) || default
347   fail "Unknown media type: %p" % type if mime_type.nil?
348   mime_type = mime_type.dup
349   unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type }
350     params[:charset] = params.delete('charset') || settings.default_encoding
351   end
352   params.delete :charset if mime_type.include? 'charset'
353   unless params.empty?
354     mime_type << (mime_type.include?(';') ? ', ' : ';')
355     mime_type << params.map do |key, val|
356       val = val.inspect if val =~ /[";,]/
357       "#{key}=#{val}"
358     end.join(', ')
359   end
360   response['Content-Type'] = mime_type
361 end
error(code, body = nil) click to toggle source

Halt processing and return the error status provided.

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

Set multiple response headers with Hash.

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

Access shared logger object.

    # File lib/sinatra/base.rb
332 def logger
333   request.logger
334 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
337 def mime_type(type)
338   Base.mime_type(type)
339 end
not_found(body = nil) click to toggle source

Halt processing and return a 404 Not Found.

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

Halt processing and redirect to the URI provided.

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

Access the underlying Rack session.

    # File lib/sinatra/base.rb
327 def session
328   request.session
329 end
status(value = nil) click to toggle source

Set or retrieve the response status code.

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