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 126
126:     def attachment(filename=nil)
127:       response['Content-Disposition'] = 'attachment'
128:       if filename
129:         params = '; filename="%s"' % File.basename(filename)
130:         response['Content-Disposition'] << params
131:       end
132:     end

Sugar for redirect (example: redirect back)

[Source]

     # File lib/sinatra/base.rb, line 207
207:     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 67
67:     def body(value=nil, &block)
68:       if block_given?
69:         def block.each ; yield call ; end
70:         response.body = block
71:       else
72:         response.body = value
73:       end
74:     end

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

[Source]

     # File lib/sinatra/base.rb, line 113
113:     def content_type(type, params={})
114:       media_type = self.media_type(type)
115:       fail "Unknown media type: %p" % type if media_type.nil?
116:       if params.any?
117:         params = params.collect { |kv| "%s=%s" % kv }.join(', ')
118:         response['Content-Type'] = [media_type, params].join(";")
119:       else
120:         response['Content-Type'] = media_type
121:       end
122:     end

Halt processing and return the error status provided.

[Source]

    # File lib/sinatra/base.rb, line 84
84:     def error(code, body=nil)
85:       code, body    = 500, code.to_str if code.respond_to? :to_str
86:       response.body = body unless body.nil?
87:       halt code
88:     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 193
193:     def etag(value, kind=:strong)
194:       raise TypeError, ":strong or :weak expected" if ![:strong,:weak].include?(kind)
195:       value = '"%s"' % value
196:       value = 'W/' + value if kind == :weak
197:       response['ETag'] = value
198: 
199:       # Conditional GET check
200:       if etags = env['HTTP_IF_NONE_MATCH']
201:         etags = etags.split(/\s*,\s*/)
202:         halt 304 if etags.include?(value) || etags.include?('*')
203:       end
204:     end

Set multiple response headers with Hash.

[Source]

    # File lib/sinatra/base.rb, line 96
96:     def headers(hash=nil)
97:       response.headers.merge! hash if hash
98:       response.headers
99:     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 176
176:     def last_modified(time)
177:       time = time.to_time if time.respond_to?(:to_time)
178:       time = time.httpdate if time.respond_to?(:httpdate)
179:       response['Last-Modified'] = time
180:       halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE']
181:       time
182:     end

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

[Source]

     # File lib/sinatra/base.rb, line 107
107:     def media_type(type)
108:       Base.media_type(type)
109:     end

Halt processing and return a 404 Not Found.

[Source]

    # File lib/sinatra/base.rb, line 91
91:     def not_found(body=nil)
92:       error 404, body
93:     end

Halt processing and redirect to the URI provided.

[Source]

    # File lib/sinatra/base.rb, line 77
77:     def  redirectredirect(uri, *args)
78:       status 302
79:       response['Location'] = uri
80:       halt(*args)
81:     end

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

[Source]

     # File lib/sinatra/base.rb, line 135
135:     def send_file(path, opts={})
136:       stat = File.stat(path)
137:       last_modified stat.mtime
138: 
139:       content_type media_type(opts[:type]) ||
140:         media_type(File.extname(path)) ||
141:         response['Content-Type'] ||
142:         'application/octet-stream'
143: 
144:       response['Content-Length'] ||= (opts[:length] || stat.size).to_s
145: 
146:       if opts[:disposition] == 'attachment' || opts[:filename]
147:         attachment opts[:filename] || path
148:       elsif opts[:disposition] == 'inline'
149:         response['Content-Disposition'] = 'inline'
150:       end
151: 
152:       halt StaticFile.open(path, 'rb')
153:     rescue Errno::ENOENT
154:       not_found
155:     end

Access the underlying Rack session.

[Source]

     # File lib/sinatra/base.rb, line 102
102:     def session
103:       env['rack.session'] ||= {}
104:     end

Set or retrieve the response status code.

[Source]

    # File lib/sinatra/base.rb, line 60
60:     def status(value=nil)
61:       response.status = value if value
62:       response.status
63:     end

[Validate]