Class: Sinatra::Base

Inherits:
Object
  • Object
show all
Includes:
Rack::Utils, Helpers, Templates
Defined in:
lib/sinatra/base.rb

Overview

Base class for all Sinatra applications and middleware.

Direct Known Subclasses

Application

Constant Summary collapse

URI_INSTANCE =
URI::Parser.new
CALLERS_TO_IGNORE =

:nodoc:

[ # :nodoc:
  /\/sinatra(\/(base|main|show_exceptions))?\.rb$/,   # all sinatra code
  /lib\/tilt.*\.rb$/,                                 # all tilt code
  /^\(.*\)$/,                                         # generated code
  /rubygems\/(custom|core_ext\/kernel)_require\.rb$/, # rubygems require hacks
  /active_support/,                                   # active_support require hacks
  /bundler(\/(?:runtime|inline))?\.rb/,               # bundler require hacks
  /<internal:/,                                       # internal in ruby >= 1.9.2
  /src\/kernel\/bootstrap\/[A-Z]/                     # maglev kernel files
]

Constants included from Helpers

Helpers::ETAG_KINDS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Templates

#asciidoc, #builder, #coffee, #creole, #erb, #erubis, #find_template, #haml, #less, #liquid, #markaby, #markdown, #mediawiki, #nokogiri, #rabl, #radius, #rdoc, #sass, #scss, #slim, #stylus, #textile, #wlang, #yajl

Methods included from Helpers

#attachment, #back, #bad_request?, #body, #cache_control, #client_error?, #content_type, #error, #etag, #expires, #headers, #informational?, #last_modified, #logger, #mime_type, #not_found, #not_found?, #redirect, #redirect?, #send_file, #server_error?, #session, #status, #stream, #success?, #time_for, #uri

Constructor Details

#initialize(app = nil, **kwargs) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:

  • _self (Sinatra::Base)

    the object that the method was called on


928
929
930
931
932
933
934
# File 'lib/sinatra/base.rb', line 928

def initialize(app = nil, **kwargs)
  super()
  @app = app
  @template_cache = Tilt::Cache.new
  @pinned_response = nil # whether a before! filter pinned the content-type
  yield self if block_given?
end

Class Attribute Details

.errorsObject (readonly)

Returns the value of attribute errors.


1231
1232
1233
# File 'lib/sinatra/base.rb', line 1231

def errors
  @errors
end

.filtersObject (readonly)

Returns the value of attribute filters.


1231
1232
1233
# File 'lib/sinatra/base.rb', line 1231

def filters
  @filters
end

.routesObject (readonly)

Returns the value of attribute routes.


1231
1232
1233
# File 'lib/sinatra/base.rb', line 1231

def routes
  @routes
end

.templatesObject (readonly)

Returns the value of attribute templates.


1231
1232
1233
# File 'lib/sinatra/base.rb', line 1231

def templates
  @templates
end

Instance Attribute Details

#appObject

Returns the value of attribute app.


925
926
927
# File 'lib/sinatra/base.rb', line 925

def app
  @app
end

#envObject

Returns the value of attribute env.


925
926
927
# File 'lib/sinatra/base.rb', line 925

def env
  @env
end

#paramsObject

Returns the value of attribute params.


925
926
927
# File 'lib/sinatra/base.rb', line 925

def params
  @params
end

#requestObject

Returns the value of attribute request.


925
926
927
# File 'lib/sinatra/base.rb', line 925

def request
  @request
end

#responseObject

Returns the value of attribute response.


925
926
927
# File 'lib/sinatra/base.rb', line 925

def response
  @response
end

#template_cacheObject (readonly)

Returns the value of attribute template_cache.


926
927
928
# File 'lib/sinatra/base.rb', line 926

def template_cache
  @template_cache
end

Class Method Details

.add_filter(type, path = /.*/, **options, &block) ⇒ Object

add a filter


1408
1409
1410
# File 'lib/sinatra/base.rb', line 1408

def add_filter(type, path = /.*/, **options, &block)
  filters[type] << compile!(type, path, block, **options)
end

.after(path = /.*/, **options, &block) ⇒ Object

Define an after filter; runs after all requests within the same context as route handlers and may access/modify the request and response.


