module Sinatra::Helpers
Methods available to routes, before/after filters, and views.
Constants
- MULTIPART_FORM_DATA_REPLACEMENT_TABLE
Public Instance Methods
Source
# File lib/sinatra/base.rb 417 def attachment(filename = nil, disposition = :attachment) 418 response['Content-Disposition'] = disposition.to_s.dup 419 return unless filename 420 421 params = format('; filename="%s"', File.basename(filename).gsub(/["\r\n]/, MULTIPART_FORM_DATA_REPLACEMENT_TABLE)) 422 response['Content-Disposition'] << params 423 ext = File.extname(filename) 424 content_type(ext) unless response['content-type'] || ext.empty? 425 end
Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save.
Source
# File lib/sinatra/base.rb 295 def body(value = nil, &block) 296 if block_given? 297 def block.each; yield(call) end 298 response.body = block 299 elsif value 300 unless request.head? || value.is_a?(Rack::Files::BaseIterator) || value.is_a?(Stream) 301 headers.delete 'content-length' 302 end 303 response.body = value 304 else 305 response.body 306 end 307 end
Set or retrieve the response body. When a block is given, evaluation is deferred until the body is read with each.
Source
# File lib/sinatra/base.rb 386 def content_type(type = nil, params = {}) 387 return response['content-type'] unless type 388 389 default = params.delete :default 390 mime_type = mime_type(type) || default 391 raise format('Unknown media type: %p', type) if mime_type.nil? 392 393 mime_type = mime_type.dup 394 unless params.include?(:charset) || settings.add_charset.all? { |p| !(p === mime_type) } 395 params[:charset] = params.delete('charset') || settings.default_encoding 396 end 397 params.delete :charset if mime_type.include? 'charset' 398 unless params.empty? 399 mime_type << (mime_type.include?(';') ? ', ' : ';') 400 mime_type << params.map do |key, val| 401 val = val.inspect if val =~ /[";,]/ 402 "#{key}=#{val}" 403 end.join(', ') 404 end 405 response['content-type'] = mime_type 406 end
Set the content-type of the response body given a media type or file extension.
Source
# File lib/sinatra/base.rb 349 def error(code, body = nil) 350 if code.respond_to? :to_str 351 body = code.to_str 352 code = 500 353 end 354 response.body = body unless body.nil? 355 halt code 356 end
Halt processing and return the error status provided.
Source
# File lib/sinatra/base.rb 364 def headers(hash = nil) 365 response.headers.merge! hash if hash 366 response.headers 367 end
Set multiple response headers with Hash.
Source
# File lib/sinatra/base.rb 375 def logger 376 request.logger 377 end
Access shared logger object.
Source
# File lib/sinatra/base.rb 380 def mime_type(type) 381 Base.mime_type(type) 382 end
Look up a media type by file extension in Rack’s mime registry.
Source
# File lib/sinatra/base.rb 359 def not_found(body = nil) 360 error 404, body 361 end
Halt processing and return a 404 Not Found.
Source
# File lib/sinatra/base.rb 310 def redirect(uri, *args) 311 # SERVER_PROTOCOL is required in Rack 3, fall back to HTTP_VERSION 312 # for servers not updated for Rack 3 (like Puma 5) 313 http_version = env['SERVER_PROTOCOL'] || env['HTTP_VERSION'] 314 if (http_version == 'HTTP/1.1') && (env['REQUEST_METHOD'] != 'GET') 315 status 303 316 else 317 status 302 318 end 319 320 # According to RFC 2616 section 14.30, "the field value consists of a 321 # single absolute URI" 322 response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?) 323 halt(*args) 324 end
Halt processing and redirect to the URI provided.
Source
# File lib/sinatra/base.rb 428 def send_file(path, opts = {}) 429 if opts[:type] || !response['content-type'] 430 content_type opts[:type] || File.extname(path), default: 'application/octet-stream' 431 end 432 433 disposition = opts[:disposition] 434 filename = opts[:filename] 435 disposition = :attachment if disposition.nil? && filename 436 filename = path if filename.nil? 437 attachment(filename, disposition) if disposition 438 439 last_modified opts[:last_modified] if opts[:last_modified] 440 441 file = Rack::Files.new(File.dirname(settings.app_file)) 442 result = file.serving(request, path) 443 444 result[1].each { |k, v| headers[k] ||= v } 445 headers['content-length'] = result[1]['content-length'] 446 opts[:status] &&= Integer(opts[:status]) 447 halt (opts[:status] || result[0]), result[2] 448 rescue Errno::ENOENT 449 not_found 450 end
Use the contents of the file at path
as the response body.
Source
# File lib/sinatra/base.rb 370 def session 371 request.session 372 end
Access the underlying Rack
session.
Source
# File lib/sinatra/base.rb 288 def status(value = nil) 289 response.status = Rack::Utils.status_code(value) if value 290 response.status 291 end
Set or retrieve the response status code.
Source
# File lib/sinatra/base.rb 328 def uri(addr = nil, absolute = true, add_script_name = true) 329 return addr if addr.to_s =~ /\A[a-z][a-z0-9+.\-]*:/i 330 331 uri = [host = String.new] 332 if absolute 333 host << "http#{'s' if request.secure?}://" 334 host << if request.forwarded? || (request.port != (request.secure? ? 443 : 80)) 335 request.host_with_port 336 else 337 request.host 338 end 339 end 340 uri << request.script_name.to_s if add_script_name 341 uri << (addr || request.path_info).to_s 342 File.join uri 343 end
Generates the absolute URI for a given path in the app. Takes Rack
routers and reverse proxies into account.