darkhelmet-sinatra 0.9.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/AUTHORS +41 -0
- data/CHANGES +243 -0
- data/LICENSE +22 -0
- data/README.rdoc +535 -0
- data/Rakefile +136 -0
- data/compat/app_test.rb +301 -0
- data/compat/application_test.rb +334 -0
- data/compat/builder_test.rb +101 -0
- data/compat/compat_test.rb +12 -0
- data/compat/custom_error_test.rb +62 -0
- data/compat/erb_test.rb +136 -0
- data/compat/events_test.rb +78 -0
- data/compat/filter_test.rb +30 -0
- data/compat/haml_test.rb +233 -0
- data/compat/helper.rb +30 -0
- data/compat/mapped_error_test.rb +72 -0
- data/compat/pipeline_test.rb +71 -0
- data/compat/public/foo.xml +1 -0
- data/compat/sass_test.rb +57 -0
- data/compat/sessions_test.rb +39 -0
- data/compat/streaming_test.rb +133 -0
- data/compat/sym_params_test.rb +19 -0
- data/compat/template_test.rb +30 -0
- data/compat/use_in_file_templates_test.rb +47 -0
- data/compat/views/foo.builder +1 -0
- data/compat/views/foo.erb +1 -0
- data/compat/views/foo.haml +1 -0
- data/compat/views/foo.sass +2 -0
- data/compat/views/foo_layout.erb +2 -0
- data/compat/views/foo_layout.haml +2 -0
- data/compat/views/layout_test/foo.builder +1 -0
- data/compat/views/layout_test/foo.erb +1 -0
- data/compat/views/layout_test/foo.haml +1 -0
- data/compat/views/layout_test/foo.sass +2 -0
- data/compat/views/layout_test/layout.builder +3 -0
- data/compat/views/layout_test/layout.erb +1 -0
- data/compat/views/layout_test/layout.haml +1 -0
- data/compat/views/layout_test/layout.sass +2 -0
- data/compat/views/no_layout/no_layout.builder +1 -0
- data/compat/views/no_layout/no_layout.haml +1 -0
- data/lib/sinatra/base.rb +1007 -0
- data/lib/sinatra/compat.rb +252 -0
- data/lib/sinatra/images/404.png +0 -0
- data/lib/sinatra/images/500.png +0 -0
- data/lib/sinatra/main.rb +47 -0
- data/lib/sinatra/test/bacon.rb +19 -0
- data/lib/sinatra/test/rspec.rb +13 -0
- data/lib/sinatra/test/spec.rb +11 -0
- data/lib/sinatra/test/unit.rb +13 -0
- data/lib/sinatra/test.rb +121 -0
- data/lib/sinatra.rb +8 -0
- data/sinatra.gemspec +116 -0
- data/test/base_test.rb +112 -0
- data/test/builder_test.rb +64 -0
- data/test/data/reload_app_file.rb +3 -0
- data/test/erb_test.rb +81 -0
- data/test/extensions_test.rb +63 -0
- data/test/filter_test.rb +99 -0
- data/test/haml_test.rb +68 -0
- data/test/helper.rb +85 -0
- data/test/helpers_test.rb +467 -0
- data/test/mapped_error_test.rb +160 -0
- data/test/middleware_test.rb +60 -0
- data/test/options_test.rb +374 -0
- data/test/reload_test.rb +68 -0
- data/test/request_test.rb +18 -0
- data/test/response_test.rb +42 -0
- data/test/result_test.rb +98 -0
- data/test/routing_test.rb +712 -0
- data/test/sass_test.rb +36 -0
- data/test/server_test.rb +41 -0
- data/test/sinatra_test.rb +13 -0
- data/test/static_test.rb +65 -0
- data/test/templates_test.rb +88 -0
- data/test/test_test.rb +109 -0
- data/test/views/hello.builder +1 -0
- data/test/views/hello.erb +1 -0
- data/test/views/hello.haml +1 -0
- data/test/views/hello.sass +2 -0
- data/test/views/hello.test +1 -0
- data/test/views/layout2.builder +3 -0
- data/test/views/layout2.erb +2 -0
- data/test/views/layout2.haml +2 -0
- data/test/views/layout2.test +1 -0
- metadata +184 -0
data/AUTHORS
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
Sinatra was designed and developed by Blake Mizerany (bmizerany) in
|
2
|
+
California. Continued development would not be possible without the ongoing
|
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
|
+
|
7
|
+
Special thanks to the following extraordinary individuals, who-out which
|
8
|
+
Sinatra would not be possible:
|
9
|
+
|
10
|
+
* Ryan Tomayko (rtomayko) for constantly fixing whitespace errors 60d5006
|
11
|
+
* Ezra Zygmuntowicz (ezmobius) for initial help and letting Blake steal
|
12
|
+
some of merbs internal code.
|
13
|
+
* Christopher Schneid (cschneid) for The Book, the blog (gittr.com),
|
14
|
+
irclogger.com, and a bunch of useful patches.
|
15
|
+
* Markus Prinz (cypher) for patches over the years, caring about
|
16
|
+
the README, and hanging in there when times were rough.
|
17
|
+
* Simon Rozet (sr) for a ton of doc patches, HAML options, and all that
|
18
|
+
advocacy stuff he's going to do for 1.0.
|
19
|
+
* Erik Kastner (kastner) for fixing `MIME_TYPES` under Rack 0.5.
|
20
|
+
* Ben Bleything (bleything) for caring about HTTP status codes and doc fixes.
|
21
|
+
* Igal Koshevoy (igal) for root path detection under Thin/Passenger.
|
22
|
+
* Jon Crosby (jcrosby) for coffee breaks, doc fixes, and just because, man.
|
23
|
+
* Karel Minarik (karmi) for screaming until the website came back up.
|
24
|
+
* Jeremy Evans (jeremyevans) for unbreaking optional path params (twice!)
|
25
|
+
* The GitHub guys for stealing Blake's table.
|
26
|
+
* Nickolas Means (nmeans) for Sass template support.
|
27
|
+
* Victor Hugo Borja (vic) for splat'n routes specs and doco.
|
28
|
+
* Avdi Grimm (avdi) for basic RSpec support.
|
29
|
+
* Jack Danger Canty for a more accurate root directory and for making me
|
30
|
+
watch [this](http://www.youtube.com/watch?v=ueaHLHgskkw) just now.
|
31
|
+
* Mathew Walker for making escaped paths work with static files.
|
32
|
+
* Millions of Us for having the problem that led to Sinatra's conception.
|
33
|
+
* Songbird for the problems that helped Sinatra's future become realized.
|
34
|
+
* Rick Olson (technoweenie) for the killer plug at RailsConf '08.
|
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.
|
37
|
+
|
38
|
+
and last but not least:
|
39
|
+
|
40
|
+
* Frank Sinatra (chairman of the board) for having so much class he
|
41
|
+
deserves a web-framework named after him.
|
data/CHANGES
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
= 0.9.1 / unreleased
|
2
|
+
|
3
|
+
* Sinatra now runs under Ruby 1.9.1 [#61]
|
4
|
+
* Route patterns (splats, :named, or Regexp captures) are now
|
5
|
+
passed as arguments to the block. [#140]
|
6
|
+
* The "helpers" method now takes a variable number of modules
|
7
|
+
along with the normal block syntax. [#133]
|
8
|
+
* New request-level #forward method for middleware components: passes
|
9
|
+
the env to the downstream app and merges the response status, headers,
|
10
|
+
and body into the current context.
|
11
|
+
* Requests are now automatically forwarded to the downstream app when
|
12
|
+
running as middleware and no matching route is found or all routes
|
13
|
+
pass.
|
14
|
+
* New simple API for extensions/plugins to add DSL-level and
|
15
|
+
request-level methods. Use Sinatra.register(mixin) to extend
|
16
|
+
the DSL with all public methods defined in the mixin module;
|
17
|
+
use Sinatra.helpers(mixin) to make all public methods defined
|
18
|
+
in the mixin module available at the request level. [#138]
|
19
|
+
* Added "redirect back" to redirect to the referring URL.
|
20
|
+
* Added a new "clean_trace" option that causes backtraces dumped
|
21
|
+
to rack.errors and displayed on the development error page to
|
22
|
+
omit framework and core library backtrace lines. The option is
|
23
|
+
enabled by default. [#77]
|
24
|
+
* Fix :provides causing crash on any request when request has no
|
25
|
+
Accept header [#139]
|
26
|
+
* Fix that ERB templates were evaluated twice per "erb" call.
|
27
|
+
* The ERB output buffer is now available to helpers via the @_out_buf
|
28
|
+
instance variable.
|
29
|
+
* The request-level #send_data method from Sinatra 0.3.3 has been added
|
30
|
+
for compatibility but is deprecated.
|
31
|
+
|
32
|
+
= 0.9.0.4 / 2009-01-25
|
33
|
+
|
34
|
+
* Using halt with more than 1 args causes ArgumentError [#131]
|
35
|
+
* using halt in a before filter doesn't modify response [#127]
|
36
|
+
* Add deprecated Sinatra::EventContext to unbreak plugins [#130]
|
37
|
+
* Give access to GET/POST params in filters [#129]
|
38
|
+
* Preserve non-nested params in nested params hash [#117]
|
39
|
+
* Fix backtrace dump with Rack::Lint [#116]
|
40
|
+
|
41
|
+
= 0.9.0.3 / 2009-01-21
|
42
|
+
|
43
|
+
* Fall back on mongrel then webrick when thin not found. [#75]
|
44
|
+
* Use :environment instead of :env in test helpers to
|
45
|
+
fix deprecation warnings coming from framework.
|
46
|
+
* Make sinatra/test/rspec work again [#113]
|
47
|
+
* Fix app_file detection on windows [#118]
|
48
|
+
* Fix static files with Rack::Lint in pipeline [#121]
|
49
|
+
|
50
|
+
= 0.9.0.2 / 2009-01-18
|
51
|
+
|
52
|
+
* Halting a before block should stop processing of routes [#85]
|
53
|
+
* Fix redirect/halt in before filters [#85]
|
54
|
+
|
55
|
+
= 0.9.0 / 2009-01-18
|
56
|
+
|
57
|
+
* Works with and requires Rack >= 0.9.1
|
58
|
+
|
59
|
+
* Multiple Sinatra applications can now co-exist peacefully within a
|
60
|
+
single process. The new "Sinatra::Base" class can be subclassed to
|
61
|
+
establish a blank-slate Rack application or middleware component.
|
62
|
+
Documentation on using these features is forth-coming; the following
|
63
|
+
provides the basic gist: http://gist.github.com/38605
|
64
|
+
|
65
|
+
* Parameters with subscripts are now parsed into a nested/recursive
|
66
|
+
Hash structure. e.g., "post[title]=Hello&post[body]=World" yields
|
67
|
+
params: {'post' => {'title' => 'Hello', 'body' => 'World'}}.
|
68
|
+
|
69
|
+
* Regular expressions may now be used in route pattens; captures are
|
70
|
+
available at "params[:captures]".
|
71
|
+
|
72
|
+
* New ":provides" route condition takes an array of mime types and
|
73
|
+
matches only when an Accept request header is present with a
|
74
|
+
corresponding type. [cypher]
|
75
|
+
|
76
|
+
* New request-level "pass" method; immediately exits the current block
|
77
|
+
and passes control to the next matching route.
|
78
|
+
|
79
|
+
* The request-level "body" method now takes a block; evaluation is
|
80
|
+
deferred until an attempt is made to read the body. The block must
|
81
|
+
return a String or Array.
|
82
|
+
|
83
|
+
* New "route conditions" system for attaching rules for when a route
|
84
|
+
matches. The :agent and :host route options now use this system.
|
85
|
+
|
86
|
+
* New "dump_errors" option controls whether the backtrace is dumped to
|
87
|
+
rack.errors when an exception is raised from a route. The option is
|
88
|
+
enabled by default for top-level apps.
|
89
|
+
|
90
|
+
* Better default "app_file", "root", "public", and "views" location
|
91
|
+
detection; changes to "root" and "app_file" automatically cascade to
|
92
|
+
other options that depend on them.
|
93
|
+
|
94
|
+
* Error mappings are now split into two distinct layers: exception
|
95
|
+
mappings and custom error pages. Exception mappings are registered
|
96
|
+
with "error(Exception)" and are run only when the app raises an
|
97
|
+
exception. Custom error pages are registered with "error(status_code)",
|
98
|
+
where "status_code" is an integer, and are run any time the response
|
99
|
+
has the status code specified. It's also possible to register an error
|
100
|
+
page for a range of status codes: "error(500..599)".
|
101
|
+
|
102
|
+
* In-file templates are now automatically imported from the file that
|
103
|
+
requires 'sinatra'. The use_in_file_templates! method is still available
|
104
|
+
for loading templates from other files.
|
105
|
+
|
106
|
+
* Sinatra's testing support is no longer dependent on Test::Unit. Requiring
|
107
|
+
'sinatra/test' adds the Sinatra::Test module and Sinatra::TestHarness
|
108
|
+
class, which can be used with any test framework. The 'sinatra/test/unit',
|
109
|
+
'sinatra/test/spec', 'sinatra/test/rspec', or 'sinatra/test/bacon' files
|
110
|
+
can be required to setup a framework-specific testing environment. See the
|
111
|
+
README for more information.
|
112
|
+
|
113
|
+
* Added support for Bacon (test framework). The 'sinatra/test/bacon' file
|
114
|
+
can be required to setup Sinatra test helpers on Bacon::Context.
|
115
|
+
|
116
|
+
* Deprecated "set_option" and "set_options"; use "set" instead.
|
117
|
+
|
118
|
+
* Deprecated the "env" option ("options.env"); use "environment" instead.
|
119
|
+
|
120
|
+
* Deprecated the request level "stop" method; use "halt" instead.
|
121
|
+
|
122
|
+
* Deprecated the request level "entity_tag" method; use "etag" instead.
|
123
|
+
Both "entity_tag" and "etag" were previously supported.
|
124
|
+
|
125
|
+
* Deprecated the request level "headers" method (HTTP response headers);
|
126
|
+
use "response['Header-Name']" instead.
|
127
|
+
|
128
|
+
* Deprecated "Sinatra.application"; use "Sinatra::Application" instead.
|
129
|
+
|
130
|
+
* Deprecated setting Sinatra.application = nil to reset an application.
|
131
|
+
This should no longer be necessary.
|
132
|
+
|
133
|
+
* Deprecated "Sinatra.default_options"; use
|
134
|
+
"Sinatra::Default.set(key, value)" instead.
|
135
|
+
|
136
|
+
* Deprecated the "ServerError" exception. All Exceptions are now
|
137
|
+
treated as internal server errors and result in a 500 response
|
138
|
+
status.
|
139
|
+
|
140
|
+
* Deprecated the "get_it", "post_it", "put_it", "delete_it", and "head_it"
|
141
|
+
test helper methods. Use "get", "post", "put", "delete", and "head",
|
142
|
+
respectively, instead.
|
143
|
+
|
144
|
+
* Removed Event and EventContext classes. Applications are defined in a
|
145
|
+
subclass of Sinatra::Base; each request is processed within an
|
146
|
+
instance.
|
147
|
+
|
148
|
+
= 0.3.3 / 2009-01-06
|
149
|
+
|
150
|
+
* Pin to Rack 0.4.0 (this is the last release on Rack 0.4)
|
151
|
+
|
152
|
+
* Log unhandled exception backtraces to rack.errors.
|
153
|
+
|
154
|
+
* Use RACK_ENV environment variable to establish Sinatra
|
155
|
+
environment when given. Thin sets this when started with
|
156
|
+
the -e argument.
|
157
|
+
|
158
|
+
* BUG: raising Sinatra::NotFound resulted in a 500 response
|
159
|
+
code instead of 404.
|
160
|
+
|
161
|
+
* BUG: use_in_file_templates! fails with CR/LF (#45)
|
162
|
+
|
163
|
+
* BUG: Sinatra detects the app file and root path when run under
|
164
|
+
thin/passenger.
|
165
|
+
|
166
|
+
= 0.3.2
|
167
|
+
|
168
|
+
* BUG: Static and send_file read entire file into String before
|
169
|
+
sending. Updated to stream with 8K chunks instead.
|
170
|
+
|
171
|
+
* Rake tasks and assets for building basic documentation website.
|
172
|
+
See http://sinatra.rubyforge.org
|
173
|
+
|
174
|
+
* Various minor doc fixes.
|
175
|
+
|
176
|
+
= 0.3.1
|
177
|
+
|
178
|
+
* Unbreak optional path parameters [jeremyevans]
|
179
|
+
|
180
|
+
= 0.3.0
|
181
|
+
|
182
|
+
* Add sinatra.gemspec w/ support for github gem builds. Forks can now
|
183
|
+
enable the build gem option in github to get free username-sinatra.gem
|
184
|
+
builds: gem install username-sinatra.gem --source=http://gems.github.com/
|
185
|
+
|
186
|
+
* Require rack-0.4 gem; removes frozen rack dir.
|
187
|
+
|
188
|
+
* Basic RSpec support; require 'sinatra/test/rspec' instead of
|
189
|
+
'sinatra/test/spec' to use. [avdi]
|
190
|
+
|
191
|
+
* before filters can modify request environment vars used for
|
192
|
+
routing (e.g., PATH_INFO, REQUEST_METHOD, etc.) for URL rewriting
|
193
|
+
type functionality.
|
194
|
+
|
195
|
+
* In-file templates now uses @@ instead of ## as template separator.
|
196
|
+
|
197
|
+
* Top-level environment test predicates: development?, test?, production?
|
198
|
+
|
199
|
+
* Top-level "set", "enable", and "disable" methods for tweaking
|
200
|
+
app options. [rtomayko]
|
201
|
+
|
202
|
+
* Top-level "use" method for building Rack middleware pipelines
|
203
|
+
leading to app. See README for usage. [rtomayko]
|
204
|
+
|
205
|
+
* New "reload" option - set false to disable reloading in development.
|
206
|
+
|
207
|
+
* New "host" option - host/ip to bind to [cschneid]
|
208
|
+
|
209
|
+
* New "app_file" option - override the file to reload in development
|
210
|
+
mode [cschneid]
|
211
|
+
|
212
|
+
* Development error/not_found page cleanup [sr, adamwiggins]
|
213
|
+
|
214
|
+
* Remove a bunch of core extensions (String#to_param, String#from_param,
|
215
|
+
Hash#from_params, Hash#to_params, Hash#symbolize_keys, Hash#pass)
|
216
|
+
|
217
|
+
* Various grammar and formatting fixes to README; additions on
|
218
|
+
community and contributing [cypher]
|
219
|
+
|
220
|
+
* Build RDoc using Hanna template: http://sinatrarb.rubyforge.org/api
|
221
|
+
|
222
|
+
* Specs, documentation and fixes for splat'n routes [vic]
|
223
|
+
|
224
|
+
* Fix whitespace errors across all source files. [rtomayko]
|
225
|
+
|
226
|
+
* Fix streaming issues with Mongrel (body not closed). [bmizerany]
|
227
|
+
|
228
|
+
* Fix various issues with environment not being set properly (configure
|
229
|
+
blocks not running, error pages not registering, etc.) [cypher]
|
230
|
+
|
231
|
+
* Fix to allow locals to be passed to ERB templates [cschneid]
|
232
|
+
|
233
|
+
* Fix locking issues causing random errors during reload in development.
|
234
|
+
|
235
|
+
* Fix for escaped paths not resolving static files [Matthew Walker]
|
236
|
+
|
237
|
+
= 0.2.1
|
238
|
+
|
239
|
+
* File upload fix and minor tweaks.
|
240
|
+
|
241
|
+
= 0.2.0
|
242
|
+
|
243
|
+
* Initial gem release of 0.2 codebase.
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2007 Blake Mizerany
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,535 @@
|
|
1
|
+
= Sinatra
|
2
|
+
|
3
|
+
Sinatra is a DSL for quickly creating web-applications in Ruby with minimal
|
4
|
+
effort:
|
5
|
+
|
6
|
+
# myapp.rb
|
7
|
+
require 'rubygems'
|
8
|
+
require 'sinatra'
|
9
|
+
get '/' do
|
10
|
+
'Hello world!'
|
11
|
+
end
|
12
|
+
|
13
|
+
Install the gem and run with:
|
14
|
+
|
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:
|
24
|
+
|
25
|
+
get '/' do
|
26
|
+
.. show something ..
|
27
|
+
end
|
28
|
+
|
29
|
+
post '/' do
|
30
|
+
.. create something ..
|
31
|
+
end
|
32
|
+
|
33
|
+
put '/' do
|
34
|
+
.. update something ..
|
35
|
+
end
|
36
|
+
|
37
|
+
delete '/' do
|
38
|
+
.. annihilate something ..
|
39
|
+
end
|
40
|
+
|
41
|
+
Routes are matched in the order they are defined. The first route that
|
42
|
+
matches the request is invoked.
|
43
|
+
|
44
|
+
Route patterns may include named parameters, accessible via the
|
45
|
+
<tt>params</tt> hash:
|
46
|
+
|
47
|
+
get '/hello/:name' do
|
48
|
+
# matches "GET /foo" and "GET /bar"
|
49
|
+
# params[:name] is 'foo' or 'bar'
|
50
|
+
"Hello #{params[:name]}!"
|
51
|
+
end
|
52
|
+
|
53
|
+
You can also access named parameters via block parameters:
|
54
|
+
|
55
|
+
get '/hello/:name' do |n|
|
56
|
+
"Hello #{n}!"
|
57
|
+
end
|
58
|
+
|
59
|
+
Route patterns may also include splat (or wildcard) parameters, accessible
|
60
|
+
via the <tt>params[:splat]</tt> array.
|
61
|
+
|
62
|
+
get '/say/*/to/*' do
|
63
|
+
# matches /say/hello/to/world
|
64
|
+
params[:splat] # => ["hello", "world"]
|
65
|
+
end
|
66
|
+
|
67
|
+
get '/download/*.*' do
|
68
|
+
# matches /download/path/to/file.xml
|
69
|
+
params[:splat] # => ["path/to/file", "xml"]
|
70
|
+
end
|
71
|
+
|
72
|
+
Route matching with Regular Expressions:
|
73
|
+
|
74
|
+
get %r{/hello/([\w]+)} do
|
75
|
+
"Hello, #{params[:captures].first}!"
|
76
|
+
end
|
77
|
+
|
78
|
+
Or with a block parameter:
|
79
|
+
|
80
|
+
get %r{/hello/([\w]+)} do |c|
|
81
|
+
"Hello, #{c}!"
|
82
|
+
end
|
83
|
+
|
84
|
+
Routes may include a variety of matching conditions, such as the user agent:
|
85
|
+
|
86
|
+
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
|
87
|
+
"You're using Songbird version #{params[:agent][0]}"
|
88
|
+
end
|
89
|
+
|
90
|
+
get '/foo' do
|
91
|
+
# Matches non-songbird browsers
|
92
|
+
end
|
93
|
+
|
94
|
+
== Static Files
|
95
|
+
|
96
|
+
Static files are served from the <tt>./public</tt> directory. You can specify
|
97
|
+
a different location by setting the <tt>:public</tt> option:
|
98
|
+
|
99
|
+
set :public, File.dirname(__FILE__) + '/static'
|
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
|
+
|
105
|
+
== Views / Templates
|
106
|
+
|
107
|
+
Templates are assumed to be located directly under the <tt>./views</tt>
|
108
|
+
directory. To use a different views directory:
|
109
|
+
|
110
|
+
set :views, File.dirname(__FILE__) + '/templates'
|
111
|
+
|
112
|
+
=== Haml Templates
|
113
|
+
|
114
|
+
The haml gem/library is required to render HAML templates:
|
115
|
+
|
116
|
+
get '/' do
|
117
|
+
haml :index
|
118
|
+
end
|
119
|
+
|
120
|
+
Renders <tt>./views/index.haml</tt>.
|
121
|
+
|
122
|
+
=== Erb Templates
|
123
|
+
|
124
|
+
get '/' do
|
125
|
+
erb :index
|
126
|
+
end
|
127
|
+
|
128
|
+
Renders <tt>./views/index.erb</tt>
|
129
|
+
|
130
|
+
=== Builder Templates
|
131
|
+
|
132
|
+
The builder gem/library is required to render builder templates:
|
133
|
+
|
134
|
+
get '/' do
|
135
|
+
content_type 'application/xml', :charset => 'utf-8'
|
136
|
+
builder :index
|
137
|
+
end
|
138
|
+
|
139
|
+
Renders <tt>./views/index.builder</tt>.
|
140
|
+
|
141
|
+
=== Sass Templates
|
142
|
+
|
143
|
+
The sass gem/library is required to render Sass templates:
|
144
|
+
|
145
|
+
get '/stylesheet.css' do
|
146
|
+
content_type 'text/css', :charset => 'utf-8'
|
147
|
+
sass :stylesheet
|
148
|
+
end
|
149
|
+
|
150
|
+
Renders <tt>./views/stylesheet.sass</tt>.
|
151
|
+
|
152
|
+
=== Inline Templates
|
153
|
+
|
154
|
+
get '/' do
|
155
|
+
haml '%div.title Hello World'
|
156
|
+
end
|
157
|
+
|
158
|
+
Renders the inlined template string.
|
159
|
+
|
160
|
+
=== Accessing Variables in Templates
|
161
|
+
|
162
|
+
Templates are evaluated within the same context as the route blocks. Instance
|
163
|
+
variables set in route blocks are available in templates:
|
164
|
+
|
165
|
+
get '/:id' do
|
166
|
+
@foo = Foo.find(params[:id])
|
167
|
+
haml '%h1= @foo.name'
|
168
|
+
end
|
169
|
+
|
170
|
+
Or, specify an explicit Hash of local variables:
|
171
|
+
|
172
|
+
get '/:id' do
|
173
|
+
foo = Foo.find(params[:id])
|
174
|
+
haml '%h1= foo.name', :locals => { :foo => foo }
|
175
|
+
end
|
176
|
+
|
177
|
+
This is typically used when rendering templates as partials from within
|
178
|
+
other templates.
|
179
|
+
|
180
|
+
=== In-file Templates
|
181
|
+
|
182
|
+
Templates may be defined at the end of the source file:
|
183
|
+
|
184
|
+
require 'rubygems'
|
185
|
+
require 'sinatra'
|
186
|
+
|
187
|
+
get '/' do
|
188
|
+
haml :index
|
189
|
+
end
|
190
|
+
|
191
|
+
__END__
|
192
|
+
|
193
|
+
@@ layout
|
194
|
+
%html
|
195
|
+
= yield
|
196
|
+
|
197
|
+
@@ index
|
198
|
+
%div.title Hello world!!!!!
|
199
|
+
|
200
|
+
NOTE: In-file templates defined in the source file that requires sinatra
|
201
|
+
are automatically loaded. Call the <tt>use_in_file_templates!</tt>
|
202
|
+
method explicitly if you have in-file templates in another source file.
|
203
|
+
|
204
|
+
=== Named Templates
|
205
|
+
|
206
|
+
It's possible to define named templates using the top-level <tt>template</tt>
|
207
|
+
method:
|
208
|
+
|
209
|
+
template :layout do
|
210
|
+
"%html\n =yield\n"
|
211
|
+
end
|
212
|
+
|
213
|
+
template :index do
|
214
|
+
'%div.title Hello World!'
|
215
|
+
end
|
216
|
+
|
217
|
+
get '/' do
|
218
|
+
haml :index
|
219
|
+
end
|
220
|
+
|
221
|
+
If a template named "layout" exists, it will be used each time a template
|
222
|
+
is rendered. You can disable layouts by passing <tt>:layout => false</tt>.
|
223
|
+
|
224
|
+
get '/' do
|
225
|
+
haml :index, :layout => !request.xhr?
|
226
|
+
end
|
227
|
+
|
228
|
+
== Helpers
|
229
|
+
|
230
|
+
Use the top-level <tt>helpers</tt> method to define helper methods for use in
|
231
|
+
route blocks and templates:
|
232
|
+
|
233
|
+
helpers do
|
234
|
+
def bar(name)
|
235
|
+
"#{name}bar"
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
get '/:name' do
|
240
|
+
bar(params[:name])
|
241
|
+
end
|
242
|
+
|
243
|
+
== Filters
|
244
|
+
|
245
|
+
Before filters are evaluated before each request within the context of the
|
246
|
+
request and can modify the request and response. Instance variables set in
|
247
|
+
filters are accessible by routes and templates.
|
248
|
+
|
249
|
+
before do
|
250
|
+
@note = 'Hi!'
|
251
|
+
request.path_info = '/foo/bar/baz'
|
252
|
+
end
|
253
|
+
|
254
|
+
get '/foo/*' do
|
255
|
+
@note #=> 'Hi!'
|
256
|
+
params[:splat] #=> 'bar/baz'
|
257
|
+
end
|
258
|
+
|
259
|
+
== Halting
|
260
|
+
|
261
|
+
To immediately stop a request during a before filter or route use:
|
262
|
+
|
263
|
+
halt
|
264
|
+
|
265
|
+
You can also specify a body when halting ...
|
266
|
+
|
267
|
+
halt 'this will be the body'
|
268
|
+
|
269
|
+
Or set the status and body ...
|
270
|
+
|
271
|
+
halt 401, 'go away!'
|
272
|
+
|
273
|
+
== Passing
|
274
|
+
|
275
|
+
A route can punt processing to the next matching route using the <tt>pass</tt>
|
276
|
+
statement:
|
277
|
+
|
278
|
+
get '/guess/:who' do
|
279
|
+
pass unless params[:who] == 'Frank'
|
280
|
+
"You got me!"
|
281
|
+
end
|
282
|
+
|
283
|
+
get '/guess/*' do
|
284
|
+
"You missed!"
|
285
|
+
end
|
286
|
+
|
287
|
+
The route block is immediately exited and control continues with the next
|
288
|
+
matching route. If no matching route is found, a 404 is returned.
|
289
|
+
|
290
|
+
== Configuration and Reloading
|
291
|
+
|
292
|
+
Sinatra supports multiple environments and reloading. Reloading happens
|
293
|
+
before each request when running under the <tt>:development</tt>
|
294
|
+
environment. Wrap your configurations (e.g., database connections, constants,
|
295
|
+
etc.) in <tt>configure</tt> blocks to protect them from reloading or to
|
296
|
+
target specific environments.
|
297
|
+
|
298
|
+
Run once, at startup, in any environment:
|
299
|
+
|
300
|
+
configure do
|
301
|
+
...
|
302
|
+
end
|
303
|
+
|
304
|
+
Run only when the environment (RACK_ENV environment variable) is set to
|
305
|
+
<tt>:production</tt>.
|
306
|
+
|
307
|
+
configure :production do
|
308
|
+
...
|
309
|
+
end
|
310
|
+
|
311
|
+
Run when the environment (RACK_ENV environment variable) is set to
|
312
|
+
either <tt>:production</tt> or <tt>:test</tt>.
|
313
|
+
|
314
|
+
configure :production, :test do
|
315
|
+
...
|
316
|
+
end
|
317
|
+
|
318
|
+
== Error handling
|
319
|
+
|
320
|
+
Error handlers run within the same context as routes and before filters, which
|
321
|
+
means you get all the goodies it has to offer, like <tt>haml</tt>, <tt>erb</tt>,
|
322
|
+
<tt>halt</tt>, etc.
|
323
|
+
|
324
|
+
=== Not Found
|
325
|
+
|
326
|
+
When a <tt>Sinatra::NotFound</tt> exception is raised, or the response's status
|
327
|
+
code is 404, the <tt>not_found</tt> handler is invoked:
|
328
|
+
|
329
|
+
not_found do
|
330
|
+
'This is nowhere to be found'
|
331
|
+
end
|
332
|
+
|
333
|
+
=== Error
|
334
|
+
|
335
|
+
The +error+ handler is invoked any time an exception is raised from a route
|
336
|
+
block or before filter. The exception object can be obtained from the
|
337
|
+
<tt>sinatra.error</tt> Rack variable:
|
338
|
+
|
339
|
+
error do
|
340
|
+
'Sorry there was a nasty error - ' + env['sinatra.error'].name
|
341
|
+
end
|
342
|
+
|
343
|
+
Custom errors:
|
344
|
+
|
345
|
+
error MyCustomError do
|
346
|
+
'So what happened was...' + request.env['sinatra.error'].message
|
347
|
+
end
|
348
|
+
|
349
|
+
Then, if this happens:
|
350
|
+
|
351
|
+
get '/' do
|
352
|
+
raise MyCustomError, 'something bad'
|
353
|
+
end
|
354
|
+
|
355
|
+
You get this:
|
356
|
+
|
357
|
+
So what happened was... something bad
|
358
|
+
|
359
|
+
Sinatra installs special <tt>not_found</tt> and <tt>error</tt> handlers when
|
360
|
+
running under the development environment.
|
361
|
+
|
362
|
+
== Mime types
|
363
|
+
|
364
|
+
When using <tt>send_file</tt> or static files you may have mime types Sinatra
|
365
|
+
doesn't understand. Use +mime+ to register them by file extension:
|
366
|
+
|
367
|
+
mime :foo, 'text/foo'
|
368
|
+
|
369
|
+
== Rack Middleware
|
370
|
+
|
371
|
+
Sinatra rides on Rack[http://rack.rubyforge.org/], a minimal standard
|
372
|
+
interface for Ruby web frameworks. One of Rack's most interesting capabilities
|
373
|
+
for application developers is support for "middleware" -- components that sit
|
374
|
+
between the server and your application monitoring and/or manipulating the
|
375
|
+
HTTP request/response to provide various types of common functionality.
|
376
|
+
|
377
|
+
Sinatra makes building Rack middleware pipelines a cinch via a top-level
|
378
|
+
+use+ method:
|
379
|
+
|
380
|
+
require 'sinatra'
|
381
|
+
require 'my_custom_middleware'
|
382
|
+
|
383
|
+
use Rack::Lint
|
384
|
+
use MyCustomMiddleware
|
385
|
+
|
386
|
+
get '/hello' do
|
387
|
+
'Hello World'
|
388
|
+
end
|
389
|
+
|
390
|
+
The semantics of +use+ are identical to those defined for the
|
391
|
+
Rack::Builder[http://rack.rubyforge.org/doc/classes/Rack/Builder.html] DSL
|
392
|
+
(most frequently used from rackup files). For example, the +use+ method
|
393
|
+
accepts multiple/variable args as well as blocks:
|
394
|
+
|
395
|
+
use Rack::Auth::Basic do |username, password|
|
396
|
+
username == 'admin' && password == 'secret'
|
397
|
+
end
|
398
|
+
|
399
|
+
Rack is distributed with a variety of standard middleware for logging,
|
400
|
+
debugging, URL routing, authentication, and session handling. Sinatra uses
|
401
|
+
many of of these components automatically based on configuration so you
|
402
|
+
typically don't have to +use+ them explicitly.
|
403
|
+
|
404
|
+
== Testing
|
405
|
+
|
406
|
+
The Sinatra::Test module includes a variety of helper methods for testing
|
407
|
+
your Sinatra app. Sinatra includes support for Test::Unit, test-spec, RSpec,
|
408
|
+
and Bacon through separate source files.
|
409
|
+
|
410
|
+
=== Test::Unit
|
411
|
+
|
412
|
+
require 'sinatra'
|
413
|
+
require 'sinatra/test/unit'
|
414
|
+
require 'my_sinatra_app'
|
415
|
+
|
416
|
+
class MyAppTest < Test::Unit::TestCase
|
417
|
+
def test_my_default
|
418
|
+
get '/'
|
419
|
+
assert_equal 'My Default Page!', @response.body
|
420
|
+
end
|
421
|
+
|
422
|
+
def test_with_agent
|
423
|
+
get '/', :env => { :agent => 'Songbird' }
|
424
|
+
assert_equal "You're in Songbird!", @response.body
|
425
|
+
end
|
426
|
+
|
427
|
+
...
|
428
|
+
end
|
429
|
+
|
430
|
+
=== Test::Spec
|
431
|
+
|
432
|
+
Install the test-spec gem and require <tt>'sinatra/test/spec'</tt> before
|
433
|
+
your app:
|
434
|
+
|
435
|
+
require 'sinatra'
|
436
|
+
require 'sinatra/test/spec'
|
437
|
+
require 'my_sinatra_app'
|
438
|
+
|
439
|
+
describe 'My app' do
|
440
|
+
it "should show a default page" do
|
441
|
+
get '/'
|
442
|
+
should.be.ok
|
443
|
+
body.should.equal 'My Default Page!'
|
444
|
+
end
|
445
|
+
|
446
|
+
...
|
447
|
+
end
|
448
|
+
|
449
|
+
=== RSpec
|
450
|
+
|
451
|
+
Install the rspec gem and require <tt>'sinatra/test/rspec'</tt> before
|
452
|
+
your app:
|
453
|
+
|
454
|
+
require 'sinatra'
|
455
|
+
require 'sinatra/test/rspec'
|
456
|
+
require 'my_sinatra_app'
|
457
|
+
|
458
|
+
describe 'My app' do
|
459
|
+
it 'should show a default page' do
|
460
|
+
get '/'
|
461
|
+
@response.should be_ok
|
462
|
+
@response.body.should == 'My Default Page!'
|
463
|
+
end
|
464
|
+
|
465
|
+
...
|
466
|
+
|
467
|
+
end
|
468
|
+
|
469
|
+
=== Bacon
|
470
|
+
|
471
|
+
require 'sinatra'
|
472
|
+
require 'sinatra/test/bacon'
|
473
|
+
require 'my_sinatra_app'
|
474
|
+
|
475
|
+
describe 'My app' do
|
476
|
+
it 'should be ok' do
|
477
|
+
get '/'
|
478
|
+
should.be.ok
|
479
|
+
body.should == 'Im OK'
|
480
|
+
end
|
481
|
+
end
|
482
|
+
|
483
|
+
See Sinatra::Test for more information on +get+, +post+, +put+, and
|
484
|
+
friends.
|
485
|
+
|
486
|
+
== Command line
|
487
|
+
|
488
|
+
Sinatra applications can be run directly:
|
489
|
+
|
490
|
+
ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-s HANDLER]
|
491
|
+
|
492
|
+
Options are:
|
493
|
+
|
494
|
+
-h # help
|
495
|
+
-p # set the port (default is 4567)
|
496
|
+
-e # set the environment (default is development)
|
497
|
+
-s # specify rack server/handler (default is thin)
|
498
|
+
-x # turn on the mutex lock (default is off)
|
499
|
+
|
500
|
+
== The Bleeding Edge
|
501
|
+
|
502
|
+
If you would like to use Sinatra's latest bleeding code, create a local
|
503
|
+
clone and run your app with the <tt>sinatra/lib</tt> directory on the
|
504
|
+
<tt>LOAD_PATH</tt>:
|
505
|
+
|
506
|
+
cd myapp
|
507
|
+
git clone git://github.com/sinatra/sinatra.git
|
508
|
+
ruby -Isinatra/lib myapp.rb
|
509
|
+
|
510
|
+
Alternatively, you can add the <tt>sinatra/lib<tt> directory to the
|
511
|
+
<tt>LOAD_PATH</tt> in your application:
|
512
|
+
|
513
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
|
514
|
+
require 'rubygems'
|
515
|
+
require 'sinatra'
|
516
|
+
|
517
|
+
get '/about' do
|
518
|
+
"I'm running version " + Sinatra::VERSION
|
519
|
+
end
|
520
|
+
|
521
|
+
To update the Sinatra sources in the future:
|
522
|
+
|
523
|
+
cd myproject/sinatra
|
524
|
+
git pull
|
525
|
+
|
526
|
+
== More
|
527
|
+
|
528
|
+
* {Project Website}[http://sinatra.github.com/] - Additional documentation,
|
529
|
+
news, and links to other resources.
|
530
|
+
* {Contributing}[http://sinatra.github.com/contributing.html] - Find a bug? Need
|
531
|
+
help? Have a patch?
|
532
|
+
* {Lighthouse}[http://sinatra.lighthouseapp.com] - Issue tracking and release
|
533
|
+
planning.
|
534
|
+
* {Mailing List}[http://groups.google.com/group/sinatrarb]
|
535
|
+
* {IRC: #sinatra}[irc://chat.freenode.net/#sinatra] on http://freenode.net
|