| Module | Sinatra::Helpers |
| In: |
lib/sinatra/base.rb
|
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 141
141: def attachment(filename=nil)
142: response['Content-Disposition'] = 'attachment'
143: if filename
144: params = '; filename="%s"' % File.basename(filename)
145: response['Content-Disposition'] << params
146: end
147: 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 82
82: def body(value=nil, &block)
83: if block_given?
84: def block.each ; yield call ; end
85: response.body = block
86: else
87: response.body = value
88: end
89: end
Specify response freshness policy for HTTP caches (Cache-Control header). Any number of non-value directives (:public, :private, :no_cache, :no_store, :must_revalidate, :proxy_revalidate) may be passed along with a Hash of value directives (:max_age, :min_stale, :s_max_age).
cache_control :public, :must_revalidate, :max_age => 60 => Cache-Control: public, must-revalidate, max-age=60
See RFC 2616 / 14.9 for more on standard cache control directives: tools.ietf.org/html/rfc2616#section-14.9.1
# File lib/sinatra/base.rb, line 194
194: def cache_control(*values)
195: if values.last.kind_of?(Hash)
196: hash = values.pop
197: hash.reject! { |k,v| v == false }
198: hash.reject! { |k,v| values << k if v == true }
199: else
200: hash = {}
201: end
202:
203: values = values.map { |value| value.to_s.tr('_','-') }
204: hash.each { |k,v| values << [k.to_s.tr('_', '-'), v].join('=') }
205:
206: response['Cache-Control'] = values.join(', ') if values.any?
207: end
Set the Content-Type of the response body given a media type or file extension.
# File lib/sinatra/base.rb, line 128
128: def content_type(type, params={})
129: mime_type = self.mime_type(type)
130: fail "Unknown media type: %p" % type if mime_type.nil?
131: if params.any?
132: params = params.collect { |kv| "%s=%s" % kv }.join(', ')
133: response['Content-Type'] = [mime_type, params].join(";")
134: else
135: response['Content-Type'] = mime_type
136: end
137: 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 kind 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 259
259: def etag(value, kind=:strong)
260: raise TypeError, ":strong or :weak expected" if ![:strong,:weak].include?(kind)
261: value = '"%s"' % value
262: value = 'W/' + value if kind == :weak
263: response['ETag'] = value
264:
265: # Conditional GET check
266: if etags = env['HTTP_IF_NONE_MATCH']
267: etags = etags.split(/\s*,\s*/)
268: halt 304 if etags.include?(value) || etags.include?('*')
269: end
270: end
Set the Expires header and Cache-Control/max-age directive. Amount can be an integer number of seconds in the future or a Time object indicating when the response should be considered "stale". The remaining "values" arguments are passed to the cache_control helper:
expires 500, :public, :must_revalidate => Cache-Control: public, must-revalidate, max-age=60 => Expires: Mon, 08 Jun 2009 08:50:17 GMT
# File lib/sinatra/base.rb, line 218
218: def expires(amount, *values)
219: values << {} unless values.last.kind_of?(Hash)
220:
221: if amount.respond_to?(:to_time)
222: max_age = amount.to_time - Time.now
223: time = amount.to_time
224: else
225: max_age = amount
226: time = Time.now + amount
227: end
228:
229: values.last.merge!(:max_age => max_age)
230: cache_control(*values)
231:
232: response['Expires'] = time.httpdate
233: 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 242
242: def last_modified(time)
243: time = time.to_time if time.respond_to?(:to_time)
244: time = time.httpdate if time.respond_to?(:httpdate)
245: response['Last-Modified'] = time
246: halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE']
247: time
248: end
Look up a media type by file extension in Rack‘s mime registry.
# File lib/sinatra/base.rb, line 122
122: def mime_type(type)
123: Base.mime_type(type)
124: end
Halt processing and return a 404 Not Found.
# File lib/sinatra/base.rb, line 106
106: def not_found(body=nil)
107: error 404, body
108: end
Use the contents of the file at path as the response body.
# File lib/sinatra/base.rb, line 150
150: def send_file(path, opts={})
151: stat = File.stat(path)
152: last_modified stat.mtime
153:
154: content_type mime_type(opts[:type]) ||
155: mime_type(File.extname(path)) ||
156: response['Content-Type'] ||
157: 'application/octet-stream'
158:
159: response['Content-Length'] ||= (opts[:length] || stat.size).to_s
160:
161: if opts[:disposition] == 'attachment' || opts[:filename]
162: attachment opts[:filename] || path
163: elsif opts[:disposition] == 'inline'
164: response['Content-Disposition'] = 'inline'
165: end
166:
167: halt StaticFile.open(path, 'rb')
168: rescue Errno::ENOENT
169: not_found
170: end