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 369 def attachment(filename = nil, disposition = :attachment) 370 response['Content-Disposition'] = disposition.to_s.dup 371 return unless filename 372 373 params = format('; filename="%s"', File.basename(filename).gsub(/["\r\n]/, MULTIPART_FORM_DATA_REPLACEMENT_TABLE)) 374 response['Content-Disposition'] << params 375 ext = File.extname(filename) 376 content_type(ext) unless response['Content-Type'] || ext.empty? 377 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 254 def body(value = nil, &block) 255 if block_given? 256 def block.each; yield(call) end 257 response.body = block 258 elsif value 259 # Rack 2.0 returns a Rack::File::Iterator here instead of 260 # Rack::File as it was in the previous API. 261 unless request.head? || value.is_a?(Rack::File::Iterator) || value.is_a?(Stream) 262 headers.delete 'Content-Length' 263 end 264 response.body = value 265 else 266 response.body 267 end 268 end
Set the Content-Type of the response body given a media type or file extension.
# File lib/sinatra/base.rb 340 def content_type(type = nil, params = {}) 341 return response['Content-Type'] unless type 342 default = params.delete :default 343 mime_type = mime_type(type) || default 344 fail "Unknown media type: %p" % type if mime_type.nil? 345 mime_type = mime_type.dup 346 unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type } 347 params[:charset] = params.delete('charset') || settings.default_encoding 348 end 349 params.delete :charset if mime_type.include? 'charset' 350 unless params.empty? 351 mime_type << (mime_type.include?(';') ? ', ' : ';') 352 mime_type << params.map do |key, val| 353 val = val.inspect if val =~ /[";,]/ 354 "#{key}=#{val}" 355 end.join(', ') 356 end 357 response['Content-Type'] = mime_type 358 end
Halt processing and return the error status provided.
# File lib/sinatra/base.rb 306 def error(code, body = nil) 307 code, body = 500, code.to_str if code.respond_to? :to_str 308 response.body = body unless body.nil? 309 halt code 310 end
Set multiple response headers with Hash.
# File lib/sinatra/base.rb 318 def headers(hash = nil) 319 response.headers.merge! hash if hash 320 response.headers 321 end
Access shared logger object.
# File lib/sinatra/base.rb 329 def logger 330 request.logger 331 end
Look up a media type by file extension in Rack's mime registry.
# File lib/sinatra/base.rb 334 def mime_type(type) 335 Base.mime_type(type) 336 end
Halt processing and return a 404 Not Found.
# File lib/sinatra/base.rb 313 def not_found(body = nil) 314 error 404, body 315 end
Halt processing and redirect to the URI provided.
# File lib/sinatra/base.rb 271 def redirect(uri, *args) 272 if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET' 273 status 303 274 else 275 status 302 276 end 277 278 # According to RFC 2616 section 14.30, "the field value consists of a 279 # single absolute URI" 280 response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?) 281 halt(*args) 282 end
Use the contents of the file at path
as the response body.
# File lib/sinatra/base.rb 380 def send_file(path, opts = {}) 381 if opts[:type] or not response['Content-Type'] 382 content_type opts[:type] || File.extname(path), :default => 'application/octet-stream' 383 end 384 385 disposition = opts[:disposition] 386 filename = opts[:filename] 387 disposition = :attachment if disposition.nil? and filename 388 filename = path if filename.nil? 389 attachment(filename, disposition) if disposition 390 391 last_modified opts[:last_modified] if opts[:last_modified] 392 393 file = Rack::File.new(File.dirname(settings.app_file)) 394 result = file.serving(request, path) 395 396 result[1].each { |k,v| headers[k] ||= v } 397 headers['Content-Length'] = result[1]['Content-Length'] 398 opts[:status] &&= Integer(opts[:status]) 399 halt (opts[:status] || result[0]), result[2] 400 rescue Errno::ENOENT 401 not_found 402 end
Access the underlying Rack
session.
# File lib/sinatra/base.rb 324 def session 325 request.session 326 end
Set or retrieve the response status code.
# File lib/sinatra/base.rb 247 def status(value = nil) 248 response.status = Rack::Utils.status_code(value) if value 249 response.status 250 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 286 def uri(addr = nil, absolute = true, add_script_name = true) 287 return addr if addr =~ /\A[a-z][a-z0-9\+\.\-]*:/i 288 uri = [host = String.new] 289 if absolute 290 host << "http#{'s' if request.secure?}://" 291 if request.forwarded? or request.port != (request.secure? ? 443 : 80) 292 host << request.host_with_port 293 else 294 host << request.host 295 end 296 end 297 uri << request.script_name.to_s if add_script_name 298 uri << (addr ? addr : request.path_info).to_s 299 File.join uri 300 end