sinatra 1.4.5 → 1.4.6
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sinatra might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/AUTHORS.md +77 -0
- data/CHANGES +30 -0
- data/Gemfile +5 -5
- data/README.de.md +186 -56
- data/README.es.md +76 -76
- data/README.fr.md +120 -56
- data/README.hu.md +19 -19
- data/README.ja.md +44 -46
- data/README.ko.md +163 -67
- data/README.md +151 -127
- data/README.pt-br.md +905 -144
- data/README.pt-pt.md +17 -17
- data/README.ru.md +88 -52
- data/README.zh.md +76 -68
- data/lib/sinatra.rb +0 -1
- data/lib/sinatra/base.rb +21 -15
- data/lib/sinatra/show_exceptions.rb +10 -4
- data/lib/sinatra/version.rb +1 -1
- data/sinatra.gemspec +1 -1
- data/test/asciidoctor_test.rb +2 -2
- data/test/base_test.rb +1 -5
- data/test/builder_test.rb +2 -2
- data/test/coffee_test.rb +8 -2
- data/test/compile_test.rb +1 -1
- data/test/contest.rb +3 -12
- data/test/creole_test.rb +2 -2
- data/test/delegator_test.rb +1 -1
- data/test/encoding_test.rb +1 -1
- data/test/erb_test.rb +1 -1
- data/test/extensions_test.rb +1 -1
- data/test/filter_test.rb +2 -2
- data/test/haml_test.rb +2 -2
- data/test/helper.rb +8 -7
- data/test/helpers_test.rb +6 -6
- data/test/integration_test.rb +3 -3
- data/test/less_test.rb +2 -2
- data/test/liquid_test.rb +3 -3
- data/test/mapped_error_test.rb +5 -5
- data/test/markaby_test.rb +2 -2
- data/test/markdown_test.rb +6 -3
- data/test/mediawiki_test.rb +2 -2
- data/test/middleware_test.rb +1 -1
- data/test/nokogiri_test.rb +2 -2
- data/test/rabl_test.rb +2 -2
- data/test/rack_test.rb +1 -1
- data/test/radius_test.rb +2 -2
- data/test/rdoc_test.rb +4 -4
- data/test/readme_test.rb +1 -1
- data/test/request_test.rb +4 -1
- data/test/response_test.rb +1 -1
- data/test/result_test.rb +2 -2
- data/test/route_added_hook_test.rb +1 -1
- data/test/routing_test.rb +7 -7
- data/test/sass_test.rb +3 -3
- data/test/scss_test.rb +2 -2
- data/test/server_test.rb +10 -2
- data/test/settings_test.rb +4 -4
- data/test/sinatra_test.rb +1 -1
- data/test/slim_test.rb +2 -2
- data/test/static_test.rb +2 -2
- data/test/streaming_test.rb +2 -2
- data/test/stylus_test.rb +2 -2
- data/test/templates_test.rb +3 -3
- data/test/textile_test.rb +2 -2
- data/test/wlang_test.rb +1 -1
- data/test/yajl_test.rb +2 -2
- metadata +10 -11
- data/AUTHORS +0 -61
data/README.md
CHANGED
@@ -69,6 +69,7 @@ pick up if available.
|
|
69
69
|
* [Named Templates](#named-templates)
|
70
70
|
* [Associating File Extensions](#associating-file-extensions)
|
71
71
|
* [Adding Your Own Template Engine](#adding-your-own-template-engine)
|
72
|
+
* [Using Custom Logic for Template Lookup](#using-custom-logic-for-template-lookup)
|
72
73
|
* [Filters](#filters)
|
73
74
|
* [Helpers](#helpers)
|
74
75
|
* [Using Sessions](#using-sessions)
|
@@ -164,8 +165,8 @@ Route patterns may include named parameters, accessible via the
|
|
164
165
|
``` ruby
|
165
166
|
get '/hello/:name' do
|
166
167
|
# matches "GET /hello/foo" and "GET /hello/bar"
|
167
|
-
# params[
|
168
|
-
"Hello #{params[
|
168
|
+
# params['name'] is 'foo' or 'bar'
|
169
|
+
"Hello #{params['name']}!"
|
169
170
|
end
|
170
171
|
```
|
171
172
|
|
@@ -174,24 +175,24 @@ You can also access named parameters via block parameters:
|
|
174
175
|
``` ruby
|
175
176
|
get '/hello/:name' do |n|
|
176
177
|
# matches "GET /hello/foo" and "GET /hello/bar"
|
177
|
-
# params[
|
178
|
-
# n stores params[
|
178
|
+
# params['name'] is 'foo' or 'bar'
|
179
|
+
# n stores params['name']
|
179
180
|
"Hello #{n}!"
|
180
181
|
end
|
181
182
|
```
|
182
183
|
|
183
184
|
Route patterns may also include splat (or wildcard) parameters, accessible
|
184
|
-
via the `params[
|
185
|
+
via the `params['splat']` array:
|
185
186
|
|
186
187
|
``` ruby
|
187
188
|
get '/say/*/to/*' do
|
188
189
|
# matches /say/hello/to/world
|
189
|
-
params[
|
190
|
+
params['splat'] # => ["hello", "world"]
|
190
191
|
end
|
191
192
|
|
192
193
|
get '/download/*.*' do
|
193
194
|
# matches /download/path/to/file.xml
|
194
|
-
params[
|
195
|
+
params['splat'] # => ["path/to/file", "xml"]
|
195
196
|
end
|
196
197
|
```
|
197
198
|
|
@@ -206,8 +207,8 @@ end
|
|
206
207
|
Route matching with Regular Expressions:
|
207
208
|
|
208
209
|
``` ruby
|
209
|
-
get
|
210
|
-
"Hello, #{params[
|
210
|
+
get /\A\/hello\/([\w]+)\z/ do
|
211
|
+
"Hello, #{params['captures'].first}!"
|
211
212
|
end
|
212
213
|
```
|
213
214
|
|
@@ -215,6 +216,7 @@ Or with a block parameter:
|
|
215
216
|
|
216
217
|
``` ruby
|
217
218
|
get %r{/hello/([\w]+)} do |c|
|
219
|
+
# Matches "GET /meta/hello/world", "GET /hello/world/1234" etc.
|
218
220
|
"Hello, #{c}!"
|
219
221
|
end
|
220
222
|
```
|
@@ -232,8 +234,8 @@ Routes may also utilize query parameters:
|
|
232
234
|
``` ruby
|
233
235
|
get '/posts' do
|
234
236
|
# matches "GET /posts?title=foo&author=bar"
|
235
|
-
title = params[
|
236
|
-
author = params[
|
237
|
+
title = params['title']
|
238
|
+
author = params['author']
|
237
239
|
# uses title and author variables; query is optional to the /posts route
|
238
240
|
end
|
239
241
|
```
|
@@ -247,7 +249,7 @@ Routes may include a variety of matching conditions, such as the user agent:
|
|
247
249
|
|
248
250
|
``` ruby
|
249
251
|
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
|
250
|
-
"You're using Songbird version #{params[
|
252
|
+
"You're using Songbird version #{params['agent'][0]}"
|
251
253
|
end
|
252
254
|
|
253
255
|
get '/foo' do
|
@@ -270,6 +272,7 @@ get '/', :provides => ['rss', 'atom', 'xml'] do
|
|
270
272
|
builder :feed
|
271
273
|
end
|
272
274
|
```
|
275
|
+
`provides` searches the request's Accept header.
|
273
276
|
|
274
277
|
You can easily define your own conditions:
|
275
278
|
|
@@ -478,8 +481,9 @@ Available Options:
|
|
478
481
|
|
479
482
|
<dt>layout</dt>
|
480
483
|
<dd>
|
481
|
-
Whether to use a layout (<tt>true</tt> or <tt>false</tt>). If it's a
|
482
|
-
what template to use. Example:
|
484
|
+
Whether to use a layout (<tt>true</tt> or <tt>false</tt>). If it's a
|
485
|
+
Symbol, specifies what template to use. Example:
|
486
|
+
<tt>erb :index, :layout => !request.xhr?</tt>
|
483
487
|
</dd>
|
484
488
|
|
485
489
|
<dt>content_type</dt>
|
@@ -584,7 +588,7 @@ get('/') { markdown :index }
|
|
584
588
|
<tr>
|
585
589
|
<td>Dependency</td>
|
586
590
|
<td>
|
587
|
-
<a href="
|
591
|
+
<a href="https://github.com/jimweirich/builder" title="builder">builder</a>
|
588
592
|
</td>
|
589
593
|
</tr>
|
590
594
|
<tr>
|
@@ -699,8 +703,8 @@ template, you almost always want to pass locals to it.
|
|
699
703
|
<a href="https://github.com/rtomayko/rdiscount" title="RDiscount">RDiscount</a>,
|
700
704
|
<a href="https://github.com/vmg/redcarpet" title="RedCarpet">RedCarpet</a>,
|
701
705
|
<a href="http://deveiate.org/projects/BlueCloth" title="BlueCloth">BlueCloth</a>,
|
702
|
-
<a href="http://kramdown.
|
703
|
-
<a href="
|
706
|
+
<a href="http://kramdown.gettalong.org/" title="kramdown">kramdown</a>,
|
707
|
+
<a href="https://github.com/bhollis/maruku" title="maruku">maruku</a>
|
704
708
|
</td>
|
705
709
|
</tr>
|
706
710
|
<tr>
|
@@ -772,7 +776,7 @@ template than for the layout by passing the `:layout_engine` option.
|
|
772
776
|
<table>
|
773
777
|
<tr>
|
774
778
|
<td>Dependency</td>
|
775
|
-
<td><a href="http://rdoc.
|
779
|
+
<td><a href="http://rdoc.sourceforge.net/" title="RDoc">RDoc</a></td>
|
776
780
|
</tr>
|
777
781
|
<tr>
|
778
782
|
<td>File Extension</td>
|
@@ -819,15 +823,15 @@ template than for the layout by passing the `:layout_engine` option.
|
|
819
823
|
</tr>
|
820
824
|
</table>
|
821
825
|
|
822
|
-
Since you cannot call Ruby methods directly from an AsciiDoc template, you
|
823
|
-
always want to pass locals to it.
|
826
|
+
Since you cannot call Ruby methods directly from an AsciiDoc template, you
|
827
|
+
almost always want to pass locals to it.
|
824
828
|
|
825
829
|
#### Radius Templates
|
826
830
|
|
827
831
|
<table>
|
828
832
|
<tr>
|
829
833
|
<td>Dependency</td>
|
830
|
-
<td><a href="
|
834
|
+
<td><a href="https://github.com/jlong/radius" title="Radius">Radius</a></td>
|
831
835
|
</tr>
|
832
836
|
<tr>
|
833
837
|
<td>File Extension</td>
|
@@ -947,8 +951,9 @@ template than for the layout by passing the `:layout_engine` option.
|
|
947
951
|
</tr>
|
948
952
|
</table>
|
949
953
|
|
950
|
-
It is not possible to call methods from MediaWiki markup, nor to pass locals to
|
951
|
-
You therefore will usually use it in combination with another rendering
|
954
|
+
It is not possible to call methods from MediaWiki markup, nor to pass locals to
|
955
|
+
it. You therefore will usually use it in combination with another rendering
|
956
|
+
engine:
|
952
957
|
|
953
958
|
``` ruby
|
954
959
|
erb :overview, :locals => { :text => mediawiki(:introduction) }
|
@@ -1084,8 +1089,8 @@ present(resource);
|
|
1084
1089
|
</tr>
|
1085
1090
|
</table>
|
1086
1091
|
|
1087
|
-
Since calling ruby methods is not idiomatic in WLang, you almost always want to
|
1088
|
-
to it. Layouts written in WLang and `yield` are supported, though.
|
1092
|
+
Since calling ruby methods is not idiomatic in WLang, you almost always want to
|
1093
|
+
pass locals to it. Layouts written in WLang and `yield` are supported, though.
|
1089
1094
|
|
1090
1095
|
### Accessing Variables in Templates
|
1091
1096
|
|
@@ -1094,7 +1099,7 @@ variables set in route handlers are directly accessible by templates:
|
|
1094
1099
|
|
1095
1100
|
``` ruby
|
1096
1101
|
get '/:id' do
|
1097
|
-
@foo = Foo.find(params[
|
1102
|
+
@foo = Foo.find(params['id'])
|
1098
1103
|
haml '%h1= @foo.name'
|
1099
1104
|
end
|
1100
1105
|
```
|
@@ -1103,7 +1108,7 @@ Or, specify an explicit Hash of local variables:
|
|
1103
1108
|
|
1104
1109
|
``` ruby
|
1105
1110
|
get '/:id' do
|
1106
|
-
foo = Foo.find(params[
|
1111
|
+
foo = Foo.find(params['id'])
|
1107
1112
|
haml '%h1= bar.name', :locals => { :bar => foo }
|
1108
1113
|
end
|
1109
1114
|
```
|
@@ -1125,8 +1130,7 @@ end
|
|
1125
1130
|
|
1126
1131
|
This code is mostly equivalent to `erb :index, :layout => :post`.
|
1127
1132
|
|
1128
|
-
Passing blocks to rendering methods is most useful for creating nested
|
1129
|
-
layouts:
|
1133
|
+
Passing blocks to rendering methods is most useful for creating nested layouts:
|
1130
1134
|
|
1131
1135
|
``` ruby
|
1132
1136
|
erb :main_layout, :layout => false do
|
@@ -1145,8 +1149,7 @@ end
|
|
1145
1149
|
```
|
1146
1150
|
|
1147
1151
|
Currently, the following rendering methods accept a block: `erb`, `haml`,
|
1148
|
-
`liquid`, `slim `, `wlang`.
|
1149
|
-
Also the general `render` method accepts a block.
|
1152
|
+
`liquid`, `slim `, `wlang`. Also the general `render` method accepts a block.
|
1150
1153
|
|
1151
1154
|
### Inline Templates
|
1152
1155
|
|
@@ -1231,6 +1234,23 @@ end
|
|
1231
1234
|
Renders `./views/index.myat`. See https://github.com/rtomayko/tilt to
|
1232
1235
|
learn more about Tilt.
|
1233
1236
|
|
1237
|
+
### Using Custom Logic for Template Lookup
|
1238
|
+
|
1239
|
+
To implement your own template lookup mechanism you can write your
|
1240
|
+
own `#find_template` method:
|
1241
|
+
|
1242
|
+
``` ruby
|
1243
|
+
configure do
|
1244
|
+
set :views [ './views/a', './views/b' ]
|
1245
|
+
end
|
1246
|
+
|
1247
|
+
def find_template(views, name, engine, &block)
|
1248
|
+
Array(views).each do |v|
|
1249
|
+
super(v, name, engine, &block)
|
1250
|
+
end
|
1251
|
+
end
|
1252
|
+
```
|
1253
|
+
|
1234
1254
|
## Filters
|
1235
1255
|
|
1236
1256
|
Before filters are evaluated before each request within the same
|
@@ -1245,13 +1265,13 @@ end
|
|
1245
1265
|
|
1246
1266
|
get '/foo/*' do
|
1247
1267
|
@note #=> 'Hi!'
|
1248
|
-
params[
|
1268
|
+
params['splat'] #=> 'bar/baz'
|
1249
1269
|
end
|
1250
1270
|
```
|
1251
1271
|
|
1252
|
-
After filters are evaluated after each request within the same
|
1253
|
-
|
1254
|
-
|
1272
|
+
After filters are evaluated after each request within the same context as the
|
1273
|
+
routes will be and can also modify the request and response. Instance variables
|
1274
|
+
set in before filters and routes are accessible by after filters:
|
1255
1275
|
|
1256
1276
|
``` ruby
|
1257
1277
|
after do
|
@@ -1301,7 +1321,7 @@ helpers do
|
|
1301
1321
|
end
|
1302
1322
|
|
1303
1323
|
get '/:name' do
|
1304
|
-
bar(params[
|
1324
|
+
bar(params['name'])
|
1305
1325
|
end
|
1306
1326
|
```
|
1307
1327
|
|
@@ -1334,7 +1354,7 @@ get '/' do
|
|
1334
1354
|
end
|
1335
1355
|
|
1336
1356
|
get '/:value' do
|
1337
|
-
session[
|
1357
|
+
session['value'] = params['value']
|
1338
1358
|
end
|
1339
1359
|
```
|
1340
1360
|
|
@@ -1352,7 +1372,7 @@ get '/' do
|
|
1352
1372
|
end
|
1353
1373
|
|
1354
1374
|
get '/:value' do
|
1355
|
-
session[
|
1375
|
+
session['value'] = params['value']
|
1356
1376
|
end
|
1357
1377
|
```
|
1358
1378
|
|
@@ -1423,7 +1443,7 @@ A route can punt processing to the next matching route using `pass`:
|
|
1423
1443
|
|
1424
1444
|
``` ruby
|
1425
1445
|
get '/guess/:who' do
|
1426
|
-
pass unless params[
|
1446
|
+
pass unless params['who'] == 'Frank'
|
1427
1447
|
'You got me!'
|
1428
1448
|
end
|
1429
1449
|
|
@@ -1452,8 +1472,7 @@ end
|
|
1452
1472
|
```
|
1453
1473
|
|
1454
1474
|
Note that in the example above, you would ease testing and increase performance
|
1455
|
-
by simply moving `"bar"` into a helper used by both `/foo`
|
1456
|
-
and `/bar`.
|
1475
|
+
by simply moving `"bar"` into a helper used by both `/foo` and `/bar`.
|
1457
1476
|
|
1458
1477
|
If you want the request to be sent to the same application instance rather than
|
1459
1478
|
a duplicate, use `call!` instead of `call`.
|
@@ -1540,19 +1559,17 @@ connections = []
|
|
1540
1559
|
|
1541
1560
|
get '/subscribe' do
|
1542
1561
|
# register a client's interest in server events
|
1543
|
-
stream(:keep_open)
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
# acknowledge
|
1549
|
-
"subscribed"
|
1562
|
+
stream(:keep_open) do |out|
|
1563
|
+
connections << out
|
1564
|
+
# purge dead connections
|
1565
|
+
connections.reject!(&:closed?)
|
1566
|
+
end
|
1550
1567
|
end
|
1551
1568
|
|
1552
|
-
post '
|
1569
|
+
post '/:message' do
|
1553
1570
|
connections.each do |out|
|
1554
1571
|
# notify client that a new message has arrived
|
1555
|
-
out << params[
|
1572
|
+
out << params['message'] << "\n"
|
1556
1573
|
|
1557
1574
|
# indicate client to connect again
|
1558
1575
|
out.close
|
@@ -1578,9 +1595,8 @@ This logger will automatically take your Rack handler's logging settings into
|
|
1578
1595
|
account. If logging is disabled, this method will return a dummy object, so
|
1579
1596
|
you do not have to worry about it in your routes and filters.
|
1580
1597
|
|
1581
|
-
Note that logging is only enabled for `Sinatra::Application` by
|
1582
|
-
|
1583
|
-
enable it yourself:
|
1598
|
+
Note that logging is only enabled for `Sinatra::Application` by default, so if
|
1599
|
+
you inherit from `Sinatra::Base`, you probably want to enable it yourself:
|
1584
1600
|
|
1585
1601
|
``` ruby
|
1586
1602
|
class MyApp < Sinatra::Base
|
@@ -1711,13 +1727,13 @@ end
|
|
1711
1727
|
```
|
1712
1728
|
|
1713
1729
|
To properly use caches, you should consider using `etag` or `last_modified`.
|
1714
|
-
It is recommended to call those helpers *before* doing any heavy lifting, as
|
1715
|
-
will immediately flush a response if the client already has the current
|
1730
|
+
It is recommended to call those helpers *before* doing any heavy lifting, as
|
1731
|
+
they will immediately flush a response if the client already has the current
|
1716
1732
|
version in its cache:
|
1717
1733
|
|
1718
1734
|
``` ruby
|
1719
|
-
get
|
1720
|
-
@article = Article.find params[
|
1735
|
+
get "/article/:id" do
|
1736
|
+
@article = Article.find params['id']
|
1721
1737
|
last_modified @article.updated_at
|
1722
1738
|
etag @article.sha1
|
1723
1739
|
erb :article
|
@@ -1774,7 +1790,8 @@ etag '', :new_resource => true, :kind => :weak
|
|
1774
1790
|
|
1775
1791
|
### Sending Files
|
1776
1792
|
|
1777
|
-
|
1793
|
+
To return the contents of a file as the response, you can use the `send_file`
|
1794
|
+
helper method:
|
1778
1795
|
|
1779
1796
|
``` ruby
|
1780
1797
|
get '/' do
|
@@ -1792,30 +1809,31 @@ The options are:
|
|
1792
1809
|
|
1793
1810
|
<dl>
|
1794
1811
|
<dt>filename</dt>
|
1795
|
-
<dd>
|
1812
|
+
<dd>File name to be used in the response, defaults to the real file name.</dd>
|
1796
1813
|
|
1797
1814
|
<dt>last_modified</dt>
|
1798
|
-
<dd>
|
1815
|
+
<dd>Value for Last-Modified header, defaults to the file's mtime.</dd>
|
1799
1816
|
|
1800
1817
|
<dt>type</dt>
|
1801
|
-
<dd>
|
1818
|
+
<dd>Value for Content-Type header, guessed from the file extension if
|
1819
|
+
missing.</dd>
|
1802
1820
|
|
1803
|
-
|
1821
|
+
<dt>disposition</dt>
|
1804
1822
|
<dd>
|
1805
|
-
|
1806
|
-
<tt>:attachment</tt> and <tt>:inline</tt>
|
1823
|
+
Value for Content-Disposition header, possible values: <tt>nil</tt>
|
1824
|
+
(default), <tt>:attachment</tt> and <tt>:inline</tt>
|
1807
1825
|
</dd>
|
1808
1826
|
|
1809
1827
|
<dt>length</dt>
|
1810
|
-
<dd>Content-Length header, defaults to file size.</dd>
|
1828
|
+
<dd>Value for Content-Length header, defaults to file size.</dd>
|
1811
1829
|
|
1812
1830
|
<dt>status</dt>
|
1813
1831
|
<dd>
|
1814
1832
|
Status code to be sent. Useful when sending a static file as an error page.
|
1815
1833
|
|
1816
1834
|
If supported by the Rack handler, other means than streaming from the Ruby
|
1817
|
-
process will be used. If you use this helper method, Sinatra will
|
1818
|
-
handle range requests.
|
1835
|
+
process will be used. If you use this helper method, Sinatra will
|
1836
|
+
automatically handle range requests.
|
1819
1837
|
</dd>
|
1820
1838
|
</dl>
|
1821
1839
|
|
@@ -1857,8 +1875,7 @@ get '/foo' do
|
|
1857
1875
|
end
|
1858
1876
|
```
|
1859
1877
|
|
1860
|
-
Some options, like `script_name` or `path_info`, can also be
|
1861
|
-
written:
|
1878
|
+
Some options, like `script_name` or `path_info`, can also be written:
|
1862
1879
|
|
1863
1880
|
``` ruby
|
1864
1881
|
before { request.path_info = "/" }
|
@@ -1901,9 +1918,8 @@ end
|
|
1901
1918
|
|
1902
1919
|
### Dealing with Date and Time
|
1903
1920
|
|
1904
|
-
Sinatra offers a `time_for` helper method that generates a Time object
|
1905
|
-
|
1906
|
-
similar classes:
|
1921
|
+
Sinatra offers a `time_for` helper method that generates a Time object from the
|
1922
|
+
given value. It is also able to convert `DateTime`, `Date` and similar classes:
|
1907
1923
|
|
1908
1924
|
``` ruby
|
1909
1925
|
get '/' do
|
@@ -2083,11 +2099,11 @@ set :protection, :session => true
|
|
2083
2099
|
</dd>
|
2084
2100
|
<dd>Disabled by default.</dd>
|
2085
2101
|
|
2086
|
-
<dt>
|
2102
|
+
<dt>add_charset</dt>
|
2087
2103
|
<dd>
|
2088
2104
|
Mime types the <tt>content_type</tt> helper will automatically add the charset info to.
|
2089
2105
|
You should add to it rather than overriding this option:
|
2090
|
-
<tt>settings.
|
2106
|
+
<tt>settings.add_charset << "application/foobar"</tt>
|
2091
2107
|
</dd>
|
2092
2108
|
|
2093
2109
|
<dt>app_file</dt>
|
@@ -2097,7 +2113,9 @@ set :protection, :session => true
|
|
2097
2113
|
</dd>
|
2098
2114
|
|
2099
2115
|
<dt>bind</dt>
|
2100
|
-
<dd>IP address to bind to (default: <tt>0.0.0.0</tt> <em>or</em>
|
2116
|
+
<dd>IP address to bind to (default: <tt>0.0.0.0</tt> <em>or</em>
|
2117
|
+
<tt>localhost</tt> if your `environment` is set to development). Only used
|
2118
|
+
for built-in server.</dd>
|
2101
2119
|
|
2102
2120
|
<dt>default_encoding</dt>
|
2103
2121
|
<dd>Encoding to assume if unknown (defaults to <tt>"utf-8"</tt>).</dd>
|
@@ -2107,8 +2125,8 @@ set :protection, :session => true
|
|
2107
2125
|
|
2108
2126
|
<dt>environment</dt>
|
2109
2127
|
<dd>
|
2110
|
-
Current environment. Defaults to <tt>ENV['RACK_ENV']</tt>, or
|
2111
|
-
not available.
|
2128
|
+
Current environment. Defaults to <tt>ENV['RACK_ENV']</tt>, or
|
2129
|
+
<tt>"development"</tt> if not available.
|
2112
2130
|
</dd>
|
2113
2131
|
|
2114
2132
|
<dt>logging</dt>
|
@@ -2138,7 +2156,8 @@ set :protection, :session => true
|
|
2138
2156
|
</dd>
|
2139
2157
|
|
2140
2158
|
<dt>protection</dt>
|
2141
|
-
<dd>Whether or not to enable web attack protections. See protection section
|
2159
|
+
<dd>Whether or not to enable web attack protections. See protection section
|
2160
|
+
above.</dd>
|
2142
2161
|
|
2143
2162
|
<dt>public_dir</dt>
|
2144
2163
|
<dd>Alias for <tt>public_folder</tt>. See below.</dd>
|
@@ -2152,12 +2171,14 @@ set :protection, :session => true
|
|
2152
2171
|
|
2153
2172
|
<dt>reload_templates</dt>
|
2154
2173
|
<dd>
|
2155
|
-
Whether or not to reload templates between requests. Enabled in development
|
2174
|
+
Whether or not to reload templates between requests. Enabled in development
|
2175
|
+
mode.
|
2156
2176
|
</dd>
|
2157
2177
|
|
2158
2178
|
<dt>root</dt>
|
2159
2179
|
<dd>
|
2160
|
-
Path to project root folder. Inferred from <tt>app_file</tt> setting if not
|
2180
|
+
Path to project root folder. Inferred from <tt>app_file</tt> setting if not
|
2181
|
+
set.
|
2161
2182
|
</dd>
|
2162
2183
|
|
2163
2184
|
<dt>raise_errors</dt>
|
@@ -2189,14 +2210,13 @@ set :protection, :session => true
|
|
2189
2210
|
|
2190
2211
|
<dt>show_exceptions</dt>
|
2191
2212
|
<dd>
|
2192
|
-
Show a stack trace in the browser when an exception
|
2193
|
-
|
2194
|
-
|
2213
|
+
Show a stack trace in the browser when an exception happens. Enabled by
|
2214
|
+
default when <tt>environment</tt> is set to <tt>"development"</tt>,
|
2215
|
+
disabled otherwise.
|
2195
2216
|
</dd>
|
2196
2217
|
<dd>
|
2197
|
-
Can also be set to <tt>:after_handler</tt> to trigger
|
2198
|
-
|
2199
|
-
trace in the browser.
|
2218
|
+
Can also be set to <tt>:after_handler</tt> to trigger app-specified error
|
2219
|
+
handling before showing a stack trace in the browser.
|
2200
2220
|
</dd>
|
2201
2221
|
|
2202
2222
|
<dt>static</dt>
|
@@ -2204,15 +2224,14 @@ set :protection, :session => true
|
|
2204
2224
|
<dd>Disable when using a server able to do this on its own.</dd>
|
2205
2225
|
<dd>Disabling will boost performance.</dd>
|
2206
2226
|
<dd>
|
2207
|
-
Enabled per default in classic style, disabled for
|
2208
|
-
modular apps.
|
2227
|
+
Enabled per default in classic style, disabled for modular apps.
|
2209
2228
|
</dd>
|
2210
2229
|
|
2211
2230
|
<dt>static_cache_control</dt>
|
2212
2231
|
<dd>
|
2213
|
-
When Sinatra is serving static files, set this to add
|
2214
|
-
|
2215
|
-
|
2232
|
+
When Sinatra is serving static files, set this to add <tt>Cache-Control</tt>
|
2233
|
+
headers to the responses. Uses the <tt>cache_control</tt> helper. Disabled
|
2234
|
+
by default.
|
2216
2235
|
</dd>
|
2217
2236
|
<dd>
|
2218
2237
|
Use an explicit array when setting multiple values:
|
@@ -2243,13 +2262,12 @@ set :protection, :session => true
|
|
2243
2262
|
|
2244
2263
|
## Environments
|
2245
2264
|
|
2246
|
-
There are three predefined `environments`: `"development"`,
|
2247
|
-
`"
|
2248
|
-
|
2249
|
-
|
2250
|
-
|
2251
|
-
|
2252
|
-
In the `"production"` and `"test"` environments, templates are cached by default.
|
2265
|
+
There are three predefined `environments`: `"development"`, `"production"` and
|
2266
|
+
`"test"`. Environments can be set through the `RACK_ENV` environment variable.
|
2267
|
+
The default value is `"development"`. In the `"development"` environment all
|
2268
|
+
templates are reloaded between requests, and special `not_found` and `error`
|
2269
|
+
handlers display stack traces in your browser. In the `"production"` and
|
2270
|
+
`"test"` environments, templates are cached by default.
|
2253
2271
|
|
2254
2272
|
To run different environments, set the `RACK_ENV` environment variable:
|
2255
2273
|
|
@@ -2290,12 +2308,18 @@ end
|
|
2290
2308
|
### Error
|
2291
2309
|
|
2292
2310
|
The `error` handler is invoked any time an exception is raised from a route
|
2293
|
-
block or a filter.
|
2294
|
-
|
2311
|
+
block or a filter. But note in development it will only run if you set the
|
2312
|
+
show exceptions option to `:after_handler`:
|
2313
|
+
|
2314
|
+
```ruby
|
2315
|
+
set :show_exceptions, :after_handler
|
2316
|
+
```
|
2317
|
+
|
2318
|
+
The exception object can be obtained from the `sinatra.error` Rack variable:
|
2295
2319
|
|
2296
2320
|
``` ruby
|
2297
2321
|
error do
|
2298
|
-
'Sorry there was a nasty error - ' + env['sinatra.error'].
|
2322
|
+
'Sorry there was a nasty error - ' + env['sinatra.error'].message
|
2299
2323
|
end
|
2300
2324
|
```
|
2301
2325
|
|
@@ -2347,7 +2371,7 @@ and additional debugging information in your browser.
|
|
2347
2371
|
|
2348
2372
|
## Rack Middleware
|
2349
2373
|
|
2350
|
-
Sinatra rides on [Rack](http://rack.
|
2374
|
+
Sinatra rides on [Rack](http://rack.github.io/), a minimal standard
|
2351
2375
|
interface for Ruby web frameworks. One of Rack's most interesting capabilities
|
2352
2376
|
for application developers is support for "middleware" -- components that sit
|
2353
2377
|
between the server and your application monitoring and/or manipulating the
|
@@ -2369,7 +2393,7 @@ end
|
|
2369
2393
|
```
|
2370
2394
|
|
2371
2395
|
The semantics of `use` are identical to those defined for the
|
2372
|
-
[Rack::Builder](http://
|
2396
|
+
[Rack::Builder](http://rubydoc.info/github/rack/rack/master/Rack/Builder) DSL
|
2373
2397
|
(most frequently used from rackup files). For example, the `use` method
|
2374
2398
|
accepts multiple/variable args as well as blocks:
|
2375
2399
|
|
@@ -2397,10 +2421,10 @@ is recommended:
|
|
2397
2421
|
|
2398
2422
|
``` ruby
|
2399
2423
|
require 'my_sinatra_app'
|
2400
|
-
require '
|
2424
|
+
require 'minitest/autorun'
|
2401
2425
|
require 'rack/test'
|
2402
2426
|
|
2403
|
-
class MyAppTest < Test
|
2427
|
+
class MyAppTest < Minitest::Test
|
2404
2428
|
include Rack::Test::Methods
|
2405
2429
|
|
2406
2430
|
def app
|
@@ -2424,8 +2448,8 @@ class MyAppTest < Test::Unit::TestCase
|
|
2424
2448
|
end
|
2425
2449
|
```
|
2426
2450
|
|
2427
|
-
Note: If you are using Sinatra in the modular style, replace
|
2428
|
-
above with the class name of your app.
|
2451
|
+
Note: If you are using Sinatra in the modular style, replace
|
2452
|
+
`Sinatra::Application` above with the class name of your app.
|
2429
2453
|
|
2430
2454
|
## Sinatra::Base - Middleware, Libraries, and Modular Apps
|
2431
2455
|
|
@@ -2450,8 +2474,8 @@ class MyApp < Sinatra::Base
|
|
2450
2474
|
end
|
2451
2475
|
```
|
2452
2476
|
|
2453
|
-
The methods available to `Sinatra::Base` subclasses are exactly the same as
|
2454
|
-
available via the top-level DSL. Most top-level apps can be converted to
|
2477
|
+
The methods available to `Sinatra::Base` subclasses are exactly the same as
|
2478
|
+
those available via the top-level DSL. Most top-level apps can be converted to
|
2455
2479
|
`Sinatra::Base` components with two modifications:
|
2456
2480
|
|
2457
2481
|
* Your file should require `sinatra/base` instead of `sinatra`;
|
@@ -2465,7 +2489,7 @@ including the built-in server. See
|
|
2465
2489
|
[Configuring Settings](http://sinatra.github.com/configuration.html)
|
2466
2490
|
for details on available options and their behavior. If you want
|
2467
2491
|
behavior more similar to when you define your app at the top level (also
|
2468
|
-
|
2492
|
+
known as Classic style), you
|
2469
2493
|
can subclass `Sinatra::Application`.
|
2470
2494
|
|
2471
2495
|
``` ruby
|
@@ -2483,10 +2507,10 @@ end
|
|
2483
2507
|
Contrary to common belief, there is nothing wrong with the classic style. If it
|
2484
2508
|
suits your application, you do not have to switch to a modular application.
|
2485
2509
|
|
2486
|
-
The main disadvantage of using the classic style rather than the modular style
|
2487
|
-
you will only have one Sinatra application per Ruby process. If you
|
2488
|
-
more than one, switch to the modular style. There is no reason you
|
2489
|
-
the modular and the classic styles.
|
2510
|
+
The main disadvantage of using the classic style rather than the modular style
|
2511
|
+
is that you will only have one Sinatra application per Ruby process. If you
|
2512
|
+
plan to use more than one, switch to the modular style. There is no reason you
|
2513
|
+
cannot mix the modular and the classic styles.
|
2490
2514
|
|
2491
2515
|
If switching from one style to the other, you should be aware of slightly
|
2492
2516
|
different default settings:
|
@@ -2608,9 +2632,9 @@ A `config.ru` file is recommended if:
|
|
2608
2632
|
* You want to use more than one subclass of `Sinatra::Base`.
|
2609
2633
|
* You want to use Sinatra only for middleware, and not as an endpoint.
|
2610
2634
|
|
2611
|
-
**There is no need to switch to a `config.ru` simply because you
|
2612
|
-
|
2613
|
-
|
2635
|
+
**There is no need to switch to a `config.ru` simply because you switched to
|
2636
|
+
the modular style, and you don't have to use the modular style for running with
|
2637
|
+
a `config.ru`.**
|
2614
2638
|
|
2615
2639
|
### Using Sinatra as Middleware
|
2616
2640
|
|
@@ -2628,8 +2652,8 @@ class LoginScreen < Sinatra::Base
|
|
2628
2652
|
get('/login') { haml :login }
|
2629
2653
|
|
2630
2654
|
post('/login') do
|
2631
|
-
if params[
|
2632
|
-
session['user_name'] = params[
|
2655
|
+
if params['name'] == 'admin' && params['password'] == 'admin'
|
2656
|
+
session['user_name'] = params['name']
|
2633
2657
|
else
|
2634
2658
|
redirect '/login'
|
2635
2659
|
end
|
@@ -2752,8 +2776,8 @@ class MyApp < Sinatra::Base
|
|
2752
2776
|
# Request scope for '/define_route/:name'
|
2753
2777
|
@value = 42
|
2754
2778
|
|
2755
|
-
settings.get("/#{params[
|
2756
|
-
# Request scope for "/#{params[
|
2779
|
+
settings.get("/#{params['name']}") do
|
2780
|
+
# Request scope for "/#{params['name']}"
|
2757
2781
|
@value # => nil (not the same request)
|
2758
2782
|
end
|
2759
2783
|
|
@@ -2831,9 +2855,9 @@ The following Ruby versions are officially supported:
|
|
2831
2855
|
until the release of Sinatra 2.0.
|
2832
2856
|
</dd>
|
2833
2857
|
|
2834
|
-
<dt>Ruby 2.
|
2858
|
+
<dt>Ruby 2.x</dt>
|
2835
2859
|
<dd>
|
2836
|
-
2.
|
2860
|
+
2.x is fully supported and recommended. There are currently no plans to drop
|
2837
2861
|
official support for it.
|
2838
2862
|
</dd>
|
2839
2863
|
|
@@ -2864,9 +2888,9 @@ known to run Sinatra:
|
|
2864
2888
|
Not being officially supported means if things only break there and not on a
|
2865
2889
|
supported platform, we assume it's not our issue but theirs.
|
2866
2890
|
|
2867
|
-
We also run our CI against ruby-head (
|
2868
|
-
guarantee anything, since it is constantly moving. Expect 2.
|
2869
|
-
supported.
|
2891
|
+
We also run our CI against ruby-head (future releases of MRI), but we can't
|
2892
|
+
guarantee anything, since it is constantly moving. Expect upcoming 2.x releases
|
2893
|
+
to be fully supported.
|
2870
2894
|
|
2871
2895
|
Sinatra should work on any operating system supported by the chosen Ruby
|
2872
2896
|
implementation.
|
@@ -2971,7 +2995,7 @@ SemVerTag.
|
|
2971
2995
|
* [Twitter](http://twitter.com/sinatra)
|
2972
2996
|
* [Mailing List](http://groups.google.com/group/sinatrarb/topics)
|
2973
2997
|
* IRC: [#sinatra](irc://chat.freenode.net/#sinatra) on http://freenode.net
|
2974
|
-
* [Sinatra Book](
|
2998
|
+
* [Sinatra Book](https://github.com/sinatra/sinatra-book/) Cookbook Tutorial
|
2975
2999
|
* [Sinatra Recipes](http://recipes.sinatrarb.com/) Community
|
2976
3000
|
contributed recipes
|
2977
3001
|
* API documentation for the [latest release](http://rubydoc.info/gems/sinatra)
|