Class: Sinatra::Base
- Includes:
- Rack::Utils, Helpers, Templates
- Defined in:
- lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb
Overview
Base class for all Sinatra applications and middleware.
Direct Known Subclasses
Constant Summary collapse
- URI_INSTANCE =
URI.const_defined?(:Parser) ? URI::Parser.new : URI
- CALLERS_TO_IGNORE =
:nodoc:
[ # :nodoc: /\/sinatra(\/(base|main|showexceptions))?\.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)?\.rb/, # bundler require hacks /<internal:/, # internal in ruby >= 1.9.2 /src\/kernel\/bootstrap\/[A-Z]/ # maglev kernel files ]
Constants included from Helpers
Constants included from Rack::Utils
Rack::Utils::DEFAULT_SEP, Rack::Utils::ESCAPE_HTML, Rack::Utils::ESCAPE_HTML_PATTERN, Rack::Utils::HTTP_STATUS_CODES, Rack::Utils::Multipart, Rack::Utils::STATUS_WITH_NO_ENTITY_BODY, Rack::Utils::SYMBOL_TO_STATUS_CODE
Class Attribute Summary collapse
-
.errors ⇒ Object
readonly
Returns the value of attribute errors.
-
.filters ⇒ Object
readonly
Returns the value of attribute filters.
-
.routes ⇒ Object
readonly
Returns the value of attribute routes.
-
.templates ⇒ Object
readonly
Returns the value of attribute templates.
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#env ⇒ Object
Returns the value of attribute env.
-
#params ⇒ Object
Returns the value of attribute params.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
-
#template_cache ⇒ Object
readonly
Returns the value of attribute template_cache.
Class Method Summary collapse
-
.add_filter(type, path = nil, options = {}, &block) ⇒ Object
add a filter.
-
.after(path = nil, 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.
-
.before(path = nil, 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.
-
.build(app) ⇒ Object
Creates a Rack::Builder instance with all the middleware set up and the given
app
as end point. - .call(env) ⇒ Object
-
.caller_files ⇒ Object
Like Kernel#caller but excluding certain magic entries and without line / method information; the resulting array contains filenames only.
-
.caller_locations ⇒ Object
Like caller_files, but containing Arrays rather than strings with the first element being the file, and the second being the line.
-
.condition(name = "#{caller.first[/`.*'/]} condition", &block) ⇒ Object
Add a route condition.
-
.configure(*envs) {|_self| ... } ⇒ Object
Set configuration options for Sinatra and/or the app.
- .delete(path, opts = {}, &bk) ⇒ Object
- .development? ⇒ Boolean
-
.disable(*opts) ⇒ Object
Same as calling ‘set :option, false` for each of the given options.
-
.enable(*opts) ⇒ Object
Same as calling ‘set :option, true` for each of the given options.
-
.error(*codes, &block) ⇒ Object
Define a custom error handler.
-
.extensions ⇒ Object
Extension modules registered on this class and all superclasses.
-
.get(path, opts = {}, &block) ⇒ Object
Defining a ‘GET` handler also automatically defines a `HEAD` handler.
- .head(path, opts = {}, &bk) ⇒ Object
-
.helpers(*extensions, &block) ⇒ Object
Makes the methods defined in the block and in the Modules given in ‘extensions` available to the handlers and templates.
-
.inline_templates=(file = nil) ⇒ Object
Load embedded templates from the file; uses the caller’s __FILE__ when no file is specified.
-
.layout(name = :layout, &block) ⇒ Object
Define the layout template.
- .link(path, opts = {}, &bk) ⇒ Object
-
.middleware ⇒ Object
Middleware used in this class and all superclasses.
-
.mime_type(type, value = nil) ⇒ Object
Lookup or register a mime type in Rack’s mime registry.
-
.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’].
-
.new(*args, &bk) ⇒ Object
Create a new instance of the class fronted by its middleware pipeline.
-
.not_found(&block) ⇒ Object
Sugar for ‘error(404) { … }`.
- .options(path, opts = {}, &bk) ⇒ Object
- .patch(path, opts = {}, &bk) ⇒ Object
- .post(path, opts = {}, &bk) ⇒ Object
- .production? ⇒ Boolean
-
.prototype ⇒ Object
The prototype instance used to process requests.
- .public=(value) ⇒ Object
- .public_dir ⇒ Object
- .public_dir=(value) ⇒ Object
- .put(path, opts = {}, &bk) ⇒ Object
-
.quit! ⇒ Object
(also: stop!)
Stop the self-hosted server if running.
-
.register(*extensions, &block) ⇒ Object
Register an extension.
-
.reset! ⇒ Object
Removes all routes, filters, middleware and extension hooks from the current class (not routes/filters/… defined by its superclass).
-
.run!(options = {}, &block) ⇒ Object
(also: start!)
Run the Sinatra app as a self-hosted server using Thin, Puma, Mongrel, or WEBrick (in that order).
-
.running? ⇒ Boolean
Check whether the self-hosted server is running or not.
-
.set(option, value = (not_set = true), ignore_setter = false, &block) ⇒ Object
Sets an option to the given value.
-
.settings ⇒ Object
Access settings defined with Base.set.
-
.template(name, &block) ⇒ Object
Define a named template.
- .test? ⇒ Boolean
- .unlink(path, opts = {}, &bk) ⇒ Object
-
.use(middleware, *args, &block) ⇒ Object
Use the specified Rack middleware.
Instance Method Summary collapse
-
#call(env) ⇒ Object
Rack call interface.
-
#call!(env) ⇒ Object
:nodoc:.
-
#forward ⇒ Object
Forward the request to the downstream app – middleware only.
-
#halt(*response) ⇒ Object
Exit the current block, halts any further processing of the request, and returns the specified response.
-
#initialize(app = nil) {|_self| ... } ⇒ Base
constructor
A new instance of Base.
-
#new! ⇒ Object
Create a new instance without middleware in front of it.
- #options ⇒ Object
-
#pass(&block) ⇒ Object
Pass control to the next matching route.
-
#settings ⇒ Object
Access settings defined with Base.set.
Methods included from Templates
#builder, #coffee, #creole, #erb, #erubis, #find_template, #haml, #less, #liquid, #markaby, #markdown, #nokogiri, #rabl, #radius, #rdoc, #sass, #scss, #slim, #stylus, #textile, #wlang, #yajl
Methods included from Helpers
#attachment, #back, #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
Methods included from Rack::Utils
best_q_match, build_nested_query, build_query, byte_ranges, bytesize, delete_cookie_header!, escape, escape_html, escape_path, normalize_params, params_hash_type?, parse_nested_query, parse_query, q_values, rfc2109, rfc2822, secure_compare, select_best_encoding, set_cookie_header!, status_code, unescape
Constructor Details
Class Attribute Details
.errors ⇒ Object (readonly)
Returns the value of attribute errors.
1148 1149 1150 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1148 def errors @errors end |
.filters ⇒ Object (readonly)
Returns the value of attribute filters.
1148 1149 1150 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1148 def filters @filters end |
.routes ⇒ Object (readonly)
Returns the value of attribute routes.
1148 1149 1150 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1148 def routes @routes end |
.templates ⇒ Object (readonly)
Returns the value of attribute templates.
1148 1149 1150 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1148 def templates @templates end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
865 866 867 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 865 def app @app end |
#env ⇒ Object
Returns the value of attribute env.
865 866 867 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 865 def env @env end |
#params ⇒ Object
Returns the value of attribute params.
865 866 867 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 865 def params @params end |
#request ⇒ Object
Returns the value of attribute request.
865 866 867 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 865 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
865 866 867 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 865 def response @response end |
#template_cache ⇒ Object (readonly)
Returns the value of attribute template_cache.
866 867 868 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 866 def template_cache @template_cache end |
Class Method Details
.add_filter(type, path = nil, options = {}, &block) ⇒ Object
add a filter
1325 1326 1327 1328 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1325 def add_filter(type, path = nil, = {}, &block) path, = //, path if path.respond_to?(:each_pair) filters[type] << compile!(type, path || //, block, ) end |
.after(path = nil, 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.
1320 1321 1322 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1320 def after(path = nil, = {}, &block) add_filter(:after, path, , &block) end |
.before(path = nil, 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.
1313 1314 1315 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1313 def before(path = nil, = {}, &block) add_filter(:before, path, , &block) end |
.build(app) ⇒ Object
Creates a Rack::Builder instance with all the middleware set up and the given app
as end point.
1460 1461 1462 1463 1464 1465 1466 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1460 def build(app) builder = Rack::Builder.new setup_default_middleware builder setup_middleware builder builder.run app builder end |
.call(env) ⇒ Object
1468 1469 1470 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1468 def call(env) synchronize { prototype.call(env) } end |
.caller_files ⇒ Object
Like Kernel#caller but excluding certain magic entries and without line / method information; the resulting array contains filenames only.
1474 1475 1476 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1474 def caller_files cleaned_caller(1).flatten end |
.caller_locations ⇒ Object
Like caller_files, but containing Arrays rather than strings with the first element being the file, and the second being the line.
1480 1481 1482 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1480 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.
1332 1333 1334 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1332 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.
1392 1393 1394 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1392 def configure(*envs) yield self if envs.empty? || envs.include?(environment.to_sym) end |
.delete(path, opts = {}, &bk) ⇒ Object
1361 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1361 def delete(path, opts = {}, &bk) route 'DELETE', path, opts, &bk end |
.development? ⇒ Boolean
1386 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1386 def development?; environment == :development end |
.disable(*opts) ⇒ Object
Same as calling ‘set :option, false` for each of the given options.
1229 1230 1231 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1229 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.
1224 1225 1226 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1224 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.
1236 1237 1238 1239 1240 1241 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1236 def error(*codes, &block) args = compile! "ERROR", //, block codes = codes.map { |c| Array(c) }.flatten codes << Exception if codes.empty? codes.each { |c| (@errors[c] ||= []) << args } end |
.extensions ⇒ Object
Extension modules registered on this class and all superclasses.
1169 1170 1171 1172 1173 1174 1175 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1169 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.
1351 1352 1353 1354 1355 1356 1357 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1351 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
1362 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1362 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
1370 1371 1372 1373 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1370 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.
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1262 def inline_templates=(file = nil) file = (file.nil? || file == true) ? (caller_files.first || File.($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('', 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.
1256 1257 1258 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1256 def layout(name = :layout, &block) template name, &block end |
.link(path, opts = {}, &bk) ⇒ Object
1365 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1365 def link(path, opts = {}, &bk) route 'LINK', path, opts, &bk end |
.middleware ⇒ Object
Middleware used in this class and all superclasses.
1178 1179 1180 1181 1182 1183 1184 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1178 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.
1294 1295 1296 1297 1298 1299 1300 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1294 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']
1305 1306 1307 1308 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1305 def mime_types(type) type = mime_type type type =~ /^application\/(xml|javascript)$/ ? [type, "text/#$1"] : [type] end |
.new(*args, &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.
1453 1454 1455 1456 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1453 def new(*args, &bk) instance = new!(*args, &bk) Wrapper.new(build(instance).to_app, instance) end |
.not_found(&block) ⇒ Object
Sugar for ‘error(404) { … }`
1244 1245 1246 1247 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1244 def not_found(&block) error(404, &block) error(Sinatra::NotFound, &block) end |
.options(path, opts = {}, &bk) ⇒ Object
1363 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1363 def (path, opts = {}, &bk) route 'OPTIONS', path, opts, &bk end |
.patch(path, opts = {}, &bk) ⇒ Object
1364 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1364 def patch(path, opts = {}, &bk) route 'PATCH', path, opts, &bk end |
.post(path, opts = {}, &bk) ⇒ Object
1360 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1360 def post(path, opts = {}, &bk) route 'POST', path, opts, &bk end |
.production? ⇒ Boolean
1387 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1387 def production?; environment == :production end |
.prototype ⇒ Object
The prototype instance used to process requests.
1443 1444 1445 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1443 def prototype @prototype ||= new end |
.public=(value) ⇒ Object
1336 1337 1338 1339 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1336 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_dir ⇒ Object
1345 1346 1347 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1345 def public_dir public_folder end |
.public_dir=(value) ⇒ Object
1341 1342 1343 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1341 def public_dir=(value) self.public_folder = value end |
.put(path, opts = {}, &bk) ⇒ Object
1359 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1359 def put(path, opts = {}, &bk) route 'PUT', path, opts, &bk end |
.quit! ⇒ Object Also known as: stop!
Stop the self-hosted server if running.
1403 1404 1405 1406 1407 1408 1409 1410 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1403 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 handler_name =~/cgi/i 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.
1377 1378 1379 1380 1381 1382 1383 1384 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1377 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).
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1152 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 Thin, Puma, Mongrel, or WEBrick (in that order). If given a block, will call with the constructed handler once we have taken the stage.
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1417 def run!( = {}, &block) return if running? set handler = detect_rack_handler 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.
1438 1439 1440 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1438 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.
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1188 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, Fixnum, 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) if setter define_singleton(option, getter) if getter define_singleton("#{option}?", "!!#{option}") unless method_defined? "#{option}?" self end |
.settings ⇒ Object
Access settings defined with Base.set.
904 905 906 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 904 def self.settings self end |
.template(name, &block) ⇒ Object
Define a named template. The block must return the template source.
1250 1251 1252 1253 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1250 def template(name, &block) filename, line = caller_locations.first templates[name] = [block, filename, line.to_i] end |
.test? ⇒ Boolean
1388 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1388 def test?; environment == :test end |
.unlink(path, opts = {}, &bk) ⇒ Object
1366 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1366 def unlink(path, opts = {}, &bk) route 'UNLINK', path, opts, &bk end |
.use(middleware, *args, &block) ⇒ Object
Use the specified Rack middleware
1397 1398 1399 1400 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1397 def use(middleware, *args, &block) @prototype = nil @middleware << [middleware, args, block] end |
Instance Method Details
#call(env) ⇒ Object
Rack call interface.
876 877 878 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 876 def call(env) dup.call!(env) end |
#call!(env) ⇒ Object
:nodoc:
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 880 def call!(env) # :nodoc: @env = env @request = Request.new(env) @response = Response.new @params = indifferent_params(@request.params) template_cache.clear if settings.reload_templates force_encoding(@params) @response['Content-Type'] = nil invoke { dispatch! } invoke { error_block!(response.status) } unless @env['sinatra.error'] unless @response['Content-Type'] if Array === body and body[0].respond_to? :content_type content_type body[0].content_type else content_type :html end end @response.finish end |
#forward ⇒ Object
Forward the request to the downstream app – middleware only.
934 935 936 937 938 939 940 941 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 934 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.
921 922 923 924 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 921 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.
1448 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 1448 alias new! new |
#options ⇒ Object
913 914 915 916 917 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 913 def 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.
929 930 931 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 929 def pass(&block) throw :pass, block end |
#settings ⇒ Object
Access settings defined with Base.set.
909 910 911 |
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 909 def settings self.class.settings end |