sinatra 1.3.2 → 1.3.3

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.

@@ -128,7 +128,7 @@ A gem/biblioteca haml é necessária para renderizar templates HAML:
128
128
 
129
129
  Renderiza <tt>./views/index.haml</tt>.
130
130
 
131
- {Opções Haml}[http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#options]
131
+ {Opções Haml}[http://haml.info/docs/yardoc/file.HAML_REFERENCE.html#options]
132
132
  podem ser setadas globalmente através das configurações do sinatra,
133
133
  veja {Opções e Configurações}[http://www.sinatrarb.com/configuration.html],
134
134
  e substitua em uma requisição individual.
@@ -128,7 +128,7 @@ A gem/biblioteca haml é necessária para renderizar templates HAML:
128
128
 
129
129
  Renderiza <tt>./views/index.haml</tt>.
130
130
 
131
- {Opções Haml}[http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#options]
131
+ {Opções Haml}[http://haml.info/docs/yardoc/file.HAML_REFERENCE.html#options]
132
132
  podem ser definidas globalmente através das configurações do sinatra,
133
133
  veja {Opções e Configurações}[http://www.sinatrarb.com/configuration.html],
134
134
  e substitua em uma requisição individual.
@@ -5,7 +5,7 @@ effort:
5
5
 
6
6
  # myapp.rb
7
7
  require 'sinatra'
8
-
8
+
9
9
  get '/' do
10
10
  'Hello world!'
11
11
  end
@@ -128,7 +128,7 @@ Other available conditions are +host_name+ and +provides+:
128
128
  get '/', :provides => 'html' do
129
129
  haml :index
130
130
  end
131
-
131
+
132
132
  get '/', :provides => ['rss', 'atom', 'xml'] do
133
133
  builder :feed
134
134
  end
@@ -136,21 +136,21 @@ Other available conditions are +host_name+ and +provides+:
136
136
  You can easily define your own conditions:
137
137
 
138
138
  set(:probability) { |value| condition { rand <= value } }
139
-
139
+
140
140
  get '/win_a_car', :probability => 0.1 do
141
141
  "You won!"
142
142
  end
143
-
143
+
144
144
  get '/win_a_car' do
145
145
  "Sorry, you lost."
146
146
  end
147
-
147
+
148
148
  For a condition that takes multiple values use a splat:
149
149
 
150
150
  set(:auth) do |*roles| # <- notice the splat here
151
151
  condition do
152
152
  unless logged_in? && roles.any? {|role| current_user.in_role? role }
153
- redirect "/login/", 303
153
+ redirect "/login/", 303
154
154
  end
155
155
  end
156
156
  end
@@ -158,11 +158,11 @@ For a condition that takes multiple values use a splat:
158
158
  get "/my/account/", :auth => [:user, :admin] do
159
159
  "Your Account Details"
160
160
  end
161
-
161
+
162
162
  get "/only/admin/", :auth => :admin do
163
163
  "Only admins are allowed here!"
164
164
  end
165
-
165
+
166
166
  === Return Values
167
167
 
168
168
  The return value of a route block determines at least the response body passed
@@ -177,7 +177,7 @@ body object or HTTP status code:
177
177
  body (responds to #each)]</tt>
178
178
  * An Array with two elements: <tt>[status (Fixnum), response body (responds to
179
179
  #each)]</tt>
180
- * An object that responds to <tt>#each</tt> and passes nothing but strings to
180
+ * An object that responds to <tt>#each</tt> and passes nothing but strings to
181
181
  the given block
182
182
  * A Fixnum representing the status code
183
183
 
@@ -251,7 +251,7 @@ Use the <tt>:static_cache_control</tt> setting (see below) to add
251
251
 
252
252
  == Views / Templates
253
253
 
254
- Each template language is exposed as via its own rendering method. These
254
+ Each template language is exposed via its own rendering method. These
255
255
  methods simply return a string:
256
256
 
257
257
  get '/' do
@@ -344,7 +344,7 @@ to use (and to be thread-safe), you should simply require it first:
344
344
 
345
345
  === Haml Templates
346
346
 
347
- Dependency:: {haml}[http://haml-lang.com/]
347
+ Dependency:: {haml}[http://haml.info/]
348
348
  File Extensions:: <tt>.haml</tt>
349
349
  Example:: <tt>haml :index, :format => :html5</tt>
350
350
 
@@ -559,7 +559,7 @@ Templates may be defined at the end of the source file:
559
559
  = yield
560
560
 
561
561
  @@ index
562
- %div.title Hello world!!!!!
562
+ %div.title Hello world.
563
563
 
564
564
  NOTE: Inline templates defined in the source file that requires sinatra are
565
565
  automatically loaded. Call <tt>enable :inline_templates</tt> explicitly if you
@@ -582,8 +582,8 @@ Templates may also be defined using the top-level <tt>template</tt> method:
582
582
  end
583
583
 
584
584
  If a template named "layout" exists, it will be used each time a template
585
- is rendered. You can individually disable layouts by passing
586
- <tt>:layout => false</tt> or disable them by default via
585
+ is rendered. You can individually disable layouts by passing
586
+ <tt>:layout => false</tt> or disable them by default via
587
587
  <tt>set :haml, :layout => false</tt>:
588
588
 
589
589
  get '/' do
@@ -659,7 +659,7 @@ Like routes, filters also take conditions:
659
659
  before :agent => /Songbird/ do
660
660
  # ...
661
661
  end
662
-
662
+
663
663
  after '/blog/*', :host_name => 'example.com' do
664
664
  # ...
665
665
  end
@@ -679,6 +679,20 @@ route handlers and templates:
679
679
  bar(params[:name])
680
680
  end
681
681
 
682
+ Alternatively, helper methods can be separately defined in a module:
683
+
684
+ module FooUtils
685
+ def foo(name) "#{name}foo" end
686
+ end
687
+
688
+ module BarUtils
689
+ def bar(name) "#{name}bar" end
690
+ end
691
+
692
+ helpers FooUtils, BarUtils
693
+
694
+ The effect is the same as including the modules in the application class.
695
+
682
696
  === Using Sessions
683
697
 
684
698
  A session is used to keep state during requests. If activated, you have one
@@ -711,7 +725,7 @@ middleware of choice as you would any other middleware:
711
725
  end
712
726
 
713
727
  To improve security, the session data in the cookie is signed with a session
714
- secret. A random secret is generate for you by Sinatra. However, since this
728
+ secret. A random secret is generated for you by Sinatra. However, since this
715
729
  secret will change with every start of your application, you might want to
716
730
  set the secret yourself, so all your application instances share it:
717
731
 
@@ -798,12 +812,12 @@ access the body:
798
812
  get '/foo' do
799
813
  body "bar"
800
814
  end
801
-
815
+
802
816
  after do
803
817
  puts body
804
818
  end
805
819
 
806
- It is also possible to pass a block to +body+, which will be executed by the
820
+ It is also possible to pass a block to +body+, which will be executed by the
807
821
  Rack handler (this can be used to implement streaming, see "Return Values").
808
822
 
809
823
  Similar to the body, you can also set the status code and headers:
@@ -838,15 +852,15 @@ creating your own wrapper:
838
852
 
839
853
  This allows you to implement streaming APIs,
840
854
  {Server Sent Events}[http://dev.w3.org/html5/eventsource/] and can be used as
841
- basis for {WebSockets}[http://en.wikipedia.org/wiki/WebSocket]. It can also be
855
+ the basis for {WebSockets}[http://en.wikipedia.org/wiki/WebSocket]. It can also be
842
856
  used to increase throughput if some but not all content depends on a slow
843
857
  resource.
844
858
 
845
- Note that the streaming behavior, especially the number of concurrent request,
859
+ Note that the streaming behavior, especially the number of concurrent requests,
846
860
  highly depends on the web server used to serve the application. Some servers,
847
861
  like WEBRick, might not even support streaming at all. If the server does not
848
862
  support streaming, the body will be sent all at once after the block passed to
849
- +stream+ finished executing. Streaming does not work at all with Shotgun.
863
+ +stream+ finishes executing. Streaming does not work at all with Shotgun.
850
864
 
851
865
  If the optional parameter is set to +keep_open+, it will not call +close+ on
852
866
  the stream object, allowing you to close it at any later point in the
@@ -954,12 +968,12 @@ To pass arguments with a redirect, either add them to the query:
954
968
  Or use a session:
955
969
 
956
970
  enable :sessions
957
-
971
+
958
972
  get '/foo' do
959
973
  session[:secret] = 'foo'
960
974
  redirect to('/bar')
961
975
  end
962
-
976
+
963
977
  get '/bar' do
964
978
  session[:secret]
965
979
  end
@@ -1006,14 +1020,14 @@ It is also possible to use a
1006
1020
  etag @article.sha1, :weak
1007
1021
 
1008
1022
  These helpers will not do any caching for you, but rather feed the necessary
1009
- information to your cache. If you are looking for a quick reverse-proxy caching solution,
1010
- try {rack-cache}[http://rtomayko.github.com/rack-cache/]:
1023
+ information to your cache. If you are looking for a quick reverse-proxy caching
1024
+ solution, try {rack-cache}[http://rtomayko.github.com/rack-cache/]:
1011
1025
 
1012
1026
  require "rack/cache"
1013
1027
  require "sinatra"
1014
-
1028
+
1015
1029
  use Rack::Cache
1016
-
1030
+
1017
1031
  get '/' do
1018
1032
  cache_control :public, :max_age => 36000
1019
1033
  sleep 5
@@ -1114,7 +1128,7 @@ Some options, like <tt>script_name</tt> or <tt>path_info</tt>, can also be
1114
1128
  written:
1115
1129
 
1116
1130
  before { request.path_info = "/" }
1117
-
1131
+
1118
1132
  get "/" do
1119
1133
  "all requests end up here"
1120
1134
  end
@@ -1223,16 +1237,16 @@ Run once, at startup, in any environment:
1223
1237
  configure do
1224
1238
  # setting one option
1225
1239
  set :option, 'value'
1226
-
1240
+
1227
1241
  # setting multiple options
1228
1242
  set :a => 1, :b => 2
1229
-
1243
+
1230
1244
  # same as `set :option, true`
1231
1245
  enable :option
1232
-
1246
+
1233
1247
  # same as `set :option, false`
1234
1248
  disable :option
1235
-
1249
+
1236
1250
  # you can also have dynamic settings with blocks
1237
1251
  set(:css_dir) { File.join(views, 'css') }
1238
1252
  end
@@ -1268,7 +1282,8 @@ You can access those options via <tt>settings</tt>:
1268
1282
  Sinatra is using
1269
1283
  {Rack::Protection}[https://github.com/rkh/rack-protection#readme] to defend
1270
1284
  you application against common, opportunistic attacks. You can easily disable
1271
- this behavior (which should result in performance gains):
1285
+ this behavior (which will open your application to tons of common
1286
+ vulnerabilities):
1272
1287
 
1273
1288
  disable :protection
1274
1289
 
@@ -1300,8 +1315,8 @@ You can also hand in an array in order to disable a list of protections:
1300
1315
 
1301
1316
  settings.add_charsets << "application/foobar"
1302
1317
 
1303
- [app_file] Path to the main application file, used to detect project root,
1304
- views and public folder and inline templates.
1318
+ [app_file] Path to the main application file, used to detect project
1319
+ root, views and public folder and inline templates.
1305
1320
 
1306
1321
  [bind] IP address to bind to (default: 0.0.0.0).
1307
1322
  Only used for built-in server.
@@ -1347,8 +1362,8 @@ You can also hand in an array in order to disable a list of protections:
1347
1362
  setting if not set.
1348
1363
 
1349
1364
  [raise_errors] raise exceptions (will stop application). Enabled
1350
- by default when <tt>environment</tt> is set to <tt>"test"</tt>,
1351
- disabled otherwise.
1365
+ by default when <tt>environment</tt> is set to
1366
+ <tt>"test"</tt>, disabled otherwise.
1352
1367
 
1353
1368
  [run] if enabled, Sinatra will handle starting the web server,
1354
1369
  do not enable if using rackup or other means.
@@ -1367,6 +1382,9 @@ You can also hand in an array in order to disable a list of protections:
1367
1382
  [show_exceptions] show a stack trace in the browser when an exception
1368
1383
  happens. Enabled by default when <tt>environment</tt>
1369
1384
  is set to <tt>"development"</tt>, disabled otherwise.
1385
+ Can also be set to <tt>:after_handler</tt> to trigger
1386
+ app-specified error handling before showing a stack
1387
+ trace in the browser.
1370
1388
 
1371
1389
  [static] Whether Sinatra should handle serving static files.
1372
1390
  Disable when using a Server able to do this on its own.
@@ -1388,15 +1406,20 @@ You can also hand in an array in order to disable a list of protections:
1388
1406
 
1389
1407
  == Environments
1390
1408
 
1391
- There are three predefined +environments+: <tt>development</tt>, <tt>production</tt> and <tt>test</tt>. Environment can be set by RACK_ENV environment variable, and default value is <tt>development</tt>.
1409
+ There are three predefined +environments+: <tt>"development"</tt>,
1410
+ <tt>"production"</tt> and <tt>"test"</tt>. Environments can be set
1411
+ through the +RACK_ENV+ environment variable. The default value is
1412
+ <tt>"development"</tt>. In this mode, all templates are reloaded between
1413
+ requests. Special <tt>not_found</tt> and <tt>error</tt> handlers are installed
1414
+ for this environment so you will see a stack trace in your browser.
1415
+ In <tt>"production"</tt> and <tt>"test"</tt> templates are cached by default.
1392
1416
 
1393
- You can also run different environemnt using <tt>-e</tt> option:
1417
+ To run different environments use the <tt>-e</tt> option:
1394
1418
 
1395
1419
  ruby my_app.rb -e [ENVIRONMENT]
1396
1420
 
1397
- You can use predefinied methods: +development?+, +test?+ and +production?+, to check which enviroment is set.
1398
-
1399
- +Developemnt+ is default setting. In this mode, all templates are being reloaded between requests. Special <tt>not_found</tt> and <tt>error</tt> handlers are installed for this enviroment, so you will see nice error page. In +production+ and +test+ templates are being cached.
1421
+ You can use predefined methods: +development?+, +test?+ and +production?+ to
1422
+ check which enviroment is currently set.
1400
1423
 
1401
1424
  == Error Handling
1402
1425
 
@@ -1501,8 +1524,8 @@ with {CodeRack}[http://coderack.org/] or in the
1501
1524
 
1502
1525
  == Testing
1503
1526
 
1504
- Sinatra tests can be written using any Rack-based testing library
1505
- or framework. {Rack::Test}[http://rdoc.info/github/brynary/rack-test/master/frames]
1527
+ Sinatra tests can be written using any Rack-based testing library or framework.
1528
+ {Rack::Test}[http://rdoc.info/github/brynary/rack-test/master/frames]
1506
1529
  is recommended:
1507
1530
 
1508
1531
  require 'my_sinatra_app'
@@ -1564,7 +1587,8 @@ available via the top-level DSL. Most top-level apps can be converted to
1564
1587
  of <tt>Sinatra::Base</tt>.
1565
1588
 
1566
1589
  <tt>Sinatra::Base</tt> is a blank slate. Most options are disabled by default,
1567
- including the built-in server. See {Options and Configuration}[http://sinatra.github.com/configuration.html]
1590
+ including the built-in server. See
1591
+ {Options and Configuration}[http://sinatra.github.com/configuration.html]
1568
1592
  for details on available options and their behavior.
1569
1593
 
1570
1594
  === Modular vs. Classic Style
@@ -1602,10 +1626,10 @@ There are two common options for starting a modular app, actively starting with
1602
1626
 
1603
1627
  # my_app.rb
1604
1628
  require 'sinatra/base'
1605
-
1629
+
1606
1630
  class MyApp < Sinatra::Base
1607
1631
  # ... app code here ...
1608
-
1632
+
1609
1633
  # start the server if ruby file executed directly
1610
1634
  run! if app_file == $0
1611
1635
  end
@@ -1630,7 +1654,7 @@ Write your app file:
1630
1654
 
1631
1655
  # app.rb
1632
1656
  require 'sinatra'
1633
-
1657
+
1634
1658
  get '/' do
1635
1659
  'Hello world!'
1636
1660
  end
@@ -1661,12 +1685,12 @@ endpoint could be another Sinatra application, or any other Rack-based
1661
1685
  application (Rails/Ramaze/Camping/...):
1662
1686
 
1663
1687
  require 'sinatra/base'
1664
-
1688
+
1665
1689
  class LoginScreen < Sinatra::Base
1666
1690
  enable :sessions
1667
-
1691
+
1668
1692
  get('/login') { haml :login }
1669
-
1693
+
1670
1694
  post('/login') do
1671
1695
  if params[:name] == 'admin' && params[:password] == 'admin'
1672
1696
  session['user_name'] = params[:name]
@@ -1675,17 +1699,17 @@ application (Rails/Ramaze/Camping/...):
1675
1699
  end
1676
1700
  end
1677
1701
  end
1678
-
1702
+
1679
1703
  class MyApp < Sinatra::Base
1680
1704
  # middleware will run before filters
1681
1705
  use LoginScreen
1682
-
1706
+
1683
1707
  before do
1684
1708
  unless session['user_name']
1685
1709
  halt "Access denied, please <a href='/login'>login</a>."
1686
1710
  end
1687
1711
  end
1688
-
1712
+
1689
1713
  get('/') { "Hello #{session['user_name']}." }
1690
1714
  end
1691
1715
 
@@ -1737,10 +1761,10 @@ available.
1737
1761
  === Application/Class Scope
1738
1762
 
1739
1763
  Every Sinatra application corresponds to a subclass of <tt>Sinatra::Base</tt>.
1740
- If you are using the top-level DSL (<tt>require 'sinatra'</tt>), then this
1741
- class is <tt>Sinatra::Application</tt>, otherwise it is the subclass you
1764
+ If you are using the top-level DSL (<tt>require 'sinatra'</tt>), then this
1765
+ class is <tt>Sinatra::Application</tt>, otherwise it is the subclass you
1742
1766
  created explicitly. At class level you have methods like +get+ or +before+, but
1743
- you cannot access the +request+ object or the +session+, as there only is a
1767
+ you cannot access the +request+ object or the +session+, as there only is a
1744
1768
  single application class for all requests.
1745
1769
 
1746
1770
  Options created via +set+ are methods at class level:
@@ -1749,7 +1773,7 @@ Options created via +set+ are methods at class level:
1749
1773
  # Hey, I'm in the application scope!
1750
1774
  set :foo, 42
1751
1775
  foo # => 42
1752
-
1776
+
1753
1777
  get '/foo' do
1754
1778
  # Hey, I'm no longer in the application scope!
1755
1779
  end
@@ -1781,12 +1805,12 @@ scope via the +settings+ helper:
1781
1805
  get '/define_route/:name' do
1782
1806
  # Request scope for '/define_route/:name'
1783
1807
  @value = 42
1784
-
1808
+
1785
1809
  settings.get("/#{params[:name]}") do
1786
1810
  # Request scope for "/#{params[:name]}"
1787
1811
  @value # => nil (not the same request)
1788
1812
  end
1789
-
1813
+
1790
1814
  "Route defined!"
1791
1815
  end
1792
1816
  end
@@ -1845,28 +1869,27 @@ The following Ruby versions are officially supported:
1845
1869
  Sinatra 1.4.0 is released.
1846
1870
 
1847
1871
  [ Ruby 1.9.2 ]
1848
- 1.9.2 is fully supported and recommended. Note that Radius and Markaby
1849
- are currently not 1.9 compatible. Do not use 1.9.2p0, it is known to cause
1850
- segmentation faults when running Sinatra. Support will continue at least
1872
+ 1.9.2 is fully supported and recommended. Do not use 1.9.2p0, it is known to
1873
+ cause segmentation faults when running Sinatra. Support will continue at least
1851
1874
  until the release of Ruby 1.9.4/2.0 and support for the latest 1.9 release
1852
1875
  will continue as long as it is still supported by the Ruby core team.
1853
1876
 
1854
1877
  [ Ruby 1.9.3 ]
1855
- 1.9.3 is fully supported. We recommend waiting for higher patch levels to be
1856
- released (current one is p0) before using it in production. Please note that
1857
- switching to 1.9.3 from an earlier version will invalidate all sessions.
1878
+ 1.9.3 is fully supported and recommended. Please note that switching to 1.9.3
1879
+ from an earlier version will invalidate all sessions.
1858
1880
 
1859
1881
  [ Rubinius ]
1860
1882
  Rubinius is officially supported (Rubinius >= 1.2.4), everything, including
1861
1883
  all template languages, works. The upcoming 2.0 release is supported as
1862
- well.
1884
+ well, including 1.9 mode.
1863
1885
 
1864
1886
  [ JRuby ]
1865
- JRuby is officially supported (JRuby >= 1.6.5). No issues with third party
1887
+ JRuby is officially supported (JRuby >= 1.6.7). No issues with third party
1866
1888
  template libraries are known, however, if you choose to use JRuby, please
1867
1889
  look into JRuby rack handlers, as the Thin web server is not fully supported
1868
1890
  on JRuby. JRuby's support for C extensions is still experimental, which only
1869
- affects RDiscount, Redcarpet and RedCloth at the moment.
1891
+ affects RDiscount, Redcarpet, RedCloth and Yajl templates as well as Thin
1892
+ and Mongrel at the moment.
1870
1893
 
1871
1894
  We also keep an eye on upcoming Ruby versions.
1872
1895
 
@@ -1915,7 +1938,7 @@ Then, in your project directory, create a +Gemfile+:
1915
1938
 
1916
1939
  source :rubygems
1917
1940
  gem 'sinatra', :git => "git://github.com/sinatra/sinatra.git"
1918
-
1941
+
1919
1942
  # other dependencies
1920
1943
  gem 'haml' # for instance, if you use haml
1921
1944
  gem 'activerecord', '~> 3.0' # maybe you also need ActiveRecord 3.x
@@ -1971,7 +1994,7 @@ SemVerTag.
1971
1994
  * {Mailing List}[http://groups.google.com/group/sinatrarb/topics]
1972
1995
  * {IRC: #sinatra}[irc://chat.freenode.net/#sinatra] on http://freenode.net
1973
1996
  * {Sinatra Book}[http://sinatra-book.gittr.com] Cookbook Tutorial
1974
- * {Sinatra Recipes}[http://recipes.sinatrarb.com/] Community
1997
+ * {Sinatra Recipes}[http://recipes.sinatrarb.com/] Community
1975
1998
  contributed recipes
1976
1999
  * API documentation for the {latest release}[http://rubydoc.info/gems/sinatra]
1977
2000
  or the {current HEAD}[http://rubydoc.info/github/sinatra/sinatra] on