adamwiggins-sinatra 0.8.9 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/AUTHORS +8 -7
  2. data/CHANGES +211 -1
  3. data/LICENSE +1 -1
  4. data/README.rdoc +183 -139
  5. data/Rakefile +20 -81
  6. data/lib/sinatra.rb +5 -1
  7. data/lib/sinatra/base.rb +569 -278
  8. data/lib/sinatra/main.rb +12 -25
  9. data/lib/sinatra/showexceptions.rb +303 -0
  10. data/sinatra.gemspec +20 -44
  11. data/test/base_test.rb +140 -52
  12. data/test/builder_test.rb +14 -17
  13. data/test/contest.rb +64 -0
  14. data/test/erb_test.rb +42 -16
  15. data/test/extensions_test.rb +100 -0
  16. data/test/filter_test.rb +85 -13
  17. data/test/haml_test.rb +39 -21
  18. data/test/helper.rb +76 -0
  19. data/test/helpers_test.rb +219 -84
  20. data/test/mapped_error_test.rb +168 -146
  21. data/test/middleware_test.rb +22 -17
  22. data/test/options_test.rb +323 -54
  23. data/test/render_backtrace_test.rb +145 -0
  24. data/test/request_test.rb +28 -6
  25. data/test/response_test.rb +42 -0
  26. data/test/result_test.rb +27 -21
  27. data/test/route_added_hook_test.rb +59 -0
  28. data/test/routing_test.rb +558 -77
  29. data/test/sass_test.rb +52 -13
  30. data/test/server_test.rb +47 -0
  31. data/test/sinatra_test.rb +3 -5
  32. data/test/static_test.rb +57 -30
  33. data/test/templates_test.rb +74 -25
  34. data/test/views/error.builder +3 -0
  35. data/test/views/error.erb +3 -0
  36. data/test/views/error.haml +3 -0
  37. data/test/views/error.sass +2 -0
  38. data/test/views/foo/hello.test +1 -0
  39. metadata +50 -46
  40. data/compat/app_test.rb +0 -300
  41. data/compat/application_test.rb +0 -334
  42. data/compat/builder_test.rb +0 -101
  43. data/compat/custom_error_test.rb +0 -62
  44. data/compat/erb_test.rb +0 -136
  45. data/compat/events_test.rb +0 -75
  46. data/compat/filter_test.rb +0 -30
  47. data/compat/haml_test.rb +0 -233
  48. data/compat/helper.rb +0 -21
  49. data/compat/mapped_error_test.rb +0 -72
  50. data/compat/pipeline_test.rb +0 -71
  51. data/compat/public/foo.xml +0 -1
  52. data/compat/sass_test.rb +0 -57
  53. data/compat/sessions_test.rb +0 -39
  54. data/compat/streaming_test.rb +0 -121
  55. data/compat/sym_params_test.rb +0 -19
  56. data/compat/template_test.rb +0 -30
  57. data/compat/use_in_file_templates_test.rb +0 -47
  58. data/compat/views/foo.builder +0 -1
  59. data/compat/views/foo.erb +0 -1
  60. data/compat/views/foo.haml +0 -1
  61. data/compat/views/foo.sass +0 -2
  62. data/compat/views/foo_layout.erb +0 -2
  63. data/compat/views/foo_layout.haml +0 -2
  64. data/compat/views/layout_test/foo.builder +0 -1
  65. data/compat/views/layout_test/foo.erb +0 -1
  66. data/compat/views/layout_test/foo.haml +0 -1
  67. data/compat/views/layout_test/foo.sass +0 -2
  68. data/compat/views/layout_test/layout.builder +0 -3
  69. data/compat/views/layout_test/layout.erb +0 -1
  70. data/compat/views/layout_test/layout.haml +0 -1
  71. data/compat/views/layout_test/layout.sass +0 -2
  72. data/compat/views/no_layout/no_layout.builder +0 -1
  73. data/compat/views/no_layout/no_layout.haml +0 -1
  74. data/lib/sinatra/compat.rb +0 -239
  75. data/lib/sinatra/test.rb +0 -112
  76. data/lib/sinatra/test/rspec.rb +0 -2
  77. data/lib/sinatra/test/spec.rb +0 -2
  78. data/lib/sinatra/test/unit.rb +0 -11
  79. data/test/reload_test.rb +0 -65
data/AUTHORS CHANGED
@@ -1,13 +1,13 @@
1
1
  Sinatra was designed and developed by Blake Mizerany (bmizerany) in
2
2
  California. Continued development would not be possible without the ongoing
