Methods
public instance
Public instance methods
Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save.
# File lib/sinatra/base.rb, line 120 120: def attachment(filename=nil) 121: response['Content-Disposition'] = 'attachment' 122: if filename 123: params = '; filename="%s"' % File.basename(filename) 124: response['Content-Disposition'] << params 125: end 126: 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 67 67: def body(value=nil, &block) 68: if block_given? 69: def block.each ; yield call ; end 70: response.body = block 71: else 72: response.body = value 73: end 74: end
Set the Content-Type of the response body given a media type or file extension.
# File lib/sinatra/base.rb, line 107 107: def content_type(type, params={}) 108: media_type = self.media_type(type) 109: fail "Unknown media type: %p" % type if media_type.nil? 110: if params.any? 111: params = params.collect { |kv| "%s=%s" % kv }.join(', ') 112: response['Content-Type'] = [media_type, params].join(";") 113: else 114: response['Content-Type'] = media_type 115: end 116: end
Halt processing and return the error status provided.
# File lib/sinatra/base.rb, line 84 84: def error(code, body=nil) 85: code, body = 500, code.to_str if code.respond_to? :to_str 86: response.body = body unless body.nil? 87: halt code 88: end
Set the response entity tag (HTTP ‘ETag’ header) and halt if conditional GET matches. The value argument is an identifier that uniquely identifies the current version of the resource. The strength argument indicates whether the etag should be used as a :strong (default) or :weak cache validator.
When the current request includes an ‘If-None-Match’ header with a matching etag, execution is immediately halted. If the request method is GET or HEAD, a ‘304 Not Modified’ response is sent.
# File lib/sinatra/base.rb, line 175 175: def etag(value, kind=:strong) 176: raise TypeError, ":strong or :weak expected" if ![:strong,:weak].include?(kind) 177: value = '"%s"' % value 178: value = 'W/' + value if kind == :weak 179: response['ETag'] = value 180: 181: # Conditional GET check 182: if etags = env['HTTP_IF_NONE_MATCH'] 183: etags = etags.split(/\s*,\s*/) 184: halt 304 if etags.include?(value) || etags.include?('*') 185: end 186: end
Set the last modified time of the resource (HTTP ‘Last-Modified’ header) and halt if conditional GET matches. The time argument is a Time, DateTime, or other object that responds to to_time.
When the current request includes an ‘If-Modified-Since’ header that matches the time specified, execution is immediately halted with a ‘304 Not Modified’ response.
# File lib/sinatra/base.rb, line 158 158: def last_modified(time) 159: time = time.to_time if time.respond_to?(:to_time) 160: time = time.httpdate if time.respond_to?(:httpdate) 161: response['Last-Modified'] = time 162: halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE'] 163: time 164: end
Look up a media type by file extension in Rack’s mime registry.
# File lib/sinatra/base.rb, line 101 101: def media_type(type) 102: Base.media_type(type) 103: end
Halt processing and return a 404 Not Found.
# File lib/sinatra/base.rb, line 91 91: def not_found(body=nil) 92: error 404, body 93: end
Halt processing and redirect to the URI provided.
# File lib/sinatra/base.rb, line 77 77: def redirectredirect(uri, *args) 78: status 302 79: response['Location'] = uri 80: halt(*args) 81: end
Use the contents of the file as the response body and attempt to
# File lib/sinatra/base.rb, line 129 129: def send_file(path, opts={}) 130: stat = File.stat(path) 131: last_modified stat.mtime 132: content_type media_type(opts[:type]) || 133: media_type(File.extname(path)) || 134: response['Content-Type'] || 135: 'application/octet-stream' 136: response['Content-Length'] ||= (opts[:length] || stat.size).to_s 137: halt StaticFile.open(path, 'rb') 138: rescue Errno::ENOENT 139: not_found 140: end
Access the underlying Rack session.
# File lib/sinatra/base.rb, line 96 96: def session 97: env['rack.session'] ||= {} 98: end
Set or retrieve the response status code.
# File lib/sinatra/base.rb, line 60 60: def status(value=nil) 61: response.status = value if value 62: response.status 63: end