Module Sinatra::Helpers
In: lib/sinatra/base.rb
Rack::ShowExceptions ShowExceptions Rack::Response Response Base Application\n[lib/sinatra/base.rb\nlib/sinatra/main.rb] Rack::Utils NameError NotFound Rack::Request Request ::File StaticFile lib/sinatra/base.rb lib/sinatra/showexceptions.rb Templates Delegator lib/sinatra/base.rb Helpers Sinatra dot/m_4_0.png

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 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

Sugar for redirect (example: redirect back)

[Source]

     # File lib/sinatra/base.rb, line 273
273:     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 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

[Source]

     # 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.

[Source]

     # 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

Halt processing and return the error status provided.

[Source]

     # File lib/sinatra/base.rb, line 99
 99:     def error(code, body=nil)
100:       code, body    = 500, code.to_str if code.respond_to? :to_str
101:       response.body = body unless body.nil?
102:       halt code
103:     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 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

[Source]

     # 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 multiple response headers with Hash.

[Source]

     # File lib/sinatra/base.rb, line 111
111:     def headers(hash=nil)
112:       response.headers.merge! hash if hash
113:       response.headers
114:     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 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.

[Source]

     # 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.

[Source]

     # File lib/sinatra/base.rb, line 106
106:     def not_found(body=nil)
107:       error 404, body
108:     end

Halt processing and redirect to the URI provided.

[Source]

    # File lib/sinatra/base.rb, line 92
92:     def  redirectredirect(uri, *args)
93:       status 302
94:       response['Location'] = uri
95:       halt(*args)
96:     end

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

[Source]

     # 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

Access the underlying Rack session.

[Source]

     # File lib/sinatra/base.rb, line 117
117:     def session
118:       env['rack.session'] ||= {}
119:     end

Set or retrieve the response status code.

[Source]

    # File lib/sinatra/base.rb, line 75
75:     def status(value=nil)
76:       response.status = value if value
77:       response.status
78:     end

[Validate]