module Sinatra::Helpers
Methods available to routes, before/after filters, and views.
Public Instance Methods
Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save.
# File lib/sinatra/base.rb 389 def attachment(filename = nil, disposition = :attachment) 390 response['Content-Disposition'] = disposition.to_s.dup 391 if filename 392 params = '; filename="%s"' % File.basename(filename) 393 response['Content-Disposition'] << params 394 ext = File.extname(filename) 395 content_type(ext) unless response['Content-Type'] or ext.empty? 396 end 397 end
Set or retrieve the response body. When a block is given, evaluation is deferred until the body is read with each.
# File lib/sinatra/base.rb 281 def body(value = nil, &block) 282 if block_given? 283 def block.each; yield(call) end 284 response.body = block 285 elsif value 286 # Rack 2.0 returns a Rack::File::Iterator here instead of 287 # Rack::File as it was in the previous API. 288 unless request.head? || value.is_a?(Rack::File::Iterator) || value.is_a?(Stream) 289 headers.delete 'Content-Length' 290 end 291 response.body = value 292 else 293 response.body 294 end 295 end
Set the Content-Type of the response body given a media type or file extension.
# File lib/sinatra/base.rb 367 def content_type(type = nil, params = {}) 368 return response['Content-Type'] unless type 369 default = params.delete :default 370 mime_type = mime_type(type) || default 371 fail "Unknown media type: %p" % type if mime_type.nil? 372 mime_type = mime_type.dup 373 unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type } 374 params[:charset] = params.delete('charset') || settings.default_encoding 375 end 376 params.delete :charset if mime_type.include? 'charset' 377 unless params.empty? 378 mime_type << (mime_type.include?(';') ? ', ' : ';') 379 mime_type << params.map do |key, val| 380 val = val.inspect if val =~ /[";,]/ 381 "#{key}=#{val}" 382 end.join(', ') 383 end 384 response['Content-Type'] = mime_type 385 end
Halt processing and return the error status provided.
# File lib/sinatra/base.rb 333 def error(code, body = nil) 334 code, body = 500, code.to_str if code.respond_to? :to_str 335 response.body = body unless body.nil? 336 halt code 337 end
Set multiple response headers with Hash.
# File lib/sinatra/base.rb 345 def headers(hash = nil) 346 response.headers.merge! hash if hash 347 response.headers 348 end
Access shared logger object.
# File lib/sinatra/base.rb 356 def logger 357 request.logger 358 end
Look up a media type by file extension in Rack’s mime registry.
# File lib/sinatra/base.rb 361 def mime_type(type) 362 Base.mime_type(type) 363 end
Halt processing and return a 404 Not Found.
# File lib/sinatra/base.rb 340 def not_found(body = nil) 341 error 404, body 342 end
Halt processing and redirect to the URI provided.
# File lib/sinatra/base.rb 298 def redirect(uri, *args) 299 if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET' 300 status 303 301 else 302 status 302 303 end 304 305 # According to RFC 2616 section 14.30, "the field value consists of a 306 # single absolute URI" 307 response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?) 308 halt(*args) 309 end
Use the contents of the file at path
as the response body.
# File lib/sinatra/base.rb 400 def send_file(path, opts = {}) 401 if opts[:type] or not response['Content-Type'] 402 content_type opts[:type] || File.extname(path), :default => 'application/octet-stream' 403 end 404 405 disposition = opts[:disposition] 406 filename = opts[:filename] 407 disposition = :attachment if disposition.nil? and filename 408 filename = path if filename.nil? 409 attachment(filename, disposition) if disposition 410 411 last_modified opts[:last_modified] if opts[:last_modified] 412 413 file = Rack::File.new(File.dirname(settings.app_file)) 414 result = file.serving(request, path) 415 416 result[1].each { |k,v| headers[k] ||= v } 417 headers['Content-Length'] = result[1]['Content-Length'] 418 opts[:status] &&= Integer(opts[:status]) 419 halt (opts[:status] || result[0]), result[2] 420 rescue Errno::ENOENT 421 not_found 422 end
Access the underlying Rack
session.
# File lib/sinatra/base.rb 351 def session 352 request.session 353 end
Set or retrieve the response status code.
# File lib/sinatra/base.rb 274 def status(value = nil) 275 response.status = Rack::Utils.status_code(value) if value 276 response.status 277 end
Generates the absolute URI for a given path in the app. Takes Rack
routers and reverse proxies into account.
# File lib/sinatra/base.rb 313 def uri(addr = nil, absolute = true, add_script_name = true) 314 return addr if addr =~ /\A[a-z][a-z0-9\+\.\-]*:/i 315 uri = [host = String.new] 316 if absolute 317 host << "http#{'s' if request.secure?}://" 318 if request.forwarded? or request.port != (request.secure? ? 443 : 80) 319 host << request.host_with_port 320 else 321 host << request.host 322 end 323 end 324 uri << request.script_name.to_s if add_script_name 325 uri << (addr ? addr : request.path_info).to_s 326 File.join uri 327 end