Module Sinatra::Helpers
In: lib/sinatra/base.rb

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

Methods

Public Instance methods

Set the Content-Disposition to "attachment" with the specified filename, instructing the user agents to prompt to save.

[Source]

     # File lib/sinatra/base.rb, line 147
147:     def attachment(filename=nil)
148:       response['Content-Disposition'] = 'attachment'
149:       if filename
150:         params = '; filename="%s"' % File.basename(filename)
151:         response['Content-Disposition'] << params
152:       end
153:     end

Sugar for redirect (example: redirect back)

[Source]

     # File lib/sinatra/base.rb, line 280
280:     def back ; request.referer ; end

Set or retrieve the response body. When a block is given, evaluation is deferred until the body is read with each.

[Source]

    # File lib/sinatra/base.rb, line 88
88:     def body(value=nil, &block)
89:       if block_given?
90:         def block.each ; yield call ; end
91:         response.body = block
92:       else
93:         response.body = value
94:       end
95:     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

[Source]

     # File lib/sinatra/base.rb, line 200
200:     def cache_control(*values)
201:       if values.last.kind_of?(Hash)
202:         hash = values.pop
203:         hash.reject! { |k,v| v == false }
204:         hash.reject! { |k,v| values << k if v == true }
205:       else
206:         hash = {}
207:       end
208: 
209:       values = values.map { |value| value.to_s.tr('_','-') }
210:       hash.each { |k,v| values << [k.to_s.tr('_', '-'), v].join('=') }
211: 
212:       response['Cache-Control'] = values.join(', ') if values.any?
213:     end

Set the Content-Type of the response body given a media type or file extension.

[Source]

     # File lib/sinatra/base.rb, line 134
134:     def content_type(type, params={})
135:       mime_type = self.mime_type(type)
136:       fail "Unknown media type: %p" % type if mime_type.nil?
137:       if params.any?
138:         params = params.collect { |kv| "%s=%s" % kv }.join(', ')
139:         response['Content-Type'] = [mime_type, params].join(";")
140:       else
141:         response['Content-Type'] = mime_type
142:       end
143:     end

Halt processing and return the error status provided.

[Source]

     # File lib/sinatra/base.rb, line 105
105:     def error(code, body=nil)
106:       code, body    = 500, code.to_str if code.respond_to? :to_str
107:       response.body = body unless body.nil?
108:       halt code
109:     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.

[Source]

     # File lib/sinatra/base.rb, line 266
266:     def etag(value, kind=:strong)
267:       raise TypeError, ":strong or :weak expected" if ![:strong,:weak].include?(kind)
268:       value = '"%s"' % value
269:       value = 'W/' + value if kind == :weak
270:       response['ETag'] = value
271: 
272:       # Conditional GET check
273:       if etags = env['HTTP_IF_NONE_MATCH']
274:         etags = etags.split(/\s*,\s*/)
275:         halt 304 if etags.include?(value) || etags.include?('*')
276:       end
277:     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

[Source]

     # File lib/sinatra/base.rb, line 224
224:     def expires(amount, *values)
225:       values << {} unless values.last.kind_of?(Hash)
226: 
227:       if amount.respond_to?(:to_time)
228:         max_age = amount.to_time - Time.now
229:         time = amount.to_time
230:       else
231:         max_age = amount
232:         time = Time.now + amount
233:       end
234: 
235:       values.last.merge!(:max_age => max_age)
236:       cache_control(*values)
237: 
238:       response['Expires'] = time.httpdate
239:     end

Set multiple response headers with Hash.

[Source]

     # File lib/sinatra/base.rb, line 117
117:     def headers(hash=nil)
118:       response.headers.merge! hash if hash
119:       response.headers
120:     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.

[Source]

     # File lib/sinatra/base.rb, line 248
248:     def last_modified(time)
249:       return unless time
250:       time = time.to_time if time.respond_to?(:to_time)
251:       time = time.httpdate if time.respond_to?(:httpdate)
252:       response['Last-Modified'] = time
253:       halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE']
254:       time
255:     end

Look up a media type by file extension in Rack‘s mime registry.

[Source]

     # File lib/sinatra/base.rb, line 128
128:     def mime_type(type)
129:       Base.mime_type(type)
130:     end

Halt processing and return a 404 Not Found.

[Source]

     # File lib/sinatra/base.rb, line 112
112:     def not_found(body=nil)
113:       error 404, body
114:     end

Halt processing and redirect to the URI provided.

[Source]

     # File lib/sinatra/base.rb, line 98
 98:     def  redirectredirect(uri, *args)
 99:       status 302
100:       response['Location'] = uri
101:       halt(*args)
102:     end

Use the contents of the file at path as the response body.

[Source]

     # File lib/sinatra/base.rb, line 156
156:     def send_file(path, opts={})
157:       stat = File.stat(path)
158:       last_modified stat.mtime
159: 
160:       content_type mime_type(opts[:type]) ||
161:         mime_type(File.extname(path)) ||
162:         response['Content-Type'] ||
163:         'application/octet-stream'
164: 
165:       response['Content-Length'] ||= (opts[:length] || stat.size).to_s
166: 
167:       if opts[:disposition] == 'attachment' || opts[:filename]
168:         attachment opts[:filename] || path
169:       elsif opts[:disposition] == 'inline'
170:         response['Content-Disposition'] = 'inline'
171:       end
172: 
173:       halt StaticFile.open(path, 'rb')
174:     rescue Errno::ENOENT
175:       not_found
176:     end

Access the underlying Rack session.

[Source]

     # File lib/sinatra/base.rb, line 123
123:     def session
124:       env['rack.session'] ||= {}
125:     end

Set or retrieve the response status code.

[Source]

    # File lib/sinatra/base.rb, line 81
81:     def status(value=nil)
82:       response.status = value if value
83:       response.status
84:     end

[Validate]