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

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

Methods

attachment   body   content_type   error   headers   logger   mime_type   not_found   redirect   send_file   session   status   to   uri   url  

Classes and Modules

Class Sinatra::Helpers::Stream

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 204
204:     def attachment(filename=nil)
205:       response['Content-Disposition'] = 'attachment'
206:       if filename
207:         params = '; filename="%s"' % File.basename(filename)
208:         response['Content-Disposition'] << params
209:         ext = File.extname(filename)
210:         content_type(ext) unless response['Content-Type'] or ext.empty?
211:       end
212:     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 104
104:     def body(value=nil, &block)
105:       if block_given?
106:         def block.each; yield(call) end
107:         response.body = block
108:       elsif value
109:         response.body = value
110:       else
111:         response.body
112:       end
113:     end

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

[Source]

     # File lib/sinatra/base.rb, line 185
185:     def content_type(type = nil, params={})
186:       return response['Content-Type'] unless type
187:       default = params.delete :default
188:       mime_type = mime_type(type) || default
189:       fail "Unknown media type: %p" % type if mime_type.nil?
190:       mime_type = mime_type.dup
191:       unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type }
192:         params[:charset] = params.delete('charset') || settings.default_encoding
193:       end
194:       params.delete :charset if mime_type.include? 'charset'
195:       unless params.empty?
196:         mime_type << (mime_type.include?(';') ? ', ' : ';')
197:         mime_type << params.map { |kv| kv.join('=') }.join(', ')
198:       end
199:       response['Content-Type'] = mime_type
200:     end

Halt processing and return the error status provided.

[Source]

     # File lib/sinatra/base.rb, line 151
151:     def error(code, body=nil)
152:       code, body    = 500, code.to_str if code.respond_to? :to_str
153:       response.body = body unless body.nil?
154:       halt code
155:     end

Set multiple response headers with Hash.

[Source]

     # File lib/sinatra/base.rb, line 163
163:     def headers(hash=nil)
164:       response.headers.merge! hash if hash
165:       response.headers
166:     end

Access shared logger object.

[Source]

     # File lib/sinatra/base.rb, line 174
174:     def logger
175:       request.logger
176:     end

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

[Source]

     # File lib/sinatra/base.rb, line 179
179:     def mime_type(type)
180:       Base.mime_type(type)
181:     end

Halt processing and return a 404 Not Found.

[Source]

     # File lib/sinatra/base.rb, line 158
158:     def not_found(body=nil)
159:       error 404, body
160:     end

Halt processing and redirect to the URI provided.

[Source]

     # File lib/sinatra/base.rb, line 116
116:     def  redirectredirect(uri, *args)
117:       if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET'
118:         status 303
119:       else
120:         status 302
121:       end
122: 
123:       # According to RFC 2616 section 14.30, "the field value consists of a
124:       # single absolute URI"
125:       response['Location'] = uri(uri, settings.absolute_redirects?, settings.prefixed_redirects?)
126:       halt(*args)
127:     end

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

[Source]

     # File lib/sinatra/base.rb, line 215
215:     def send_file(path, opts={})
216:       if opts[:type] or not response['Content-Type']
217:         content_type opts[:type] || File.extname(path), :default => 'application/octet-stream'
218:       end
219: 
220:       if opts[:disposition] == 'attachment' || opts[:filename]
221:         attachment opts[:filename] || path
222:       elsif opts[:disposition] == 'inline'
223:         response['Content-Disposition'] = 'inline'
224:       end
225: 
226:       last_modified opts[:last_modified] if opts[:last_modified]
227: 
228:       file      = Rack::File.new nil
229:       file.path = path
230:       result    = file.serving env
231:       result[1].each { |k,v| headers[k] ||= v }
232:       halt result[0], result[2]
233:     rescue Errno::ENOENT
234:       not_found
235:     end

Access the underlying Rack session.

[Source]

     # File lib/sinatra/base.rb, line 169
169:     def session
170:       request.session
171:     end

Set or retrieve the response status code.

[Source]

     # File lib/sinatra/base.rb, line 97
 97:     def status(value=nil)
 98:       response.status = value if value
 99:       response.status
100:     end
to(addr = nil, absolute = true, add_script_name = true)

Alias for uri

Generates the absolute URI for a given path in the app. Takes Rack routers and reverse proxies into account.

[Source]

     # File lib/sinatra/base.rb, line 131
131:     def uri(addr = nil, absolute = true, add_script_name = true)
132:       return addr if addr =~ /\A[A-z][A-z0-9\+\.\-]*:/
133:       uri = [host = ""]
134:       if absolute
135:         host << "http#{'s' if request.secure?}://"
136:         if request.forwarded? or request.port != (request.secure? ? 443 : 80)
137:           host << request.host_with_port
138:         else
139:           host << request.host
140:         end
141:       end
142:       uri << request.script_name.to_s if add_script_name
143:       uri << (addr ? addr : request.path_info).to_s
144:       File.join uri
145:     end
url(addr = nil, absolute = true, add_script_name = true)

Alias for uri

[Validate]