1403
1404
1405
# File 'lib/sinatra/base.rb', line 1403

def after(path = /.*/, **options, &block)
  add_filter(:after, path, **options, &block)
end

.before(path = /.*/, **options, &block) ⇒ Object

Define a before filter; runs before all requests within the same context as route handlers and may access/modify the request and response.


1396
1397
1398
# File 'lib/sinatra/base.rb', line 1396

def before(path = /.*/, **options, &block)
  add_filter(:before, path, **options, &block)
end

.build(app) ⇒ Object

Creates a Rack::Builder instance with all the middleware set up and the given +app+ as end point.


1543
1544
1545
1546
1547
1548
1549
# File 'lib/sinatra/base.rb', line 1543

def build(app)
  builder = Rack::Builder.new
  setup_default_middleware builder
  setup_middleware builder
  builder.run app
  builder
end

.call(env) ⇒ Object


1551
1552
1553
# File 'lib/sinatra/base.rb', line 1551

def call(env)
  synchronize { prototype.call(env) }
end

.caller_filesObject

Like Kernel#caller but excluding certain magic entries and without line / method information; the resulting array contains filenames only.


1557
1558
1559
# File 'lib/sinatra/base.rb', line 1557

def caller_files
  cleaned_caller(1).flatten
end

.caller_locationsObject

Like caller_files, but containing Arrays rather than strings with the first element being the file, and the second being the line.


1563
1564
1565
# File 'lib/sinatra/base.rb', line 1563

def caller_locations
  cleaned_caller 2
end

.condition(name = "#{caller.first[/`.*'/]} condition", &block) ⇒ Object

Add a route condition. The route is considered non-matching when the block returns false.


1414
1415
1416
# File 'lib/sinatra/base.rb', line 1414

def condition(name = "#{caller.first[/`.*'/]} condition", &block)
  @conditions << generate_method(name, &block)
end

.configure(*envs) {|_self| ... } ⇒ Object

Set configuration options for Sinatra and/or the app. Allows scoping of settings for certain environments.

Yields:

  • (_self)

Yield Parameters:

  • _self (Sinatra::Base)

    the object that the method was called on


1474
1475
1476
# File 'lib/sinatra/base.rb', line 1474

def configure(*envs)
  yield self if envs.empty? || envs.include?(environment.to_sym)
end

.delete(path, opts = {}, &bk) ⇒ Object


1443
# File 'lib/sinatra/base.rb', line 1443

def delete(path, opts = {}, &bk)  route 'DELETE',  path, opts, &bk end

.development?Boolean

Returns:

  • (Boolean)

1468
# File 'lib/sinatra/base.rb', line 1468

def development?; environment == :development end

.disable(*opts) ⇒ Object

Same as calling set :option, false for each of the given options.


1312
1313
1314
# File 'lib/sinatra/base.rb', line 1312

def disable(*opts)
  opts.each { |key| set(key, false) }
end

.enable(*opts) ⇒ Object

Same as calling set :option, true for each of the given options.


1307
1308
1309
# File 'lib/sinatra/base.rb', line 1307

def enable(*opts)
  opts.each { |key| set(key, true) }
end

.error(*codes, &block) ⇒ Object

Define a custom error handler. Optionally takes either an Exception class, or an HTTP status code to specify which errors should be handled.


1319
1320
1321
1322
1323
1324
1325
# File 'lib/sinatra/base.rb', line 1319

def error(*codes, &block)
  args  = compile! "ERROR", /.*/, block
  codes = codes.flat_map(&method(:Array))
  codes << Exception if codes.empty?
  codes << Sinatra::NotFound if codes.include?(404)
  codes.each { |c| (@errors[c] ||= []) << args }
end

.extensionsObject

Extension modules registered on this class and all superclasses.


1252
1253
1254
1255
1256
1257
1258
# File 'lib/sinatra/base.rb', line 1252

def extensions
  if superclass.respond_to?(:extensions)
    (@extensions + superclass.extensions).uniq
  else
    @extensions
  end
end

.get(path, opts = {}, &block) ⇒ Object

Defining a GET handler also automatically defines a HEAD handler.


1433
1434
1435
1436
1437
1438
1439
# File 'lib/sinatra/base.rb', line 1433

