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

Methods available to routes, before 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 139
139:     def attachment(filename=nil)
140:       response['Content-Disposition'] = 'attachment'
141:       if filename
142:         params = '; filename="%s"' % File.basename(filename)
143:         response['Content-Disposition'] << params
144:       end
145:     end

Sugar for redirect (example: redirect back)

[Source]

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

[Source]

     # File lib/sinatra/base.rb, line 126
126:     def content_type(type, params={})
127:       mime_type = self.mime_type(type)
128:       fail "Unknown media type: %p" % type if mime_type.nil?
129:       if params.any?
130:         params = params.collect { |kv| "%s=%s" % kv }.join(', ')
131:         response['Content-Type'] = [mime_type, params].join(";")
132:       else
133:         response['Content-Type'] = mime_type
134:       end
135:     end

Halt processing and return the error status provided.

[Source]

    # File lib/sinatra/base.rb, line 92
92:     def error(code, body=nil)
93:       code, body    = 500, code.to_str if code.respond_to? :to_str
94:       response.body = body unless body.nil?
95:       halt code
96:     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 206
206:     def etag(value, kind=:strong)
207:       raise TypeError, ":strong or :weak expected" if ![:strong,:weak].include?(kind)
208:       value = '"%s"' % value
209:       value = 'W/' + value if kind == :weak
210:       response['ETag'] = value
211: 
212:       # Conditional GET check
213:       if etags = env['HTTP_IF_NONE_MATCH']
214:         etags = etags.split(/\s*,\s*/)
215:         halt 304 if etags.include?(value) || etags.include?('*')
216:       end
217:     end

Set multiple response headers with Hash.

[Source]

     # File lib/sinatra/base.rb, line 104
104:     def headers(hash=nil)
105:       response.headers.merge! hash if hash
106:       response.headers
107:     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 189
189:     def last_modified(time)
190:       time = time.to_time if time.respond_to?(:to_time)
191:       time = time.httpdate if time.respond_to?(:httpdate)
192:       response['Last-Modified'] = time
193:       halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE']
194:       time
195:     end

[Source]

     # File lib/sinatra/base.rb, line 119
119:     def media_type(type)
120:       sinatra_warn "media_type is deprecated; use mime_type instead"
121:       mime_type(type)
122:     end

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

[Source]

     # File lib/sinatra/base.rb, line 115
115:     def mime_type(type)
116:       Base.mime_type(type)
117:     end

Halt processing and return a 404 Not Found.

[Source]

     # File lib/sinatra/base.rb, line 99
 99:     def not_found(body=nil)
100:       error 404, body
101:     end

Halt processing and redirect to the URI provided.

[Source]

    # File lib/sinatra/base.rb, line 85
85:     def  redirectredirect(uri, *args)
86:       status 302
87:       response['Location'] = uri
88:       halt(*args)
89:     end

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

[Source]

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

Access the underlying Rack session.

[Source]

     # File lib/sinatra/base.rb, line 110
110:     def session
111:       env['rack.session'] ||= {}
112:     end

Set or retrieve the response status code.

[Source]

    # File lib/sinatra/base.rb, line 68
68:     def status(value=nil)
69:       response.status = value if value
70:       response.status
71:     end

[Validate]