3
- financial support provided by Heroku <heroku.com> and the emotional support
4
- provided by Adam Wiggins (adamwiggins), Chris Wanstrath (defunkt), PJ Hyett (pjhyett), and
5
- the rest of the Github crew.
3
+ financial support provided by [Heroku](http://heroku.com) and the emotional
4
+ support provided by Adam Wiggins (adamwiggins) of Heroku, Chris Wanstrath (defunkt),
5
+ PJ Hyett (pjhyett), and the rest of the GitHub crew.
6
6
 
7
7
  Special thanks to the following extraordinary individuals, who-out which
8
8
  Sinatra would not be possible:
9
9
 
10
- * Ryan Tomayko (rtomayko) for constantly fixing whitespace errors 60d5006
10
+ * Ryan Tomayko (rtomayko) for constantly fixing whitespace errors 60d5006
11
11
  * Ezra Zygmuntowicz (ezmobius) for initial help and letting Blake steal
12
12
  some of merbs internal code.
13
13
  * Christopher Schneid (cschneid) for The Book, the blog (gittr.com),
@@ -16,7 +16,7 @@ Sinatra would not be possible:
16
16
  the README, and hanging in there when times were rough.
17
17
  * Simon Rozet (sr) for a ton of doc patches, HAML options, and all that
18
18
  advocacy stuff he's going to do for 1.0.
19
- * Erik Kastner (kastner) for fixing MIME_TYPES under Rack 0.5.
19
+ * Erik Kastner (kastner) for fixing `MIME_TYPES` under Rack 0.5.
20
20
  * Ben Bleything (bleything) for caring about HTTP status codes and doc fixes.
21
21
  * Igal Koshevoy (igal) for root path detection under Thin/Passenger.
22
22
  * Jon Crosby (jcrosby) for coffee breaks, doc fixes, and just because, man.
@@ -27,12 +27,13 @@ Sinatra would not be possible:
27
27
  * Victor Hugo Borja (vic) for splat'n routes specs and doco.
28
28
  * Avdi Grimm (avdi) for basic RSpec support.
29
29
  * Jack Danger Canty for a more accurate root directory and for making me
30
- watch this just now: http://www.youtube.com/watch?v=ueaHLHgskkw.
30
+ watch [this](http://www.youtube.com/watch?v=ueaHLHgskkw) just now.
31
31
  * Mathew Walker for making escaped paths work with static files.
32
32
  * Millions of Us for having the problem that led to Sinatra's conception.
33
33
  * Songbird for the problems that helped Sinatra's future become realized.
34
- * Rick Olsen (technoweenie) for the killer plug at RailsConf '08.
34
+ * Rick Olson (technoweenie) for the killer plug at RailsConf '08.
35
35
  * Steven Garcia for the amazing custom artwork you see on 404's and 500's
36
+ * Pat Nakajima (nakajima) for fixing non-nested params in nested params Hash's.
36
37
 
37
38
  and last but not least:
38
39
 
data/CHANGES CHANGED
@@ -1,4 +1,192 @@
1
- = 0.9.0 (unreleased)
1
+ = 1.0 / unreleased
2
+
3
+ * Route handlers, before filters, templates, error mappings, and
4
+ middleware are now resolved dynamically up the inheritance hierarchy
5
+ when needed instead of duplicating the superclass's version when
6
+ a new Sinatra::Base subclass is created. This should fix a variety
7
+ of issues with extensions that need to add any of these things
8
+ to the base class.
9
+
10
+ = 0.9.2 / 2009-05-18
11
+
12
+ * This version is compatible with Rack 1.0. [Rein Henrichs]
13
+
14
+ * The development-mode unhandled exception / error page has been
15
+ greatly enhanced, functionally and aesthetically. The error
16
+ page is used when the :show_exceptions option is enabled and an
17
+ exception propagates outside of a route handler or before filter.
18
+ [Simon Rozet / Matte Noble / Ryan Tomayko]
19
+
20
+ * Backtraces that move through templates now include filenames and
21
+ line numbers where possible. [#51 / S. Brent Faulkner]
22
+
23
+ * All templates now have an app-level option for setting default
24
+ template options (:haml, :sass, :erb, :builder). The app-level
25
+ option value must be a Hash if set and is merged with the
26
+ template options specified to the render method (Base#haml,
27
+ Base#erb, Base#builder). [S. Brent Faulkner, Ryan Tomayko]
28
+
29
+ * The method signature for all template rendering methods has
30
+ been unified: "def engine(template, options={}, locals={})".
31
+ The options Hash now takes the generic :views, :layout, and
32
+ :locals options but also any template-specific options. The
33
+ generic options are removed before calling the template specific
34
+ render method. Locals may be specified using either the
35
+ :locals key in the options hash or a second Hash option to the
36
+ rendering method. [#191 / Ryan Tomayko]
37
+
38
+ * The receiver is now passed to "configure" blocks. This
39
+ allows for the following idiom in top-level apps:
40
+ configure { |app| set :foo, app.root + '/foo' }
41
+ [TJ Holowaychuck / Ryan Tomayko]
42
+
43
+ * The "sinatra/test" lib is deprecated and will be removed in
44
+ Sinatra 1.0. This includes the Sinatra::Test module and
45
+ Sinatra::TestHarness class in addition to all the framework
46
+ test helpers that were deprecated in 0.9.1. The Rack::Test
47
+ lib should be used instead: http://gitrdoc.com/brynary/rack-test
48
+ [#176 / Simon Rozet]
49
+
50
+ * Development mode source file reloading has been removed. The
51
+ "shotgun" (http://rtomayko.github.com/shotgun/) program can be
52
+ used to achieve the same basic functionality in most situations.
53
+ Passenger users should use the "tmp/always_restart.txt"
54
+ file (http://tinyurl.com/c67o4h). [#166 / Ryan Tomayko]
55
+
56
+ * Auto-requiring template libs in the erb, builder, haml, and
57
+ sass methods is deprecated due to thread-safety issues. You must
58
+ require the template libs explicitly in your app file. [Simon Rozet]
59
+
60
+ * A new Sinatra::Base#route_missing method was added. route_missing
61
+ is sent when no route matches the request or all route handlers
62
+ pass. The default implementation forwards the request to the
63
+ downstream app when running as middleware (i.e., "@app" is
64
+ non-nil), or raises a NotFound exception when no downstream app
65
+ is defined. Subclasses can override this method to perform custom
66
+ route miss logic. [Jon Crosby]
67
+
68
+ * A new Sinatra::Base#route_eval method was added. The method
69
+ yields to the block and throws :halt with the result. Subclasses
70
+ can override this method to tap into the route execution logic.
71
+ [TJ Holowaychuck]
72
+
73
+ * Fix the "-x" (enable request mutex / locking) command line
74
+ argument. Passing -x now properly sets the :lock option.
75
+ [S. Brent Faulkner, Ryan Tomayko]
76
+
77
+ * Fix writer ("foo=") and predicate ("foo?") methods in extension
78
+ modules not being added to the registering class.
79
+ [#172 / Pat Nakajima]
80
+
81
+ * Fix in-file templates when running alongside activesupport and
82
+ fatal errors when requiring activesupport before sinatra
83
+ [#178 / Brian Candler]
84
+
85
+ * Fix various issues running on Google AppEngine.
86
+ [Samuel Goebert, Simon Rozet]
87
+
88
+ * Fix in-file templates __END__ detection when __END__ exists with
89
+ other stuff on a line [Yoji Shidara]
90
+
91
+ = 0.9.1.1 / 2009-03-09
92
+
93
+ * Fix directory traversal vulnerability in default static files
94
+ route. See [#177] for more info.
95
+
96
+ = 0.9.1 / 2009-03-01
97
+
98
+ * Sinatra now runs under Ruby 1.9.1 [#61]
99
+
100
+ * Route patterns (splats, :named, or Regexp captures) are now
101
+ passed as arguments to the block. [#140]
102
+
103
+ * The "helpers" method now takes a variable number of modules
104
+ along with the normal block syntax. [#133]
105
+
106
+ * New request-level #forward method for middleware components: passes
107
+ the env to the downstream app and merges the response status, headers,
108
+ and body into the current context. [#126]
109
+
110
+ * Requests are now automatically forwarded to the downstream app when
111
+ running as middleware and no matching route is found or all routes
112
+ pass.
113
+
114
+ * New simple API for extensions/plugins to add DSL-level and
115
+ request-level methods. Use Sinatra.register(mixin) to extend
116
+ the DSL with all public methods defined in the mixin module;
117
+ use Sinatra.helpers(mixin) to make all public methods defined
118
+ in the mixin module available at the request level. [#138]
119
+ See http://www.sinatrarb.com/extensions.html for details.
120
+
121
+ * Named parameters in routes now capture the "." character. This makes
122
+ routes like "/:path/:filename" match against requests like
123
+ "/foo/bar.txt"; in this case, "params[:filename]" is "bar.txt".
124
+ Previously, the route would not match at all.
125
+
126
+ * Added request-level "redirect back" to redirect to the referring
127
+ URL.
128
+
129
+ * Added a new "clean_trace" option that causes backtraces dumped
130
+ to rack.errors and displayed on the development error page to
131
+ omit framework and core library backtrace lines. The option is
132
+ enabled by default. [#77]
133
+
134
+ * The ERB output buffer is now available to helpers via the @_out_buf
135
+ instance variable.
136
+
137
+ * It's now much easier to test sessions in unit tests by passing a
138
+ ":session" option to any of the mock request methods. e.g.,
139
+ get '/', {}, :session => { 'foo' => 'bar' }
140
+
141
+ * The testing framework specific files ('sinatra/test/spec',
142
+ 'sinatra/test/bacon', 'sinatra/test/rspec', etc.) have been deprecated.
143
+ See http://sinatrarb.com/testing.html for instructions on setting up
144
+ a testing environment with these frameworks.
145
+
146
+ * The request-level #send_data method from Sinatra 0.3.3 has been added
147
+ for compatibility but is deprecated.
148
+
149
+ * Fix :provides causing crash on any request when request has no
150
+ Accept header [#139]
151
+
152
+ * Fix that ERB templates were evaluated twice per "erb" call.
153
+
154
+ * Fix app-level middleware not being run when the Sinatra application is
155
+ run as middleware.
156
+
157
+ * Fixed some issues with running under Rack's CGI handler caused by
158
+ writing informational stuff to stdout.
159
+
160
+ * Fixed that reloading was sometimes enabled when starting from a
161
+ rackup file [#110]
162
+
163
+ * Fixed that "." in route patterns erroneously matched any character
164
+ instead of a literal ".". [#124]
165
+
166
+ = 0.9.0.4 / 2009-01-25
167
+
168
+ * Using halt with more than 1 args causes ArgumentError [#131]
169
+ * using halt in a before filter doesn't modify response [#127]
170
+ * Add deprecated Sinatra::EventContext to unbreak plugins [#130]
171
+ * Give access to GET/POST params in filters [#129]
172
+ * Preserve non-nested params in nested params hash [#117]
173
+ * Fix backtrace dump with Rack::Lint [#116]
174
+
175
+ = 0.9.0.3 / 2009-01-21
176
+
177
+ * Fall back on mongrel then webrick when thin not found. [#75]
178
+ * Use :environment instead of :env in test helpers to
179
+ fix deprecation warnings coming from framework.
180
+ * Make sinatra/test/rspec work again [#113]
181
+ * Fix app_file detection on windows [#118]
182
+ * Fix static files with Rack::Lint in pipeline [#121]
183
+
184
+ = 0.9.0.2 / 2009-01-18
185
+
186
+ * Halting a before block should stop processing of routes [#85]
187
+ * Fix redirect/halt in before filters [#85]
188
+
189
+ = 0.9.0 / 2009-01-18
2
190
 
3
191
  * Works with and requires Rack >= 0.9.1
4
192
 
@@ -8,6 +196,10 @@
8
196
  Documentation on using these features is forth-coming; the following
9
197
  provides the basic gist: http://gist.github.com/38605
10
198
 
199
+ * Parameters with subscripts are now parsed into a nested/recursive
200
+ Hash structure. e.g., "post[title]=Hello&post[body]=World" yields
201
+ params: {'post' => {'title' => 'Hello', 'body' => 'World'}}.
202
+
11
203
  * Regular expressions may now be used in route pattens; captures are
12
204
  available at "params[:captures]".
13
205
 
@@ -41,6 +233,20 @@
41
233
  has the status code specified. It's also possible to register an error
42
234
  page for a range of status codes: "error(500..599)".
43
235
 
236
+ * In-file templates are now automatically imported from the file that
237
+ requires 'sinatra'. The use_in_file_templates! method is still available
238
+ for loading templates from other files.
239
+
240
+ * Sinatra's testing support is no longer dependent on Test::Unit. Requiring
241
+ 'sinatra/test' adds the Sinatra::Test module and Sinatra::TestHarness
242
+ class, which can be used with any test framework. The 'sinatra/test/unit',
243
+ 'sinatra/test/spec', 'sinatra/test/rspec', or 'sinatra/test/bacon' files
244
+ can be required to setup a framework-specific testing environment. See the
245
+ README for more information.
246
+
247
+ * Added support for Bacon (test framework). The 'sinatra/test/bacon' file
248
+ can be required to setup Sinatra test helpers on Bacon::Context.
249
+
44
250
  * Deprecated "set_option" and "set_options"; use "set" instead.
45
251
 
46
252
  * Deprecated the "env" option ("options.env"); use "environment" instead.
@@ -65,6 +271,10 @@
65
271
  treated as internal server errors and result in a 500 response
66
272
  status.
67
273
 
274
+ * Deprecated the "get_it", "post_it", "put_it", "delete_it", and "head_it"
275
+ test helper methods. Use "get", "post", "put", "delete", and "head",
276
+ respectively, instead.
277
+
68
278
  * Removed Event and EventContext classes. Applications are defined in a
69
279
  subclass of Sinatra::Base; each request is processed within an
70
280
  instance.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2007 Blake Mizerany
1
+ Copyright (c) 2007, 2008, 2009 Blake Mizerany
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person
4
4
  obtaining a copy of this software and associated documentation
@@ -1,6 +1,6 @@
1
1
  = Sinatra
2
2
 
3
- Sinatra is a DSL for quickly creating web-applications in Ruby with minimal
3
+ Sinatra is a DSL for quickly creating web applications in Ruby with minimal
4
4
  effort:
5
5
 
6
6
  # myapp.rb
@@ -10,12 +10,20 @@ effort:
10
10
  'Hello world!'
11
11
  end
12
12
 
13
- Run with <tt>ruby myapp.rb</tt> and view at <tt>http://localhost:4567</tt>
13
+ Install the gem and run with:
14
14
 
15
- == HTTP Methods
15
+ sudo gem install sinatra
16
+ ruby myapp.rb
17
+
18
+ View at: http://localhost:4567
19
+
20
+ == Routes
21
+
22
+ In Sinatra, a route is an HTTP method paired with an URL matching pattern.
23
+ Each route is associated with a block:
16
24
 
17
25
  get '/' do
18
- .. show things ..
26
+ .. show something ..
19
27
  end
20
28
 
21
29
  post '/' do
@@ -30,26 +38,24 @@ Run with <tt>ruby myapp.rb</tt> and view at <tt>http://localhost:4567</tt>
30
38
  .. annihilate something ..
31
39
  end
32
40
 
33
- == Routes
34
-
35
- Routes are matched based on the order of declaration. The first route that
41
+ Routes are matched in the order they are defined. The first route that
36
42
  matches the request is invoked.
37
43
 
38
- Basic routes:
39
-
40
- get '/hi' do
41
- ...
42
- end
43
-
44
44
  Route patterns may include named parameters, accessible via the
45
45
  <tt>params</tt> hash:
46
46
 
47
- get '/:name' do
48
- # matches "GET /foo" and "GET /bar"
47
+ get '/hello/:name' do
48
+ # matches "GET /hello/foo" and "GET /hello/bar"
49
49
  # params[:name] is 'foo' or 'bar'
50
50
  "Hello #{params[:name]}!"
51
51
  end
52
52
 
53
+ You can also access named parameters via block parameters:
54
+
55
+ get '/hello/:name' do |n|
56
+ "Hello #{n}!"
57
+ end
58
+
53
59
  Route patterns may also include splat (or wildcard) parameters, accessible
54
60
  via the <tt>params[:splat]</tt> array.
55
61
 
@@ -69,6 +75,12 @@ Route matching with Regular Expressions:
69
75
  "Hello, #{params[:captures].first}!"
70
76
  end
71
77
 
78
+ Or with a block parameter:
79
+
80
+ get %r{/hello/([\w]+)} do |c|
81
+ "Hello, #{c}!"
82
+ end
83
+
72
84
  Routes may include a variety of matching conditions, such as the user agent:
73
85
 
74
86
  get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
@@ -86,25 +98,52 @@ a different location by setting the <tt>:public</tt> option:
86
98
 
87
99
  set :public, File.dirname(__FILE__) + '/static'
88
100
 
101
+ Note that the public directory name is not included in the URL. A file
102
+ <tt>./public/css/style.css</tt> is made available as
103
+ <tt>http://example.com/css/style.css</tt>.
104
+
89
105
  == Views / Templates
90
106
 
91
- Templates are assumed to be located directly under a <tt>./views</tt>
107
+ Templates are assumed to be located directly under the <tt>./views</tt>
92
108
  directory. To use a different views directory:
93
109
 
94
110
  set :views, File.dirname(__FILE__) + '/templates'
95
111
 
112
+ One important thing to remember is that you always have to reference
113
+ templates with symbols, even if they're in a subdirectory (in this
114
+ case use <tt>:'subdir/template'</tt>). Rendering methods will render
115
+ any strings passed to them directly.
116
+
96
117
  === Haml Templates
97
118
 
98
119
  The haml gem/library is required to render HAML templates:
99
120
 
121
+ ## You'll need to require haml in your app
122
+ require 'haml'
123
+
100
124
  get '/' do
101
125
  haml :index
102
126
  end
103
127
 
104
128
  Renders <tt>./views/index.haml</tt>.
105
129
 
130
+ {Haml's options}[http://haml.hamptoncatlin.com/docs/rdoc/classes/Haml.html]
131
+ can be set globally through Sinatra's configurations,
132
+ see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
133
+ and overridden on an individual basis.
134
+
135
+ set :haml, {:format => :html5 } # default Haml format is :xhtml
136
+
137
+ get '/' do
138
+ haml :index, :haml_options => {:format => :html4 } # overridden
139
+ end
140
+
141
+
106
142
  === Erb Templates
107
143
 
144
+ ## You'll need to require erb in your app
145
+ require 'erb'
146
+
108
147
  get '/' do
109
148
  erb :index
110
149
  end
@@ -115,6 +154,9 @@ Renders <tt>./views/index.erb</tt>
115
154
 
116
155
  The builder gem/library is required to render builder templates:
117
156
 
157
+ ## You'll need to require builder in your app
158
+ require 'builder'
159
+
118
160
  get '/' do
119
161
  content_type 'application/xml', :charset => 'utf-8'
120
162
  builder :index
@@ -126,6 +168,9 @@ Renders <tt>./views/index.builder</tt>.
126
168
 
127
169
  The sass gem/library is required to render Sass templates:
128
170
 
171
+ ## You'll need to require haml or sass in your app
172
+ require 'sass'
173
+
129
174
  get '/stylesheet.css' do
130
175
  content_type 'text/css', :charset => 'utf-8'
131
176
  sass :stylesheet
@@ -133,6 +178,19 @@ The sass gem/library is required to render Sass templates:
133
178
 
134
179
  Renders <tt>./views/stylesheet.sass</tt>.
135
180
 
181
+ {Sass' options}[http://haml.hamptoncatlin.com/docs/rdoc/classes/Sass.html]
182
+ can be set globally through Sinatra's configurations,
183
+ see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
184
+ and overridden on an individual basis.
185
+
186
+ set :sass, {:style => :compact } # default Sass style is :nested
187
+
188
+ get '/stylesheet.css' do
189
+ content_type 'text/css', :charset => 'utf-8'
190
+ sass :stylesheet, :sass_options => {:style => :expanded } # overridden
191
+ end
192
+
193
+
136
194
  === Inline Templates
137
195
 
138
196
  get '/' do
@@ -141,10 +199,10 @@ Renders <tt>./views/stylesheet.sass</tt>.
141
199
 
142
200
  Renders the inlined template string.
143
201
 
144
- === Accessing Variables
202
+ === Accessing Variables in Templates
145
203
 
146
- Templates are evaluated within the same context as the route blocks. Instance
147
- variables set in route blocks are available in templates:
204
+ Templates are evaluated within the same context as route handlers. Instance
205
+ variables set in route handlers are direcly accessible by templates:
148
206
 
149
207
  get '/:id' do
150
208
  @foo = Foo.find(params[:id])
@@ -165,12 +223,13 @@ other templates.
165
223
 
166
224
  Templates may be defined at the end of the source file:
167
225
 
226
+ require 'rubygems'
227
+ require 'sinatra'
228
+
168
229
  get '/' do
169
230
  haml :index
170
231
  end
171
232
 
172
- use_in_file_templates!
173
-
174
233
  __END__
175
234
 
176
235
  @@ layout
@@ -180,8 +239,13 @@ Templates may be defined at the end of the source file:
180
239
  @@ index
181
240
  %div.title Hello world!!!!!
182
241
 
183
- It's also possible to define named templates using the top-level template
184
- method:
242
+ NOTE: In-file templates defined in the source file that requires sinatra
243
+ are automatically loaded. Call the <tt>use_in_file_templates!</tt>
244
+ method explicitly if you have in-file templates in other source files.
245
+
246
+ === Named Templates
247
+
248
+ Templates may also be defined using the top-level <tt>template</tt> method:
185
249
 
186
250
  template :layout do
187
251
  "%html\n =yield\n"
@@ -205,7 +269,7 @@ is rendered. You can disable layouts by passing <tt>:layout => false</tt>.
205
269
  == Helpers
206
270
 
207
271
  Use the top-level <tt>helpers</tt> method to define helper methods for use in
208
- route blocks and templates:
272
+ route handlers and templates:
209
273
 
210
274
  helpers do
211
275
  def bar(name)
@@ -221,7 +285,7 @@ route blocks and templates:
221
285
 
222
286
  Before filters are evaluated before each request within the context of the
223
287
  request and can modify the request and response. Instance variables set in
224
- filters are accessible by routes and templates.
288
+ filters are accessible by routes and templates:
225
289
 
226
290
  before do
227
291
  @note = 'Hi!'
@@ -243,14 +307,13 @@ You can also specify a body when halting ...
243
307
 
244
308
  halt 'this will be the body'
245
309
 
246
- Set the status and body ...
310
+ Or set the status and body ...
247
311
 
248
312
  halt 401, 'go away!'
249
313
 
250
314
  == Passing
251
315
 
252
- A route can punt processing to the next matching route using the <tt>pass</tt>
253
- statement:
316
+ A route can punt processing to the next matching route using <tt>pass</tt>:
254
317
 
255
318
  get '/guess/:who' do
256
319
  pass unless params[:who] == 'Frank'
@@ -264,13 +327,7 @@ statement:
264
327
  The route block is immediately exited and control continues with the next
265
328
  matching route. If no matching route is found, a 404 is returned.
266
329
 
267
- == Configuration and Reloading
268
-
269
- Sinatra supports multiple environments and reloading. Reloading happens
270
- before each request when running under the <tt>:development</tt>
271
- environment. Wrap your configurations (e.g., database connections, constants,
272
- etc.) in <tt>configure</tt> blocks to protect them from reloading or to
273
- target specific environments.
330
+ == Configuration
274
331
 
275
332
  Run once, at startup, in any environment:
276
333
 
@@ -279,14 +336,14 @@ Run once, at startup, in any environment:
279
336
  end
280
337
 
281
338
  Run only when the environment (RACK_ENV environment variable) is set to
282
- <tt>:production</tt>.
339
+ <tt>:production</tt>:
283
340
 
284
341
  configure :production do
285
342
  ...
286
343
  end
287
344
 
288
- Run when the environment (RACK_ENV environment variable) is set to
289
- either <tt>:production</tt> or <tt>:test</tt>.
345
+ Run when the environment is set to either <tt>:production</tt> or
346
+ <tt>:test</tt>:
290
347
 
291
348
  configure :production, :test do
292
349
  ...
@@ -311,7 +368,7 @@ code is 404, the <tt>not_found</tt> handler is invoked:
311
368
 
312
369
  The +error+ handler is invoked any time an exception is raised from a route
313
370
  block or before filter. The exception object can be obtained from the
314
- 'sinatra.error' Rack variable:
371
+ <tt>sinatra.error</tt> Rack variable:
315
372
 
316
373
  error do
317
374
  'Sorry there was a nasty error - ' + env['sinatra.error'].name
@@ -333,8 +390,8 @@ You get this:
333
390
 
334
391
  So what happened was... something bad
335
392
 
336
- Sinatra installs special not_found and error handlers when running under
337
- the development environment.
393
+ Sinatra installs special <tt>not_found</tt> and <tt>error</tt> handlers when
394
+ running under the development environment.
338
395
 
339
396
  == Mime types
340
397
 
@@ -380,150 +437,137 @@ typically don't have to +use+ them explicitly.
380
437
 
381
438
  == Testing
382
439
 
383
- === Test/Unit
440
+ Sinatra tests can be written using any Rack-based testing library
441
+ or framework. {Rack::Test}[http://gitrdoc.com/brynary/rack-test] is
442
+ recommended:
384
443
 
385
- require 'rubygems'
386
- require 'sinatra'
387
- require 'sinatra/test/unit'
388
444
  require 'my_sinatra_app'
445
+ require 'rack/test'
389
446
 
390
447
  class MyAppTest < Test::Unit::TestCase
448
+ include Rack::Test::Methods
391
449
 
392
- def test_my_default
393
- get_it '/'
394
- assert_equal 'My Default Page!', @response.body
450
+ def app
451
+ Sinatra::Application
395
452
  end
396
453
 
397
- def test_with_agent
398
- get_it '/', :agent => 'Songbird'
399
- assert_equal 'You're in Songbird!', @response.body
454
+ def test_my_default
455
+ get '/'
456
+ assert_equal 'Hello World!', last_response.body
400
457
  end
401
458
 
402
- ...
459
+ def test_with_params
460
+ get '/meet', :name => 'Frank'
461
+ assert_equal 'Hello Frank!', last_response.body
462
+ end
403
463
 
464
+ def test_with_rack_env
465
+ get '/', {}, 'HTTP_USER_AGENT' => 'Songbird'
466
+ assert_equal "You're using Songbird!", last_response.body
467
+ end
404
468
  end
405
469
 
406
- === Test/Spec
470
+ NOTE: The built-in Sinatra::Test module and Sinatra::TestHarness class
471
+ are deprecated as of the 0.9.2 release.
407
472
 
408
- require 'rubygems'
409
- require 'sinatra'
410
- require 'sinatra/test/spec'
411
- require 'my_sinatra_app'
473
+ == Sinatra::Base - Middleware, Libraries, and Modular Apps
412
474
 
413
- describe 'My app' do
475
+ Defining your app at the top-level works well for micro-apps but has
476
+ considerable drawbacks when building reuseable components such as Rack
477
+ middleware, Rails metal, simple libraries with a server component, or
478
+ even Sinatra extensions. The top-level DSL pollutes the Object namespace
479
+ and assumes a micro-app style configuration (e.g., a single application
480
+ file, ./public and ./views directories, logging, exception detail page,
481
+ etc.). That's where Sinatra::Base comes into play:
414
482
 
415
- it "should show a default page" do
416
- get_it '/'
417
- should.be.ok
418
- body.should.equal 'My Default Page!'
419
- end
483
+ require 'sinatra/base'
420
484
 
421
- ...
485
+ class MyApp < Sinatra::Base
486
+ set :sessions, true
487
+ set :foo, 'bar'
422
488
 
489
+ get '/' do
490
+ 'Hello world!'
491
+ end
423
492
  end
424
493
 
425
- === RSpec
494
+ The MyApp class is an independent Rack component that can act as
495
+ Rack middleware, a Rack application, or Rails metal. You can +use+ or
496
+ +run+ this class from a rackup +config.ru+ file; or, control a server
497
+ component shipped as a library:
426
498
 
427
- require 'rubygems'
428
- require 'spec'
429
- require 'sinatra'
430
- require 'sinatra/test/rspec'
431
- require 'my_sinatra_app'
499
+ MyApp.run! :host => 'localhost', :port => 9090
432
500
 
433
- describe 'My app' do
434
- it 'should show a default page' do
435
- get_it '/'
436
- @response.should be_ok
437
- @response.body.should == 'My Default Page!'
438
- end
501
+ The methods available to Sinatra::Base subclasses are exactly as those
502
+ available via the top-level DSL. Most top-level apps can be converted to
503
+ Sinatra::Base components with two modifications:
439
504
 
440
- ...
505
+ * Your file should require +sinatra/base+ instead of +sinatra+;
506
+ otherwise, all of Sinatra's DSL methods are imported into the main
507
+ namespace.
508
+ * Put your app's routes, error handlers, filters, and options in a subclass
509
+ of Sinatra::Base.
441
510
 
442
- end
511
+ +Sinatra::Base+ is a blank slate. Most options are disabled by default,
512
+ including the built-in server. See {Options and Configuration}[http://sinatra.github.com/configuration.html]
513
+ for details on available options and their behavior.
443
514
 
444
- See Sinatra::Test::Methods for more information on +get_it+, +post_it+,
445
- +put_it+, and friends.
515
+ SIDEBAR: Sinatra's top-level DSL is implemented using a simple delegation
516
+ system. The +Sinatra::Application+ class -- a special subclass of
517
+ Sinatra::Base -- receives all :get, :put, :post, :delete, :before,
518
+ :error, :not_found, :configure, and :set messages sent to the
519
+ top-level. Have a look at the code for yourself: here's the
520
+ {Sinatra::Delegator mixin}[http://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1064]
521
+ being {included into the main namespace}[http://github.com/sinatra/sinatra/blob/master/lib/sinatra/main.rb#L25].
446
522
 
447
523
  == Command line
448
524
 
449
525
  Sinatra applications can be run directly:
450
526
 
451
- ruby myapp.rb [-h] [-x] [-p PORT] [-e ENVIRONMENT]
527
+ ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-s HANDLER]
452
528
 
453
529
  Options are:
454
530
 
455
531
  -h # help
456
532
  -p # set the port (default is 4567)
457
533
  -e # set the environment (default is development)
534
+ -s # specify rack server/handler (default is thin)
458
535
  -x # turn on the mutex lock (default is off)
459
536
 
460
- == Contributing
461
-
462
- === Tools
463
-
464
- Besides Ruby itself, you only need a text editor, preferably one that supports
465
- Ruby syntax hilighting. VIM and Emacs are a fine choice on any platform, but
466
- feel free to use whatever you're familiar with.
467
-
468
- Sinatra uses the Git source code management system. If you're unfamiliar with
469
- Git, you can find more information and tutorials on http://git.or.cz/ as well
470
- as http://git-scm.com/. Scott Chacon created a great series of introductory
471
- screencasts about Git, which you can find here: http://www.gitcasts.com/
537
+ == The Bleeding Edge
472
538
 
473
- === First Time: Cloning The Sinatra Repo
539
+ If you would like to use Sinatra's latest bleeding code, create a local
540
+ clone and run your app with the <tt>sinatra/lib</tt> directory on the
541
+ <tt>LOAD_PATH</tt>:
474
542
 
475
- cd where/you/keep/your/projects
476
- git clone git://github.com/bmizerany/sinatra.git
477
- cd sinatra
478
- cd path/to/your_project
479
- ln -s ../sinatra/
543
+ cd myapp
544
+ git clone git://github.com/sinatra/sinatra.git
545
+ ruby -Isinatra/lib myapp.rb
480
546
 
481
- === Updating Your Existing Sinatra Clone
482
-
483
- cd where/you/keep/sinatra
484
- git pull
485
-
486
- === Using Edge Sinatra in Your App
487
-
488
- at the top of your sinatra_app.rb file:
547
+ Alternatively, you can add the <tt>sinatra/lib</tt> directory to the
548
+ <tt>LOAD_PATH</tt> in your application:
489
549
 
490
550
  $LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
551
+ require 'rubygems'
491
552
  require 'sinatra'
492
553
 
493
554
  get '/about' do
494
- "I'm running on Version " + Sinatra::VERSION
555
+ "I'm running version " + Sinatra::VERSION
495
556
  end
496
557
 
497
- === Contributing a Patch
558
+ To update the Sinatra sources in the future:
498
559
 
499
- There are several ways to do this. Probably the easiest (and preferred) way is
500
- to fork Sinatra on GitHub (http://github.com/bmizerany/sinatra), push your
501
- changes to your Sinatra repo, and then send Blake Mizerany (bmizerany on
502
- GitHub) a pull request.
503
-
504
- You can also create a patch file and attach it to a feature request or bug fix
505
- on the issue tracker (see below) or send it to the mailing list (see Community
506
- section).
507
-
508
- === Issue Tracking and Feature Requests
509
-
510
- http://sinatra.lighthouseapp.com/
511
-
512
- == Community
513
-
514
- === Mailing List
515
-
516
- http://groups.google.com/group/sinatrarb
517
-
518
- If you have a problem or question, please make sure to include all the
519
- relevant information in your mail, like the Sinatra version you're using, what
520
- version of Ruby you have, and so on.
521
-
522
- === IRC Channel
523
-
524
- You can find us on the Freenode network in the channel #sinatra
525
- (irc://chat.freenode.net/#sinatra)
560
+ cd myproject/sinatra
561
+ git pull
526
562
 
527
- There's usually someone online at any given time, but we cannot pay attention
528
- to the channel all the time, so please stick around for a while after asking a
529
- question.
563
+ == More
564
+
565
+ * {Project Website}[http://sinatra.github.com/] - Additional documentation,
566
+ news, and links to other resources.
567
+ * {Contributing}[http://sinatra.github.com/contributing.html] - Find a bug? Need
568
+ help? Have a patch?
569
+ * {Lighthouse}[http://sinatra.lighthouseapp.com] - Issue tracking and release
570
+ planning.
571
+ * {Twitter}[http://twitter.com/sinatra]
572
+ * {Mailing List}[http://groups.google.com/group/sinatrarb]
573
+ * {IRC: #sinatra}[irc://chat.freenode.net/#sinatra] on http://freenode.net