def get(path, opts = {}, &block)
  conditions = @conditions.dup
  route('GET', path, opts, &block)

  @conditions = conditions
  route('HEAD', path, opts, &block)
end

.head(path, opts = {}, &bk) ⇒ Object


1444
# File 'lib/sinatra/base.rb', line 1444

def head(path, opts = {}, &bk)    route 'HEAD',    path, opts, &bk end

.helpers(*extensions, &block) ⇒ Object

Makes the methods defined in the block and in the Modules given in extensions available to the handlers and templates


1452
1453
1454
1455
# File 'lib/sinatra/base.rb', line 1452

def helpers(*extensions, &block)
  class_eval(&block)   if block_given?
  include(*extensions) if extensions.any?
end

.inline_templates=(file = nil) ⇒ Object

Load embedded templates from the file; uses the caller's FILE when no file is specified.


1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
# File 'lib/sinatra/base.rb', line 1345

def inline_templates=(file = nil)
  file = (file.nil? || file == true) ? (caller_files.first || File.expand_path($0)) : file

  begin
    io = ::IO.respond_to?(:binread) ? ::IO.binread(file) : ::IO.read(file)
    app, data = io.gsub("\r\n", "\n").split(/^__END__$/, 2)
  rescue Errno::ENOENT
    app, data = nil
  end

  if data
    if app and app =~ /([^\n]*\n)?#[^\n]*coding: *(\S+)/m
      encoding = $2
    else
      encoding = settings.default_encoding
    end
    lines = app.count("\n") + 1
    template = nil
    force_encoding data, encoding
    data.each_line do |line|
      lines += 1
      if line =~ /^@@\s*(.*\S)\s*$/
        template = force_encoding(String.new, encoding)
        templates[$1.to_sym] = [template, file, lines]
      elsif template
        template << line
      end
    end
  end
end

.layout(name = :layout, &block) ⇒ Object

Define the layout template. The block must return the template source.


1339
1340
1341
# File 'lib/sinatra/base.rb', line 1339

def layout(name = :layout, &block)
  template name, &block
end

1447
# File 'lib/sinatra/base.rb', line 1447

def link(path, opts = {}, &bk)    route 'LINK',    path, opts, &bk end

.middlewareObject

Middleware used in this class and all superclasses.


1261
1262
1263
1264
1265
1266
1267
# File 'lib/sinatra/base.rb', line 1261

def middleware
  if superclass.respond_to?(:middleware)
    superclass.middleware + @middleware
  else
    @middleware
  end
end

.mime_type(type, value = nil) ⇒ Object

Lookup or register a mime type in Rack's mime registry.


1377
1378
1379
1380
1381
1382
1383
# File 'lib/sinatra/base.rb', line 1377

def mime_type(type, value = nil)
  return type      if type.nil?
  return type.to_s if type.to_s.include?('/')
  type = ".#{type}" unless type.to_s[0] == ?.
  return Rack::Mime.mime_type(type, nil) unless value
  Rack::Mime::MIME_TYPES[type] = value
end

.mime_types(type) ⇒ Object

provides all mime types matching type, including deprecated types: mime_types :html # => ['text/html'] mime_types :js # => ['application/javascript', 'text/javascript']


1388
1389
1390
1391
# File 'lib/sinatra/base.rb', line 1388

def mime_types(type)
  type = mime_type type
  type =~ /^application\/(xml|javascript)$/ ? [type, "text/#$1"] : [type]
end

.new(*args, **kwargs, &bk) ⇒ Object

Create a new instance of the class fronted by its middleware pipeline. The object is guaranteed to respond to #call but may not be an instance of the class new was called on.


1536
1537
1538
1539
# File 'lib/sinatra/base.rb', line 1536

def new(*args, **kwargs, &bk)
  instance = new!(*args, **kwargs, &bk)
  Wrapper.new(build(instance).to_app, instance)
end

.not_found(&block) ⇒ Object

Sugar for error(404) { ... }


1328
1329
1330
# File 'lib/sinatra/base.rb', line 1328

def not_found(&block)
  error(404, &block)
end

.options(path, opts = {}, &bk) ⇒ Object


1445
# File 'lib/sinatra/base.rb', line 1445

