sinatra 1.2.0.c → 1.2.0.d
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.
- data/AUTHORS +10 -1
- data/Gemfile +24 -6
- data/README.de.rdoc +696 -72
- data/README.es.rdoc +545 -61
- data/README.fr.rdoc +2 -2
- data/README.jp.rdoc +2 -2
- data/README.rdoc +281 -74
- data/README.ru.rdoc +2 -2
- data/README.zh.rdoc +2 -2
- data/Rakefile +10 -7
- data/lib/sinatra/base.rb +48 -20
- data/sinatra.gemspec +4 -5
- data/test/encoding_test.rb +4 -3
- data/test/helper.rb +2 -0
- data/test/helpers_test.rb +48 -13
- data/test/rdoc_test.rb +1 -1
- data/test/routing_test.rb +5 -0
- data/test/views/{ascii.haml → ascii.erb} +1 -1
- data/test/views/{utf8.haml → utf8.erb} +1 -1
- metadata +6 -7
- data/Gemfile.lock +0 -74
data/README.fr.rdoc
CHANGED
@@ -374,8 +374,8 @@ Notez que vous pouvez également appeler la méthode textile au sein d'autres te
|
|
374
374
|
|
375
375
|
Le gem RDoc est nécessaire pour utiliser la fonction de rendu RDoc:
|
376
376
|
|
377
|
-
# Chargez la bibliothèque rdoc dans votre application
|
378
|
-
require "rdoc"
|
377
|
+
# Chargez la bibliothèque rdoc/markup/to_html dans votre application
|
378
|
+
require "rdoc/markup/to_html"
|
379
379
|
|
380
380
|
get '/' do
|
381
381
|
rdoc :index
|
data/README.jp.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -17,9 +17,12 @@ Install the gem and run with:
|
|
17
17
|
|
18
18
|
View at: http://localhost:4567
|
19
19
|
|
20
|
+
It is recommended to also run <tt>gem install thin</tt>, which Sinatra will
|
21
|
+
pick up if available.
|
22
|
+
|
20
23
|
== Routes
|
21
24
|
|
22
|
-
In Sinatra, a route is an HTTP method paired with
|
25
|
+
In Sinatra, a route is an HTTP method paired with a URL-matching pattern.
|
23
26
|
Each route is associated with a block:
|
24
27
|
|
25
28
|
get '/' do
|
@@ -127,7 +130,7 @@ You can easily define your own conditions:
|
|
127
130
|
|
128
131
|
The return value of a route block determines at least the response body passed
|
129
132
|
on to the HTTP client, or at least the next middleware in the Rack stack.
|
130
|
-
Most commonly this is a string, as in the above examples. But other values are
|
133
|
+
Most commonly, this is a string, as in the above examples. But other values are
|
131
134
|
also accepted.
|
132
135
|
|
133
136
|
You can return any object that would either be a valid Rack response, Rack
|
@@ -138,7 +141,7 @@ body object or HTTP status code:
|
|
138
141
|
* An object that responds to <tt>#each</tt> and passes nothing but strings to the given block
|
139
142
|
* A Fixnum representing the status code
|
140
143
|
|
141
|
-
That way we can for instance easily implement a streaming example:
|
144
|
+
That way we can, for instance, easily implement a streaming example:
|
142
145
|
|
143
146
|
class Stream
|
144
147
|
def each
|
@@ -148,6 +151,47 @@ That way we can for instance easily implement a streaming example:
|
|
148
151
|
|
149
152
|
get('/') { Stream.new }
|
150
153
|
|
154
|
+
=== Custom Route Matchers
|
155
|
+
|
156
|
+
As shown above, Sinatra ships with built-in support for using String patterns
|
157
|
+
and regular expressions as route matches. However, it does not stop there. You
|
158
|
+
can easily define your own matchers:
|
159
|
+
|
160
|
+
class AllButPattern
|
161
|
+
Match = Struct.new(:captures)
|
162
|
+
|
163
|
+
def initialize(except)
|
164
|
+
@except = except
|
165
|
+
@caputres = Match.new([])
|
166
|
+
end
|
167
|
+
|
168
|
+
def match(str)
|
169
|
+
@caputres unless @except === str
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def all_but(pattern)
|
174
|
+
AllButPattern.new(pattern)
|
175
|
+
end
|
176
|
+
|
177
|
+
get all_but("/index") do
|
178
|
+
# ...
|
179
|
+
end
|
180
|
+
|
181
|
+
Note that the above example might be over-engineered, as it can also be
|
182
|
+
expressed as:
|
183
|
+
|
184
|
+
get // do
|
185
|
+
pass if request.path_info == "/index"
|
186
|
+
# ...
|
187
|
+
end
|
188
|
+
|
189
|
+
Or, using negative look ahead:
|
190
|
+
|
191
|
+
get %r{^(?!/index$)} do
|
192
|
+
# ...
|
193
|
+
end
|
194
|
+
|
151
195
|
== Static Files
|
152
196
|
|
153
197
|
Static files are served from the <tt>./public</tt> directory. You can specify
|
@@ -168,8 +212,8 @@ directory. To use a different views directory:
|
|
168
212
|
|
169
213
|
One important thing to remember is that you always have to reference
|
170
214
|
templates with symbols, even if they're in a subdirectory (in this
|
171
|
-
case use <tt>:'subdir/template'</tt>). You must use a symbol because
|
172
|
-
otherwise rendering methods will render any strings passed to them
|
215
|
+
case, use <tt>:'subdir/template'</tt>). You must use a symbol because
|
216
|
+
otherwise rendering methods will render any strings passed to them
|
173
217
|
directly.
|
174
218
|
|
175
219
|
=== Haml Templates
|
@@ -271,7 +315,7 @@ The <tt>haml</tt> or <tt>sass</tt> gem/library is required to render Sass templa
|
|
271
315
|
|
272
316
|
Renders <tt>./views/stylesheet.sass</tt>.
|
273
317
|
|
274
|
-
{Sass' options}[http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options]
|
318
|
+
{Sass's options}[http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options]
|
275
319
|
can be set globally through Sinatra's configurations,
|
276
320
|
see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
277
321
|
and overridden on an individual basis.
|
@@ -295,7 +339,7 @@ The <tt>haml</tt> or <tt>sass</tt> gem/library is required to render Scss templa
|
|
295
339
|
|
296
340
|
Renders <tt>./views/stylesheet.scss</tt>.
|
297
341
|
|
298
|
-
{Scss' options}[http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options]
|
342
|
+
{Scss's options}[http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options]
|
299
343
|
can be set globally through Sinatra's configurations,
|
300
344
|
see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
301
345
|
and overridden on an individual basis.
|
@@ -447,8 +491,8 @@ template) with <tt>./views/post.haml</tt> as layout.
|
|
447
491
|
|
448
492
|
The <tt>rdoc</tt> gem/library is required to render RDoc templates:
|
449
493
|
|
450
|
-
# You'll need to require rdoc in your app
|
451
|
-
require "rdoc"
|
494
|
+
# You'll need to require rdoc/markup/to_html in your app
|
495
|
+
require "rdoc/markup/to_html"
|
452
496
|
|
453
497
|
get '/' do
|
454
498
|
rdoc :index
|
@@ -571,7 +615,7 @@ Renders the embedded template string.
|
|
571
615
|
=== Accessing Variables in Templates
|
572
616
|
|
573
617
|
Templates are evaluated within the same context as route handlers. Instance
|
574
|
-
variables set in route handlers are
|
618
|
+
variables set in route handlers are directly accessible by templates:
|
575
619
|
|
576
620
|
get '/:id' do
|
577
621
|
@foo = Foo.find(params[:id])
|
@@ -643,7 +687,7 @@ To associate a file extension with a template engine, use
|
|
643
687
|
|
644
688
|
Tilt.register :tt, Tilt[:textile]
|
645
689
|
|
646
|
-
=== Adding
|
690
|
+
=== Adding Your Own Template Engine
|
647
691
|
|
648
692
|
First, register your engine with Tilt, then create a rendering method:
|
649
693
|
|
@@ -662,9 +706,9 @@ learn more about Tilt.
|
|
662
706
|
|
663
707
|
== Filters
|
664
708
|
|
665
|
-
Before filters are evaluated before each request within the same
|
666
|
-
the routes will be and can modify the request and response. Instance
|
667
|
-
set in filters are accessible by routes and templates:
|
709
|
+
Before filters are evaluated before each request within the same
|
710
|
+
context as the routes will be and can modify the request and response. Instance
|
711
|
+
variables set in filters are accessible by routes and templates:
|
668
712
|
|
669
713
|
before do
|
670
714
|
@note = 'Hi!'
|
@@ -676,7 +720,7 @@ set in filters are accessible by routes and templates:
|
|
676
720
|
params[:splat] #=> 'bar/baz'
|
677
721
|
end
|
678
722
|
|
679
|
-
After
|
723
|
+
After filters are evaluated after each request within the same context and can
|
680
724
|
also modify the request and response. Instance variables set in before filters
|
681
725
|
and routes are accessible by after filters:
|
682
726
|
|
@@ -688,7 +732,7 @@ Note: Unless you use the +body+ method rather than just returning a String from
|
|
688
732
|
the routes, the body will not yet be available in the after filter, since it is
|
689
733
|
generated later on.
|
690
734
|
|
691
|
-
Filters optionally
|
735
|
+
Filters optionally take a pattern, causing them to be evaluated only if the
|
692
736
|
request path matches that pattern:
|
693
737
|
|
694
738
|
before '/protected/*' do
|
@@ -724,6 +768,37 @@ route handlers and templates:
|
|
724
768
|
bar(params[:name])
|
725
769
|
end
|
726
770
|
|
771
|
+
=== Using Sessions
|
772
|
+
|
773
|
+
A session is used to keep state during requests. If activated, you have one
|
774
|
+
session hash per user session:
|
775
|
+
|
776
|
+
enable :sessions
|
777
|
+
|
778
|
+
get '/' do
|
779
|
+
"value = " << session[:value].inspect
|
780
|
+
end
|
781
|
+
|
782
|
+
get '/:value' do
|
783
|
+
session[:value] = params[:value]
|
784
|
+
end
|
785
|
+
|
786
|
+
Note that <tt>enable :sessions</tt> actually stores all data in a cookie. This
|
787
|
+
might not always be what you want (storing lots of data will increase your
|
788
|
+
traffic, for instance). You can use any Rack session middleware, in order to
|
789
|
+
do so, do *not* call <tt>enable :sessions</tt>, but instead pull in your
|
790
|
+
middleware of choice how you would any other middleware:
|
791
|
+
|
792
|
+
use Rack::Session::Pool, :expire_after => 2592000
|
793
|
+
|
794
|
+
get '/' do
|
795
|
+
"value = " << session[:value].inspect
|
796
|
+
end
|
797
|
+
|
798
|
+
get '/:value' do
|
799
|
+
session[:value] = params[:value]
|
800
|
+
end
|
801
|
+
|
727
802
|
=== Halting
|
728
803
|
|
729
804
|
To immediately stop a request within a filter or route use:
|
@@ -777,19 +852,19 @@ of calling another route. Simply use +call+ to achieve this:
|
|
777
852
|
end
|
778
853
|
|
779
854
|
Note that in the example above, you would ease testing and increase performance
|
780
|
-
by simply moving <tt>"bar"</tt> into a helper used by both <tt>/foo</tt>
|
781
|
-
<tt>/bar</tt>.
|
855
|
+
by simply moving <tt>"bar"</tt> into a helper used by both <tt>/foo</tt>
|
856
|
+
and <tt>/bar</tt>.
|
782
857
|
|
783
858
|
If you want the request to be sent to the same application instance rather than
|
784
859
|
a duplicate, use <tt>call!</tt> instead of <tt>call</tt>.
|
785
860
|
|
786
861
|
Check out the Rack specification if you want to learn more about <tt>call</tt>.
|
787
862
|
|
788
|
-
=== Setting Body
|
863
|
+
=== Setting Body, Status Code and Headers
|
789
864
|
|
790
865
|
It is possible and recommended to set the status code and response body with the
|
791
866
|
return value of the route block. However, in some scenarios you might want to
|
792
|
-
set the body at an
|
867
|
+
set the body at an arbitrary point in the execution flow. You can do so with the
|
793
868
|
+body+ helper method. If you do so, you can use that method from there on to
|
794
869
|
access the body:
|
795
870
|
|
@@ -801,15 +876,34 @@ access the body:
|
|
801
876
|
puts body
|
802
877
|
end
|
803
878
|
|
804
|
-
It is also possible to pass a block to body
|
805
|
-
handler (this can be used to implement streaming, see
|
806
|
-
"Return Values").
|
879
|
+
It is also possible to pass a block to +body+, which will be executed by the Rack
|
880
|
+
handler (this can be used to implement streaming, see "Return Values").
|
807
881
|
|
808
|
-
Similar to the body, you can also set the status code:
|
882
|
+
Similar to the body, you can also set the status code and headers:
|
809
883
|
|
810
884
|
get '/foo' do
|
811
885
|
status 418
|
812
|
-
|
886
|
+
headers \
|
887
|
+
"Allow" => "BREW, POST, GET, PROPFIND, WHEN"
|
888
|
+
"Refresh" => "Refresh: 20; http://www.ietf.org/rfc/rfc2324.txt"
|
889
|
+
body "I'm a tea pot!"
|
890
|
+
end
|
891
|
+
|
892
|
+
Like +body+, +headers+ and +status+ with no arguments can be used to access
|
893
|
+
their current values.
|
894
|
+
|
895
|
+
=== Mime Types
|
896
|
+
|
897
|
+
When using <tt>send_file</tt> or static files you may have mime types Sinatra
|
898
|
+
doesn't understand. Use +mime_type+ to register them by file extension:
|
899
|
+
|
900
|
+
mime_type :foo, 'text/foo'
|
901
|
+
|
902
|
+
You can also use it with the +content_type+ helper:
|
903
|
+
|
904
|
+
get '/' do
|
905
|
+
content_type :foo
|
906
|
+
"foo foo foo"
|
813
907
|
end
|
814
908
|
|
815
909
|
=== Generating URLs
|
@@ -821,8 +915,7 @@ Haml:
|
|
821
915
|
|
822
916
|
It takes reverse proxies and Rack routers into account, if present.
|
823
917
|
|
824
|
-
This method is also aliased to +to+ (see below
|
825
|
-
example).
|
918
|
+
This method is also aliased to +to+ (see below for an example).
|
826
919
|
|
827
920
|
=== Browser Redirect
|
828
921
|
|
@@ -866,6 +959,62 @@ Or use a session:
|
|
866
959
|
session[:secret]
|
867
960
|
end
|
868
961
|
|
962
|
+
=== Cache Control
|
963
|
+
|
964
|
+
Setting your headers correctly is the foundation for proper HTTP caching.
|
965
|
+
|
966
|
+
You can easily set the Cache-Control header with like this:
|
967
|
+
|
968
|
+
get '/' do
|
969
|
+
cache_control :public
|
970
|
+
"cache it!"
|
971
|
+
end
|
972
|
+
|
973
|
+
Pro tip: Set up caching in a before filter.
|
974
|
+
|
975
|
+
before do
|
976
|
+
cache_control :public, :must_revalidate, :max_age => 60
|
977
|
+
end
|
978
|
+
|
979
|
+
If you are using the +expires+ helper to set the corresponding header,
|
980
|
+
<tt>Cache-Control</tt> will be set automatically for you:
|
981
|
+
|
982
|
+
before do
|
983
|
+
expires 500, :public, :must_revalidate
|
984
|
+
end
|
985
|
+
|
986
|
+
To properly use caches, you should consider using +etag+ and +last_modified+.
|
987
|
+
It is recommended to call those helpers *before* doing heavy lifting, as they
|
988
|
+
will immediately flush a response if the client already has the current
|
989
|
+
version in its cache.
|
990
|
+
|
991
|
+
get '/article/:id' do
|
992
|
+
@article = Article.find params[:id]
|
993
|
+
last_modified @article.updated_at
|
994
|
+
etag @article.sha1
|
995
|
+
erb :article
|
996
|
+
end
|
997
|
+
|
998
|
+
It is also possible to use a
|
999
|
+
{weak ETag}[http://en.wikipedia.org/wiki/HTTP_ETag#Strong_and_weak_validation]:
|
1000
|
+
|
1001
|
+
etag @article.sha1, :weak
|
1002
|
+
|
1003
|
+
These helpers will not do any caching for you, but rather feed the necessary
|
1004
|
+
information to your cache. If you are looking for a quick caching solutions, try
|
1005
|
+
{rack-cache}[http://rtomayko.github.com/rack-cache/]:
|
1006
|
+
|
1007
|
+
require "rack/cache"
|
1008
|
+
require "sinatra"
|
1009
|
+
|
1010
|
+
use Rack::Cache
|
1011
|
+
|
1012
|
+
get '/' do
|
1013
|
+
cache_control :public, :max_age => 36000
|
1014
|
+
sleep 5
|
1015
|
+
"hello"
|
1016
|
+
end
|
1017
|
+
|
869
1018
|
=== Sending Files
|
870
1019
|
|
871
1020
|
For sending files, you can use the <tt>send_file</tt> helper method:
|
@@ -874,14 +1023,14 @@ For sending files, you can use the <tt>send_file</tt> helper method:
|
|
874
1023
|
send_file 'foo.png'
|
875
1024
|
end
|
876
1025
|
|
877
|
-
It also
|
1026
|
+
It also takes a couple of options:
|
878
1027
|
|
879
1028
|
send_file 'foo.png', :type => :jpg
|
880
1029
|
|
881
1030
|
The options are:
|
882
1031
|
|
883
1032
|
[filename]
|
884
|
-
file name in response, defaults to the real file name.
|
1033
|
+
file name, in response, defaults to the real file name.
|
885
1034
|
|
886
1035
|
[last_modified]
|
887
1036
|
value for Last-Modified header, defaults to the file's mtime.
|
@@ -891,7 +1040,7 @@ The options are:
|
|
891
1040
|
|
892
1041
|
[disposition]
|
893
1042
|
used for Content-Disposition, possible values: +nil+ (default),
|
894
|
-
<tt>:
|
1043
|
+
<tt>:attachment</tt> and <tt>:inline</tt>
|
895
1044
|
|
896
1045
|
[length]
|
897
1046
|
Content-Length header, defaults to file size.
|
@@ -920,18 +1069,19 @@ error handlers) through the <tt>request</tt> method:
|
|
920
1069
|
request.get? # true (similar methods for other verbs)
|
921
1070
|
request.form_data? # false
|
922
1071
|
request["SOME_HEADER"] # value of SOME_HEADER header
|
923
|
-
request.
|
1072
|
+
request.referrer # the referrer of the client or '/'
|
924
1073
|
request.user_agent # user agent (used by :agent condition)
|
925
1074
|
request.cookies # hash of browser cookies
|
926
1075
|
request.xhr? # is this an ajax request?
|
927
1076
|
request.url # "http://example.com/example/foo"
|
928
1077
|
request.path # "/example/foo"
|
929
1078
|
request.ip # client IP address
|
930
|
-
request.secure? # false
|
1079
|
+
request.secure? # false (would be true over ssl)
|
1080
|
+
request.forwarded? # true (if running behind a reverse proxy)
|
931
1081
|
request.env # raw env hash handed in by Rack
|
932
1082
|
end
|
933
1083
|
|
934
|
-
Some options, like <tt>script_name</tt> or <tt>path_info</tt
|
1084
|
+
Some options, like <tt>script_name</tt> or <tt>path_info</tt>, can also be
|
935
1085
|
written:
|
936
1086
|
|
937
1087
|
before { request.path_info = "/" }
|
@@ -948,6 +1098,23 @@ The <tt>request.body</tt> is an IO or StringIO object:
|
|
948
1098
|
"Hello #{data['name']}!"
|
949
1099
|
end
|
950
1100
|
|
1101
|
+
=== Attachments
|
1102
|
+
|
1103
|
+
You can use the +attachment+ helper to tell the browser the response should be
|
1104
|
+
stored on disk rather than displayed in the browser.
|
1105
|
+
|
1106
|
+
get '/' do
|
1107
|
+
attachment
|
1108
|
+
"store it!"
|
1109
|
+
end
|
1110
|
+
|
1111
|
+
You can also pass it a file name:
|
1112
|
+
|
1113
|
+
get '/' do
|
1114
|
+
attachment "info.txt"
|
1115
|
+
"store it!"
|
1116
|
+
end
|
1117
|
+
|
951
1118
|
=== Looking Up Template Files
|
952
1119
|
|
953
1120
|
The <tt>find_template</tt> helper is used to find template files for rendering:
|
@@ -1057,20 +1224,20 @@ You can access those options via <tt>settings</tt>:
|
|
1057
1224
|
settings.add_charsets << "application/foobar"
|
1058
1225
|
|
1059
1226
|
[app_file] main application file, used to detect project root,
|
1060
|
-
views and public folder and inline templates
|
1227
|
+
views and public folder and inline templates.
|
1061
1228
|
|
1062
1229
|
[bind] IP address to bind to (default: 0.0.0.0).
|
1063
1230
|
Only used for built-in server.
|
1064
1231
|
|
1065
1232
|
[default_encoding] encoding to assume if unknown
|
1066
|
-
(defaults to <tt>"utf-8"</tt>)
|
1233
|
+
(defaults to <tt>"utf-8"</tt>).
|
1067
1234
|
|
1068
|
-
[dump_errors] display errors in the log
|
1235
|
+
[dump_errors] display errors in the log.
|
1069
1236
|
|
1070
1237
|
[environment] current environment, defaults to <tt>ENV['RACK_ENV']</tt>,
|
1071
|
-
or <tt>"development"</tt> if not available
|
1238
|
+
or <tt>"development"</tt> if not available.
|
1072
1239
|
|
1073
|
-
[logging] use the logger
|
1240
|
+
[logging] use the logger.
|
1074
1241
|
|
1075
1242
|
[lock] Places a lock around every request, only running
|
1076
1243
|
processing on request per Ruby process concurrently.
|
@@ -1079,7 +1246,7 @@ You can access those options via <tt>settings</tt>:
|
|
1079
1246
|
Disabled per default.
|
1080
1247
|
|
1081
1248
|
[method_override] use <tt>_method</tt> magic to allow put/delete forms in
|
1082
|
-
browsers that don't support it
|
1249
|
+
browsers that don't support it.
|
1083
1250
|
|
1084
1251
|
[port] Port to listen on. Only used for built-in server.
|
1085
1252
|
|
@@ -1088,17 +1255,17 @@ You can access those options via <tt>settings</tt>:
|
|
1088
1255
|
<tt>redirect '/foo'</tt> would behave like
|
1089
1256
|
<tt>redirect to('/foo')</tt>. Disabled per default.
|
1090
1257
|
|
1091
|
-
[public] folder public files are
|
1258
|
+
[public] folder public files are served from
|
1092
1259
|
|
1093
1260
|
[reload_templates] whether or not to reload templates between requests.
|
1094
|
-
|
1095
|
-
compensate a bug in Ruby causing a memory leak)
|
1261
|
+
Enabled in development mode and on Ruby 1.8.6 (to
|
1262
|
+
compensate a bug in Ruby causing a memory leak).
|
1096
1263
|
|
1097
|
-
[root] project root folder
|
1264
|
+
[root] project root folder.
|
1098
1265
|
|
1099
|
-
[raise_errors] raise exceptions (will stop application)
|
1266
|
+
[raise_errors] raise exceptions (will stop application).
|
1100
1267
|
|
1101
|
-
[run] if enabled, Sinatra will handle starting the
|
1268
|
+
[run] if enabled, Sinatra will handle starting the web server,
|
1102
1269
|
do not enable if using rackup or other means.
|
1103
1270
|
|
1104
1271
|
[running] is the built-in server running now?
|
@@ -1108,16 +1275,16 @@ You can access those options via <tt>settings</tt>:
|
|
1108
1275
|
defaults to ['thin', 'mongrel', 'webrick'], order indicates
|
1109
1276
|
priority.
|
1110
1277
|
|
1111
|
-
[sessions] enable cookie based sessions
|
1278
|
+
[sessions] enable cookie based sessions.
|
1112
1279
|
|
1113
|
-
[show_exceptions] show a
|
1280
|
+
[show_exceptions] show a stack trace in the browser.
|
1114
1281
|
|
1115
1282
|
[static] Whether Sinatra should handle serving static files.
|
1116
1283
|
Disable when using a Server able to do this on its own.
|
1117
1284
|
Disabling will boost performance.
|
1118
1285
|
Enabled per default.
|
1119
1286
|
|
1120
|
-
[views] views folder
|
1287
|
+
[views] views folder.
|
1121
1288
|
|
1122
1289
|
== Error Handling
|
1123
1290
|
|
@@ -1160,7 +1327,7 @@ You get this:
|
|
1160
1327
|
|
1161
1328
|
So what happened was... something bad
|
1162
1329
|
|
1163
|
-
Alternatively, you can install error handler for a status code:
|
1330
|
+
Alternatively, you can install an error handler for a status code:
|
1164
1331
|
|
1165
1332
|
error 403 do
|
1166
1333
|
'Access forbidden'
|
@@ -1179,17 +1346,6 @@ Or a range:
|
|
1179
1346
|
Sinatra installs special <tt>not_found</tt> and <tt>error</tt> handlers when
|
1180
1347
|
running under the development environment.
|
1181
1348
|
|
1182
|
-
== Mime Types
|
1183
|
-
|
1184
|
-
When using <tt>send_file</tt> or static files you may have mime types Sinatra
|
1185
|
-
doesn't understand. Use +mime_type+ to register them by file extension:
|
1186
|
-
|
1187
|
-
mime_type :foo, 'text/foo'
|
1188
|
-
|
1189
|
-
You can also use it with the +content_type+ helper:
|
1190
|
-
|
1191
|
-
content_type :foo
|
1192
|
-
|
1193
1349
|
== Rack Middleware
|
1194
1350
|
|
1195
1351
|
Sinatra rides on Rack[http://rack.rubyforge.org/], a minimal standard
|
@@ -1222,7 +1378,7 @@ accepts multiple/variable args as well as blocks:
|
|
1222
1378
|
|
1223
1379
|
Rack is distributed with a variety of standard middleware for logging,
|
1224
1380
|
debugging, URL routing, authentication, and session handling. Sinatra uses
|
1225
|
-
many of
|
1381
|
+
many of these components automatically based on configuration so you
|
1226
1382
|
typically don't have to +use+ them explicitly.
|
1227
1383
|
|
1228
1384
|
== Testing
|
@@ -1286,7 +1442,7 @@ The methods available to Sinatra::Base subclasses are exactly as those
|
|
1286
1442
|
available via the top-level DSL. Most top-level apps can be converted to
|
1287
1443
|
Sinatra::Base components with two modifications:
|
1288
1444
|
|
1289
|
-
* Your file should require
|
1445
|
+
* Your file should require <tt>sinatra/base</tt> instead of +sinatra+;
|
1290
1446
|
otherwise, all of Sinatra's DSL methods are imported into the main
|
1291
1447
|
namespace.
|
1292
1448
|
* Put your app's routes, error handlers, filters, and options in a subclass
|
@@ -1298,15 +1454,15 @@ for details on available options and their behavior.
|
|
1298
1454
|
|
1299
1455
|
=== Modular vs. Classic Style
|
1300
1456
|
|
1301
|
-
Contrary to common
|
1457
|
+
Contrary to common belief, there is nothing wrong with classic style. If it
|
1302
1458
|
suits your application, you do not have to switch to a modular application.
|
1303
1459
|
|
1304
|
-
There are only two downsides compared
|
1460
|
+
There are only two downsides compared with modular style:
|
1305
1461
|
|
1306
|
-
* You may only have one Sinatra application per Ruby process
|
1462
|
+
* You may only have one Sinatra application per Ruby process. If you plan to
|
1307
1463
|
use more, switch to modular style.
|
1308
1464
|
|
1309
|
-
* Classic style pollutes Object with delegator methods
|
1465
|
+
* Classic style pollutes Object with delegator methods. If you plan to ship
|
1310
1466
|
your application in a library/gem, switch to modular style.
|
1311
1467
|
|
1312
1468
|
There is no reason you cannot mix modular and classic style.
|
@@ -1325,7 +1481,7 @@ differences in the setting:
|
|
1325
1481
|
|
1326
1482
|
=== Serving a Modular Application
|
1327
1483
|
|
1328
|
-
There are two common options for starting a modular app,
|
1484
|
+
There are two common options for starting a modular app, actively starting with
|
1329
1485
|
<tt>run!</tt>:
|
1330
1486
|
|
1331
1487
|
# my_app.rb
|
@@ -1425,7 +1581,7 @@ available.
|
|
1425
1581
|
=== Application/Class Scope
|
1426
1582
|
|
1427
1583
|
Every Sinatra application corresponds to a subclass of Sinatra::Base. If you
|
1428
|
-
are using the top
|
1584
|
+
are using the top-level DSL (<tt>require 'sinatra'</tt>), then this class is
|
1429
1585
|
Sinatra::Application, otherwise it is the subclass you created explicitly. At
|
1430
1586
|
class level you have methods like +get+ or +before+, but you cannot access the
|
1431
1587
|
+request+ object or the +session+, as there only is a single application class
|
@@ -1488,8 +1644,8 @@ You have the request scope binding inside:
|
|
1488
1644
|
=== Delegation Scope
|
1489
1645
|
|
1490
1646
|
The delegation scope just forwards methods to the class scope. However, it
|
1491
|
-
does not behave 100% like the class scope, as you do not have the class
|
1492
|
-
binding
|
1647
|
+
does not behave 100% like the class scope, as you do not have the class
|
1648
|
+
binding. Only methods explicitly marked for delegation are available and you
|
1493
1649
|
do not share variables/state with the class scope (read: you have a different
|
1494
1650
|
+self+). You can explicitly add method delegations by calling
|
1495
1651
|
<tt>Sinatra::Delegator.delegate :method_name</tt>.
|
@@ -1518,6 +1674,57 @@ Options are:
|
|
1518
1674
|
-s # specify rack server/handler (default is thin)
|
1519
1675
|
-x # turn on the mutex lock (default is off)
|
1520
1676
|
|
1677
|
+
== Requirements
|
1678
|
+
|
1679
|
+
It is recommended to install Sinatra on Ruby 1.8.7, 1.9.2, JRuby or Rubinius.
|
1680
|
+
|
1681
|
+
The following Ruby versions are officially supported:
|
1682
|
+
|
1683
|
+
[ Ruby 1.8.6 ]
|
1684
|
+
It is not recommended to use 1.8.6 for Sinatra. However, it will be
|
1685
|
+
officially supported until Sinatra 1.3.0 is released. RDoc and CoffeScript
|
1686
|
+
templates are not supported by this Ruby version. 1.8.6 includes a major
|
1687
|
+
memory leak in its Hash implementation, which is triggered by Sinatra
|
1688
|
+
versions prior to 1.1.1. The current version explicitly prevents this leak
|
1689
|
+
at the cost of performance. You will have to downgrade Rack to 1.1.x, as
|
1690
|
+
Rack >= 1.2 no longer supports 1.8.6.
|
1691
|
+
|
1692
|
+
[ Ruby 1.8.7 ]
|
1693
|
+
1.8.7 is fully supported, however, if nothing is keeping you from it, we
|
1694
|
+
recommend upgrading to 1.9.2 or switching to JRuby or Rubinius.
|
1695
|
+
|
1696
|
+
[ Ruby 1.9.2 ]
|
1697
|
+
1.9.2 is supported and recommended. Note that Radius and Markaby are
|
1698
|
+
currently not 1.9 compatible. Do not use 1.9.2p0, it is known to cause
|
1699
|
+
segmentation faults when using Sinatra.
|
1700
|
+
|
1701
|
+
[ Rubinius ]
|
1702
|
+
Rubinius is officially supported (Rubinius >= 1.2.2), with the exception
|
1703
|
+
of Textile templates.
|
1704
|
+
|
1705
|
+
[ JRuby ]
|
1706
|
+
JRuby is officially supported (JRuby >= 1.5.6). No issues with third party
|
1707
|
+
template libraries are known, however, if you choose to use JRuby, please
|
1708
|
+
look into JRuby rack handlers, as the Thin web server is not (yet) supported
|
1709
|
+
on JRuby.
|
1710
|
+
|
1711
|
+
We also keep an eye on upcoming Ruby versions.
|
1712
|
+
|
1713
|
+
The following Ruby implementations are not officially supported but still are
|
1714
|
+
known to run Sinatra:
|
1715
|
+
|
1716
|
+
* Older versions of JRuby and Rubinius
|
1717
|
+
* MacRuby
|
1718
|
+
* Maglev
|
1719
|
+
* IronRuby
|
1720
|
+
* Ruby 1.9.0 and 1.9.1
|
1721
|
+
|
1722
|
+
Not being officially supported means if things only break there and not on a
|
1723
|
+
supported platform, we assume it's not our issue but theirs.
|
1724
|
+
|
1725
|
+
Sinatra should work on any operating system supported by the chosen Ruby
|
1726
|
+
implementation.
|
1727
|
+
|
1521
1728
|
== The Bleeding Edge
|
1522
1729
|
If you would like to use Sinatra's latest bleeding code, feel free to run your
|
1523
1730
|
application against the master branch, it should be rather stable.
|
@@ -1530,13 +1737,13 @@ To get some of the latest features.
|
|
1530
1737
|
|
1531
1738
|
=== With Bundler
|
1532
1739
|
If you want to run your application with the latest Sinatra, using
|
1533
|
-
{Bundler}[http://gembundler.com/] is the
|
1740
|
+
{Bundler}[http://gembundler.com/] is the recommended way.
|
1534
1741
|
|
1535
1742
|
First, install bundler, if you haven't:
|
1536
1743
|
|
1537
1744
|
gem install bundler
|
1538
1745
|
|
1539
|
-
Then, in
|
1746
|
+
Then, in your project directory, create a +Gemfile+:
|
1540
1747
|
|
1541
1748
|
source :rubygems
|
1542
1749
|
gem 'sinatra', :git => "git://github.com/sinatra/sinatra.git"
|
@@ -1546,7 +1753,7 @@ Then, in you project directory, create a +Gemfile+:
|
|
1546
1753
|
gem 'activerecord', '~> 3.0' # maybe you also need ActiveRecord 3.x
|
1547
1754
|
|
1548
1755
|
Note that you will have to list all your applications dependencies in there.
|
1549
|
-
Sinatra's direct dependencies (Rack and Tilt) will however be automatically
|
1756
|
+
Sinatra's direct dependencies (Rack and Tilt) will, however, be automatically
|
1550
1757
|
fetched and added by Bundler.
|
1551
1758
|
|
1552
1759
|
Now you can run your app like this:
|
@@ -1555,7 +1762,7 @@ Now you can run your app like this:
|
|
1555
1762
|
|
1556
1763
|
=== Roll Your Own
|
1557
1764
|
Create a local clone and run your app with the <tt>sinatra/lib</tt> directory
|
1558
|
-
on the <tt
|
1765
|
+
on the <tt>$LOAD_PATH</tt>:
|
1559
1766
|
|
1560
1767
|
cd myapp
|
1561
1768
|
git clone git://github.com/sinatra/sinatra.git
|