Methods available to routes, before/after filters, and views.
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
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
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
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
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
Access shared logger object.
# File lib/sinatra/base.rb, line 174 def logger request.logger end
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
Halt processing and return a 404 Not Found.
# File lib/sinatra/base.rb, line 158 def not_found(body=nil) error 404, body end
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
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
Access the underlying Rack session.
# File lib/sinatra/base.rb, line 169 def session request.session end
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
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