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

Sugar for redirect (example: redirect back)

[Source]

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

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

[Source]

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

[Source]

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

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

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]