def options(path, opts = {}, &bk) route 'OPTIONS', path, opts, &bk end

.patch(path, opts = {}, &bk) ⇒ Object


1446
# File 'lib/sinatra/base.rb', line 1446

def patch(path, opts = {}, &bk)   route 'PATCH',   path, opts, &bk end

.post(path, opts = {}, &bk) ⇒ Object


1442
# File 'lib/sinatra/base.rb', line 1442

def post(path, opts = {}, &bk)    route 'POST',    path, opts, &bk end

.production?Boolean

Returns:

  • (Boolean)

1469
# File 'lib/sinatra/base.rb', line 1469

def production?;  environment == :production  end

.prototypeObject

The prototype instance used to process requests.


1526
1527
1528
# File 'lib/sinatra/base.rb', line 1526

def prototype
  @prototype ||= new
end

.public=(value) ⇒ Object


1418
1419
1420
1421
# File 'lib/sinatra/base.rb', line 1418

def public=(value)
  warn ":public is no longer used to avoid overloading Module#public, use :public_folder or :public_dir instead"
  set(:public_folder, value)
end

.public_dirObject


1427
1428
1429
# File 'lib/sinatra/base.rb', line 1427

def public_dir
  public_folder
end

.public_dir=(value) ⇒ Object


1423
1424
1425
# File 'lib/sinatra/base.rb', line 1423

def public_dir=(value)
  self.public_folder = value
end

.put(path, opts = {}, &bk) ⇒ Object


1441
# File 'lib/sinatra/base.rb', line 1441

def put(path, opts = {}, &bk)     route 'PUT',     path, opts, &bk end

.quit!Object Also known as: stop!

Stop the self-hosted server if running.


1486
1487
1488
1489
1490
1491
1492
1493
# File 'lib/sinatra/base.rb', line 1486

def quit!
  return unless running?
  # Use Thin's hard #stop! if available, otherwise just #stop.
  running_server.respond_to?(:stop!) ? running_server.stop! : running_server.stop
  $stderr.puts "== Sinatra has ended his set (crowd applauds)" unless suppress_messages?
  set :running_server, nil
  set :handler_name, nil
end

.register(*extensions, &block) ⇒ Object

Register an extension. Alternatively take a block from which an extension will be created and registered on the fly.


1459
1460
1461
1462
1463
1464
1465
1466
# File 'lib/sinatra/base.rb', line 1459

def register(*extensions, &block)
  extensions << Module.new(&block) if block_given?
  @extensions += extensions
  extensions.each do |extension|
    extend extension
    extension.registered(self) if extension.respond_to?(:registered)
  end
end

.reset!Object

Removes all routes, filters, middleware and extension hooks from the current class (not routes/filters/... defined by its superclass).


1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
# File 'lib/sinatra/base.rb', line 1235

def reset!
  @conditions     = []
  @routes         = {}
  @filters        = {:before => [], :after => []}
  @errors         = {}
  @middleware     = []
  @prototype      = nil
  @extensions     = []

  if superclass.respond_to?(:templates)
    @templates = Hash.new { |hash, key| superclass.templates[key] }
  else
    @templates = {}
  end
end

.run!(options = {}, &block) ⇒ Object Also known as: start!

Run the Sinatra app as a self-hosted server using Puma, Mongrel, or WEBrick (in that order). If given a block, will call with the constructed handler once we have taken the stage.


1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
# File 'lib/sinatra/base.rb', line 1500

def run!(options = {}, &block)
  return if running?
  set options
  handler         = Rack::Handler.pick(server)
  handler_name    = handler.name.gsub(/.*::/, '')
  server_settings = settings.respond_to?(:server_settings) ? settings.server_settings : {}
  server_settings.merge!(:Port => port, :Host => bind)

  begin
    start_server(handler, server_settings, handler_name, &block)
  rescue Errno::EADDRINUSE
    $stderr.puts "== Someone is already performing on port #{port}!"
    raise
  ensure
    quit!
  end
end

.running?Boolean

Check whether the self-hosted server is running or not.

Returns:

  • (Boolean)

1521
1522
1523
# File 'lib/sinatra/base.rb', line 1521

def running?
  running_server?
end

.set(option, value = (not_set = true), ignore_setter = false, &block) ⇒ Object

