module Sinatra::Helpers
Methods available to routes, before/after filters, and views.
Constants
- MULTIPART_FORM_DATA_REPLACEMENT_TABLE
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 393 def attachment(filename = nil, disposition = :attachment) 394 response['Content-Disposition'] = disposition.to_s.dup 395 return unless filename 396 397 params = format('; filename="%s"', File.basename(filename).gsub(/["\r\n]/, MULTIPART_FORM_DATA_REPLACEMENT_TABLE)) 398 response['Content-Disposition'] << params 399 ext = File.extname(filename) 400 content_type(ext) unless response['Content-Type'] || ext.empty? 401 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 278 def body(value = nil, &block) 279 if block_given? 280 def block.each; yield(call) end 281 response.body = block 282 elsif value 283 # Rack 2.0 returns a Rack::File::Iterator here instead of 284 # Rack::File as it was in the previous API. 285 unless request.head? || value.is_a?(Rack::File::Iterator) || value.is_a?(Stream) 286 headers.delete 'Content-Length' 287 end 288 response.body = value 289 else 290 response.body 291 end 292 end
Set the Content-Type of the response body given a media type or file extension.
# File lib/sinatra/base.rb 364 def content_type(type = nil, params = {}) 365 return response['Content-Type'] unless type 366 default = params.delete :default 367 mime_type = mime_type(type) || default 368 fail "Unknown media type: %p" % type if mime_type.nil? 369 mime_type = mime_type.dup 370 unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type } 371 params[:charset] = params.delete('charset') || settings.default_encoding 372 end 373 params.delete :charset if mime_type.include? 'charset' 374 unless params.empty? 375 mime_type << (mime_type.include?(';') ? ', ' : ';') 376 mime_type << params.map do |key, val| 377 val = val.inspect if val =~ /[";,]/ 378 "#{key}=#{val}" 379 end.join(', ') 380 end 381 response['Content-Type'] = mime_type 382 end
Halt processing and return the error status provided.
# File lib/sinatra/base.rb 330 def error(code, body = nil) 331 code, body = 500, code.to_str if code.respond_to? :to_str 332 response.body = body unless body.nil? 333 halt code 334 end
Set multiple response headers with Hash.
# File lib/sinatra/base.rb 342 def headers(hash = nil) 343 response.headers.merge! hash if hash 344 response.headers 345 end
Access shared logger object.
# File lib/sinatra/base.rb 353 def logger 354 request.logger 355 end
Look up a media type by file extension in Rack's mime registry.
# File lib/sinatra/base.rb 358 def mime_type(type) 359 Base.mime_type(type) 360 end
Halt processing and return a 404 Not Found.
# File lib/sinatra/base.rb 337 def not_found(body = nil) 338 error 404, body 339 end
Halt processing and redirect to the URI provided.
# File lib/sinatra/base.rb 295 def redirect(uri, *args) 296 if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET' 297 status 303 298 else 299 status 302 300 end 301 302 # According to RFC 2616 section 14.30, "the field value consists of a 303 # single absolute URI" 304 response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?) 305 halt(*args) 306 end
Use the contents of the file at path
as the response body.
# File lib/sinatra/base.rb 404 def send_file(path, opts = {}) 405 if opts[:type] or not response['Content-Type'] 406 content_type opts[:type] || File.extname(path), :default => 'application/octet-stream' 407 end 408 409 disposition = opts[:disposition] 410 filename = opts[:filename] 411 disposition = :attachment if disposition.nil? and filename 412 filename = path if filename.nil? 413 attachment(filename, disposition) if disposition 414 415 last_modified opts[:last_modified] if opts[:last_modified] 416 417 file = Rack::File.new(File.dirname(settings.app_file)) 418 result = file.serving(request, path) 419 420 result[1].each { |k,v| headers[k] ||= v } 421 headers['Content-Length'] = result[1]['Content-Length'] 422 opts[:status] &&= Integer(opts[:status]) 423 halt (opts[:status] || result[0]), result[2] 424 rescue Errno::ENOENT 425 not_found 426 end
Access the underlying Rack
session.
# File lib/sinatra/base.rb 348 def session 349 request.session 350 end
Set or retrieve the response status code.
# File lib/sinatra/base.rb 271 def status(value = nil) 272 response.status = Rack::Utils.status_code(value) if value 273 response.status 274 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 310 def uri(addr = nil, absolute = true, add_script_name = true) 311 return addr if addr =~ /\A[a-z][a-z0-9\+\.\-]*:/i 312 uri = [host = String.new] 313 if absolute 314 host << "http#{'s' if request.secure?}://" 315 if request.forwarded? or request.port != (request.secure? ? 443 : 80) 316 host << request.host_with_port 317 else 318 host << request.host 319 end 320 end 321 uri << request.script_name.to_s if add_script_name 322 uri << (addr ? addr : request.path_info).to_s 323 File.join uri 324 end