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 408 def attachment(filename = nil, disposition = :attachment) 409 response['Content-Disposition'] = disposition.to_s.dup 410 return unless filename 411 412 params = format('; filename="%s"', File.basename(filename).gsub(/["\r\n]/, MULTIPART_FORM_DATA_REPLACEMENT_TABLE)) 413 response['Content-Disposition'] << params 414 ext = File.extname(filename) 415 content_type(ext) unless response['Content-Type'] || ext.empty? 416 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 287 def body(value = nil, &block) 288 if block_given? 289 def block.each; yield(call) end 290 response.body = block 291 elsif value 292 # Rack 2.0 returns a Rack::File::Iterator here instead of 293 # Rack::File as it was in the previous API. 294 unless request.head? || value.is_a?(Rack::Files::Iterator) || value.is_a?(Stream) 295 headers.delete 'Content-Length' 296 end 297 response.body = value 298 else 299 response.body 300 end 301 end
Set the Content-Type of the response body given a media type or file extension.
# File lib/sinatra/base.rb 377 def content_type(type = nil, params = {}) 378 return response['Content-Type'] unless type 379 380 default = params.delete :default 381 mime_type = mime_type(type) || default 382 raise format('Unknown media type: %p', type) if mime_type.nil? 383 384 mime_type = mime_type.dup 385 unless params.include?(:charset) || settings.add_charset.all? { |p| !(p === mime_type) } 386 params[:charset] = params.delete('charset') || settings.default_encoding 387 end 388 params.delete :charset if mime_type.include? 'charset' 389 unless params.empty? 390 mime_type << (mime_type.include?(';') ? ', ' : ';') 391 mime_type << params.map do |key, val| 392 val = val.inspect if val =~ /[";,]/ 393 "#{key}=#{val}" 394 end.join(', ') 395 end 396 response['Content-Type'] = mime_type 397 end
Halt processing and return the error status provided.
# File lib/sinatra/base.rb 340 def error(code, body = nil) 341 if code.respond_to? :to_str 342 body = code.to_str 343 code = 500 344 end 345 response.body = body unless body.nil? 346 halt code 347 end
Set multiple response headers with Hash.
# File lib/sinatra/base.rb 355 def headers(hash = nil) 356 response.headers.merge! hash if hash 357 response.headers 358 end
Access shared logger object.
# File lib/sinatra/base.rb 366 def logger 367 request.logger 368 end
Look up a media type by file extension in Rack’s mime registry.
# File lib/sinatra/base.rb 371 def mime_type(type) 372 Base.mime_type(type) 373 end
Halt processing and return a 404 Not Found.
# File lib/sinatra/base.rb 350 def not_found(body = nil) 351 error 404, body 352 end
Halt processing and redirect to the URI provided.
# File lib/sinatra/base.rb 304 def redirect(uri, *args) 305 if (env['HTTP_VERSION'] == 'HTTP/1.1') && (env['REQUEST_METHOD'] != 'GET') 306 status 303 307 else 308 status 302 309 end 310 311 # According to RFC 2616 section 14.30, "the field value consists of a 312 # single absolute URI" 313 response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?) 314 halt(*args) 315 end
Use the contents of the file at path
as the response body.
# File lib/sinatra/base.rb 419 def send_file(path, opts = {}) 420 if opts[:type] || !response['Content-Type'] 421 content_type opts[:type] || File.extname(path), default: 'application/octet-stream' 422 end 423 424 disposition = opts[:disposition] 425 filename = opts[:filename] 426 disposition = :attachment if disposition.nil? && filename 427 filename = path if filename.nil? 428 attachment(filename, disposition) if disposition 429 430 last_modified opts[:last_modified] if opts[:last_modified] 431 432 file = Rack::Files.new(File.dirname(settings.app_file)) 433 result = file.serving(request, path) 434 435 result[1].each { |k, v| headers[k] ||= v } 436 headers['Content-Length'] = result[1]['Content-Length'] 437 opts[:status] &&= Integer(opts[:status]) 438 halt (opts[:status] || result[0]), result[2] 439 rescue Errno::ENOENT 440 not_found 441 end
Access the underlying Rack
session.
# File lib/sinatra/base.rb 361 def session 362 request.session 363 end
Set or retrieve the response status code.
# File lib/sinatra/base.rb 280 def status(value = nil) 281 response.status = Rack::Utils.status_code(value) if value 282 response.status 283 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 319 def uri(addr = nil, absolute = true, add_script_name = true) 320 return addr if addr.to_s =~ /\A[a-z][a-z0-9+.\-]*:/i 321 322 uri = [host = String.new] 323 if absolute 324 host << "http#{'s' if request.secure?}://" 325 host << if request.forwarded? || (request.port != (request.secure? ? 443 : 80)) 326 request.host_with_port 327 else 328 request.host 329 end 330 end 331 uri << request.script_name.to_s if add_script_name 332 uri << (addr || request.path_info).to_s 333 File.join uri 334 end