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