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