Module | Sinatra::Helpers |
In: |
lib/sinatra/base.rb
|
Methods available to routes, before 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 134 134: def attachment(filename=nil) 135: response['Content-Disposition'] = 'attachment' 136: if filename 137: params = '; filename="%s"' % File.basename(filename) 138: response['Content-Disposition'] << params 139: end 140: 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 75 75: def body(value=nil, &block) 76: if block_given? 77: def block.each ; yield call ; end 78: response.body = block 79: else 80: response.body = value 81: end 82: end
Set the Content-Type of the response body given a media type or file extension.
# File lib/sinatra/base.rb, line 121 121: def content_type(type, params={}) 122: media_type = self.media_type(type) 123: fail "Unknown media type: %p" % type if media_type.nil? 124: if params.any? 125: params = params.collect { |kv| "%s=%s" % kv }.join(', ') 126: response['Content-Type'] = [media_type, params].join(";") 127: else 128: response['Content-Type'] = media_type 129: end 130: 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 199 199: def etag(value, kind=:strong) 200: raise TypeError, ":strong or :weak expected" if ![:strong,:weak].include?(kind) 201: value = '"%s"' % value 202: value = 'W/' + value if kind == :weak 203: response['ETag'] = value 204: 205: # Conditional GET check 206: if etags = env['HTTP_IF_NONE_MATCH'] 207: etags = etags.split(/\s*,\s*/) 208: halt 304 if etags.include?(value) || etags.include?('*') 209: end 210: 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 182 182: def last_modified(time) 183: time = time.to_time if time.respond_to?(:to_time) 184: time = time.httpdate if time.respond_to?(:httpdate) 185: response['Last-Modified'] = time 186: halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE'] 187: time 188: end
Look up a media type by file extension in Rack‘s mime registry.
# File lib/sinatra/base.rb, line 115 115: def media_type(type) 116: Base.media_type(type) 117: end
Halt processing and return a 404 Not Found.
# File lib/sinatra/base.rb, line 99 99: def not_found(body=nil) 100: error 404, body 101: end
Use the contents of the file at path as the response body.
# File lib/sinatra/base.rb, line 143 143: def send_file(path, opts={}) 144: stat = File.stat(path) 145: last_modified stat.mtime 146: 147: content_type media_type(opts[:type]) || 148: media_type(File.extname(path)) || 149: response['Content-Type'] || 150: 'application/octet-stream' 151: 152: response['Content-Length'] ||= (opts[:length] || stat.size).to_s 153: 154: if opts[:disposition] == 'attachment' || opts[:filename] 155: attachment opts[:filename] || path 156: elsif opts[:disposition] == 'inline' 157: response['Content-Disposition'] = 'inline' 158: end 159: 160: halt StaticFile.open(path, 'rb') 161: rescue Errno::ENOENT 162: not_found 163: end