Sets an option to the given value. If the value is a proc, the proc will be called every time the option is accessed.

Raises:

  • (ArgumentError)

1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
# File 'lib/sinatra/base.rb', line 1271

def set(option, value = (not_set = true), ignore_setter = false, &block)
  raise ArgumentError if block and !not_set
  value, not_set = block, false if block

  if not_set
    raise ArgumentError unless option.respond_to?(:each)
    option.each { |k,v| set(k, v) }
    return self
  end

  if respond_to?("#{option}=") and not ignore_setter
    return __send__("#{option}=", value)
  end

  setter = proc { |val| set option, val, true }
  getter = proc { value }

  case value
  when Proc
    getter = value
  when Symbol, Integer, FalseClass, TrueClass, NilClass
    getter = value.inspect
  when Hash
    setter = proc do |val|
      val = value.merge val if Hash === val
      set option, val, true
    end
  end

  define_singleton("#{option}=", setter)
  define_singleton(option, getter)
  define_singleton("#{option}?", "!!#{option}") unless method_defined? "#{option}?"
  self
end

.settingsObject

Access settings defined with Base.set.


964
965
966
# File 'lib/sinatra/base.rb', line 964

def self.settings
  self
end

.template(name, &block) ⇒ Object

Define a named template. The block must return the template source.


1333
1334
1335
1336
# File 'lib/sinatra/base.rb', line 1333

def template(name, &block)
  filename, line = caller_locations.first
  templates[name] = [block, filename, line.to_i]
end

.test?Boolean

Returns:

  • (Boolean)

1470
# File 'lib/sinatra/base.rb', line 1470

def test?;        environment == :test        end

1448
# File 'lib/sinatra/base.rb', line 1448

def unlink(path, opts = {}, &bk)  route 'UNLINK',  path, opts, &bk end

.use(middleware, *args, &block) ⇒ Object

Use the specified Rack middleware


1479
1480
1481
1482
# File 'lib/sinatra/base.rb', line 1479

def use(middleware, *args, &block)
  @prototype = nil
  @middleware << [middleware, args, block]
end

Instance Method Details

#call(env) ⇒ Object

Rack call interface.


937
938
939
# File 'lib/sinatra/base.rb', line 937

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object

:nodoc:


941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# File 'lib/sinatra/base.rb', line 941

def call!(env) # :nodoc:
  @env      = env
  @params   = IndifferentHash.new
  @request  = Request.new(env)
  @response = Response.new
  @pinned_response = nil
  template_cache.clear if settings.reload_templates

  invoke { dispatch! }
  invoke { error_block!(response.status) } unless @env['sinatra.error']

  unless @response['Content-Type']
    if Array === body && body[0].respond_to?(:content_type)
      content_type body[0].content_type
    elsif default = settings.default_content_type
      content_type default
    end
  end

  @response.finish
end

#forwardObject

Forward the request to the downstream app -- middleware only.


994
995
996
997
998
999
1000
1001
# File 'lib/sinatra/base.rb', line 994

def forward
  fail "downstream app not set" unless @app.respond_to? :call
  status, headers, body = @app.call env
  @response.status = status
  @response.body = body
  @response.headers.merge! headers
  nil
end

#halt(*response) ⇒ Object

Exit the current block, halts any further processing of the request, and returns the specified response.


981
982
983
984
# File 'lib/sinatra/base.rb', line 981

def halt(*response)
  response = response.first if response.length == 1
  throw :halt, response
end

#new!Object

Create a new instance without middleware in front of it.


1531
# File 'lib/sinatra/base.rb', line 1531

alias new! new

#optionsObject


973
974
975
976
977
# File 'lib/sinatra/base.rb', line 973

def options
  warn "Sinatra::Base#options is deprecated and will be removed, " \
    "use #settings instead."
  settings
end

#pass(&block) ⇒ Object

Pass control to the next matching route. If there are no more matching routes, Sinatra will return a 404 response.


989
990
991
# File 'lib/sinatra/base.rb', line 989

def pass(&block)
  throw :pass, block
end

#settingsObject

Access settings defined with Base.set.


969
970
971
# File 'lib/sinatra/base.rb', line 969

def settings
  self.class.settings
end