bmizerany-sinatra 0.3.2 → 0.8.9
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +40 -0
- data/CHANGES +174 -0
- data/README.rdoc +138 -116
- data/Rakefile +27 -9
- data/{test → compat}/app_test.rb +11 -10
- data/{test → compat}/application_test.rb +21 -5
- data/compat/builder_test.rb +101 -0
- data/{test → compat}/custom_error_test.rb +0 -0
- data/compat/erb_test.rb +136 -0
- data/{test → compat}/events_test.rb +16 -3
- 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/{test → compat}/pipeline_test.rb +9 -4
- data/{test → compat}/public/foo.xml +0 -0
- data/compat/sass_test.rb +57 -0
- data/{test → compat}/sessions_test.rb +0 -0
- data/{test → compat}/streaming_test.rb +4 -1
- data/{test → compat}/sym_params_test.rb +0 -0
- data/{test → compat}/template_test.rb +0 -0
- data/{test → compat}/use_in_file_templates_test.rb +0 -0
- data/{test → compat}/views/foo.builder +0 -0
- data/{test → compat}/views/foo.erb +0 -0
- data/{test → compat}/views/foo.haml +0 -0
- data/{test → compat}/views/foo.sass +0 -0
- data/{test → compat}/views/foo_layout.erb +0 -0
- data/{test → compat}/views/foo_layout.haml +0 -0
- data/{test → compat}/views/layout_test/foo.builder +0 -0
- data/{test → compat}/views/layout_test/foo.erb +0 -0
- data/{test → compat}/views/layout_test/foo.haml +0 -0
- data/{test → compat}/views/layout_test/foo.sass +0 -0
- data/{test → compat}/views/layout_test/layout.builder +0 -0
- data/{test → compat}/views/layout_test/layout.erb +0 -0
- data/{test → compat}/views/layout_test/layout.haml +0 -0
- data/{test → compat}/views/layout_test/layout.sass +0 -0
- data/{test → compat}/views/no_layout/no_layout.builder +0 -0
- data/{test → compat}/views/no_layout/no_layout.haml +0 -0
- data/lib/sinatra/base.rb +818 -0
- data/lib/sinatra/compat.rb +239 -0
- data/{images → lib/sinatra/images}/404.png +0 -0
- data/{images → lib/sinatra/images}/500.png +0 -0
- data/lib/sinatra/main.rb +48 -0
- data/lib/sinatra/test/bacon.rb +17 -0
- data/lib/sinatra/test/rspec.rb +7 -8
- data/lib/sinatra/test/spec.rb +3 -4
- data/lib/sinatra/test/unit.rb +3 -5
- data/lib/sinatra/test.rb +109 -0
- data/lib/sinatra.rb +4 -1466
- data/sinatra.gemspec +67 -35
- data/test/base_test.rb +68 -0
- data/test/builder_test.rb +50 -87
- data/test/data/reload_app_file.rb +3 -0
- data/test/erb_test.rb +38 -124
- data/test/filter_test.rb +27 -22
- data/test/haml_test.rb +51 -216
- data/test/helper.rb +18 -5
- data/test/helpers_test.rb +361 -0
- data/test/mapped_error_test.rb +137 -49
- data/test/middleware_test.rb +58 -0
- data/test/options_test.rb +97 -0
- data/test/reload_test.rb +61 -0
- data/test/request_test.rb +9 -0
- data/test/result_test.rb +88 -0
- data/test/routing_test.rb +334 -0
- data/test/sass_test.rb +27 -48
- data/test/sinatra_test.rb +13 -0
- data/test/static_test.rb +57 -0
- data/test/templates_test.rb +88 -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 +78 -46
- data/ChangeLog +0 -78
- data/lib/sinatra/test/methods.rb +0 -76
- data/test/event_context_test.rb +0 -15
data/AUTHORS
ADDED
@@ -0,0 +1,40 @@
|
|
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 <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.
|
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 just now: http://www.youtube.com/watch?v=ueaHLHgskkw.
|
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 Olsen (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
|
+
|
37
|
+
and last but not least:
|
38
|
+
|
39
|
+
* Frank Sinatra (chairman of the board) for having so much class he
|
40
|
+
deserves a web-framework named after him.
|
data/CHANGES
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
= 0.9.0 (unreleased)
|
2
|
+
|
3
|
+
* Works with and requires Rack >= 0.9.1
|
4
|
+
|
5
|
+
* Multiple Sinatra applications can now co-exist peacefully within a
|
6
|
+
single process. The new "Sinatra::Base" class can be subclassed to
|
7
|
+
establish a blank-slate Rack application or middleware component.
|
8
|
+
Documentation on using these features is forth-coming; the following
|
9
|
+
provides the basic gist: http://gist.github.com/38605
|
10
|
+
|
11
|
+
* Regular expressions may now be used in route pattens; captures are
|
12
|
+
available at "params[:captures]".
|
13
|
+
|
14
|
+
* New ":provides" route condition takes an array of mime types and
|
15
|
+
matches only when an Accept request header is present with a
|
16
|
+
corresponding type. [cypher]
|
17
|
+
|
18
|
+
* New request-level "pass" method; immediately exits the current block
|
19
|
+
and passes control to the next matching route.
|
20
|
+
|
21
|
+
* The request-level "body" method now takes a block; evaluation is
|
22
|
+
deferred until an attempt is made to read the body. The block must
|
23
|
+
return a String or Array.
|
24
|
+
|
25
|
+
* New "route conditions" system for attaching rules for when a route
|
26
|
+
matches. The :agent and :host route options now use this system.
|
27
|
+
|
28
|
+
* New "dump_errors" option controls whether the backtrace is dumped to
|
29
|
+
rack.errors when an exception is raised from a route. The option is
|
30
|
+
enabled by default for top-level apps.
|
31
|
+
|
32
|
+
* Better default "app_file", "root", "public", and "views" location
|
33
|
+
detection; changes to "root" and "app_file" automatically cascade to
|
34
|
+
other options that depend on them.
|
35
|
+
|
36
|
+
* Error mappings are now split into two distinct layers: exception
|
37
|
+
mappings and custom error pages. Exception mappings are registered
|
38
|
+
with "error(Exception)" and are run only when the app raises an
|
39
|
+
exception. Custom error pages are registered with "error(status_code)",
|
40
|
+
where "status_code" is an integer, and are run any time the response
|
41
|
+
has the status code specified. It's also possible to register an error
|
42
|
+
page for a range of status codes: "error(500..599)".
|
43
|
+
|
44
|
+
* Added support for Bacon (test framework). The 'sinatra/test/bacon' file
|
45
|
+
can be required to setup Sinatra test helpers on Bacon::Context.
|
46
|
+
|
47
|
+
* Deprecated "set_option" and "set_options"; use "set" instead.
|
48
|
+
|
49
|
+
* Deprecated the "env" option ("options.env"); use "environment" instead.
|
50
|
+
|
51
|
+
* Deprecated the request level "stop" method; use "halt" instead.
|
52
|
+
|
53
|
+
* Deprecated the request level "entity_tag" method; use "etag" instead.
|
54
|
+
Both "entity_tag" and "etag" were previously supported.
|
55
|
+
|
56
|
+
* Deprecated the request level "headers" method (HTTP response headers);
|
57
|
+
use "response['Header-Name']" instead.
|
58
|
+
|
59
|
+
* Deprecated "Sinatra.application"; use "Sinatra::Application" instead.
|
60
|
+
|
61
|
+
* Deprecated setting Sinatra.application = nil to reset an application.
|
62
|
+
This should no longer be necessary.
|
63
|
+
|
64
|
+
* Deprecated "Sinatra.default_options"; use
|
65
|
+
"Sinatra::Default.set(key, value)" instead.
|
66
|
+
|
67
|
+
* Deprecated the "ServerError" exception. All Exceptions are now
|
68
|
+
treated as internal server errors and result in a 500 response
|
69
|
+
status.
|
70
|
+
|
71
|
+
* Deprecated the "get_it", "post_it", "put_it", "delete_it", and "head_it"
|
72
|
+
test helper methods. Use "get", "post", "put", "delete", and "head",
|
73
|
+
respectively, instead.
|
74
|
+
|
75
|
+
* Removed Event and EventContext classes. Applications are defined in a
|
76
|
+
subclass of Sinatra::Base; each request is processed within an
|
77
|
+
instance.
|
78
|
+
|
79
|
+
= 0.3.3 / 2009-01-06
|
80
|
+
|
81
|
+
* Pin to Rack 0.4.0 (this is the last release on Rack 0.4)
|
82
|
+
|
83
|
+
* Log unhandled exception backtraces to rack.errors.
|
84
|
+
|
85
|
+
* Use RACK_ENV environment variable to establish Sinatra
|
86
|
+
environment when given. Thin sets this when started with
|
87
|
+
the -e argument.
|
88
|
+
|
89
|
+
* BUG: raising Sinatra::NotFound resulted in a 500 response
|
90
|
+
code instead of 404.
|
91
|
+
|
92
|
+
* BUG: use_in_file_templates! fails with CR/LF (#45)
|
93
|
+
|
94
|
+
* BUG: Sinatra detects the app file and root path when run under
|
95
|
+
thin/passenger.
|
96
|
+
|
97
|
+
= 0.3.2
|
98
|
+
|
99
|
+
* BUG: Static and send_file read entire file into String before
|
100
|
+
sending. Updated to stream with 8K chunks instead.
|
101
|
+
|
102
|
+
* Rake tasks and assets for building basic documentation website.
|
103
|
+
See http://sinatra.rubyforge.org
|
104
|
+
|
105
|
+
* Various minor doc fixes.
|
106
|
+
|
107
|
+
= 0.3.1
|
108
|
+
|
109
|
+
* Unbreak optional path parameters [jeremyevans]
|
110
|
+
|
111
|
+
= 0.3.0
|
112
|
+
|
113
|
+
* Add sinatra.gemspec w/ support for github gem builds. Forks can now
|
114
|
+
enable the build gem option in github to get free username-sinatra.gem
|
115
|
+
builds: gem install username-sinatra.gem --source=http://gems.github.com/
|
116
|
+
|
117
|
+
* Require rack-0.4 gem; removes frozen rack dir.
|
118
|
+
|
119
|
+
* Basic RSpec support; require 'sinatra/test/rspec' instead of
|
120
|
+
'sinatra/test/spec' to use. [avdi]
|
121
|
+
|
122
|
+
* before filters can modify request environment vars used for
|
123
|
+
routing (e.g., PATH_INFO, REQUEST_METHOD, etc.) for URL rewriting
|
124
|
+
type functionality.
|
125
|
+
|
126
|
+
* In-file templates now uses @@ instead of ## as template separator.
|
127
|
+
|
128
|
+
* Top-level environment test predicates: development?, test?, production?
|
129
|
+
|
130
|
+
* Top-level "set", "enable", and "disable" methods for tweaking
|
131
|
+
app options. [rtomayko]
|
132
|
+
|
133
|
+
* Top-level "use" method for building Rack middleware pipelines
|
134
|
+
leading to app. See README for usage. [rtomayko]
|
135
|
+
|
136
|
+
* New "reload" option - set false to disable reloading in development.
|
137
|
+
|
138
|
+
* New "host" option - host/ip to bind to [cschneid]
|
139
|
+
|
140
|
+
* New "app_file" option - override the file to reload in development
|
141
|
+
mode [cschneid]
|
142
|
+
|
143
|
+
* Development error/not_found page cleanup [sr, adamwiggins]
|
144
|
+
|
145
|
+
* Remove a bunch of core extensions (String#to_param, String#from_param,
|
146
|
+
Hash#from_params, Hash#to_params, Hash#symbolize_keys, Hash#pass)
|
147
|
+
|
148
|
+
* Various grammar and formatting fixes to README; additions on
|
149
|
+
community and contributing [cypher]
|
150
|
+
|
151
|
+
* Build RDoc using Hanna template: http://sinatrarb.rubyforge.org/api
|
152
|
+
|
153
|
+
* Specs, documentation and fixes for splat'n routes [vic]
|
154
|
+
|
155
|
+
* Fix whitespace errors across all source files. [rtomayko]
|
156
|
+
|
157
|
+
* Fix streaming issues with Mongrel (body not closed). [bmizerany]
|
158
|
+
|
159
|
+
* Fix various issues with environment not being set properly (configure
|
160
|
+
blocks not running, error pages not registering, etc.) [cypher]
|
161
|
+
|
162
|
+
* Fix to allow locals to be passed to ERB templates [cschneid]
|
163
|
+
|
164
|
+
* Fix locking issues causing random errors during reload in development.
|
165
|
+
|
166
|
+
* Fix for escaped paths not resolving static files [Matthew Walker]
|
167
|
+
|
168
|
+
= 0.2.1
|
169
|
+
|
170
|
+
* File upload fix and minor tweaks.
|
171
|
+
|
172
|
+
= 0.2.0
|
173
|
+
|
174
|
+
* Initial gem release of 0.2 codebase.
|
data/README.rdoc
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
= Sinatra
|
2
2
|
|
3
3
|
Sinatra is a DSL for quickly creating web-applications in Ruby with minimal
|
4
|
-
effort
|
5
|
-
|
6
|
-
== Sample App
|
4
|
+
effort:
|
7
5
|
|
8
6
|
# myapp.rb
|
9
7
|
require 'rubygems'
|
@@ -32,78 +30,80 @@ Run with <tt>ruby myapp.rb</tt> and view at <tt>http://localhost:4567</tt>
|
|
32
30
|
.. annihilate something ..
|
33
31
|
end
|
34
32
|
|
35
|
-
head '/' do
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
NOTE: <tt>put</tt> and <tt>delete</tt> are also triggered when a
|
40
|
-
<tt>_method</tt> parameter is set to PUT or DELETE and the HTTP request method
|
41
|
-
is POST
|
42
|
-
|
43
33
|
== Routes
|
44
34
|
|
45
35
|
Routes are matched based on the order of declaration. The first route that
|
46
36
|
matches the request is invoked.
|
47
37
|
|
48
|
-
|
38
|
+
Basic routes:
|
49
39
|
|
50
40
|
get '/hi' do
|
51
41
|
...
|
52
42
|
end
|
53
43
|
|
54
|
-
|
44
|
+
Route patterns may include named parameters, accessible via the
|
45
|
+
<tt>params</tt> hash:
|
55
46
|
|
56
47
|
get '/:name' do
|
57
|
-
# matches /
|
48
|
+
# matches "GET /foo" and "GET /bar"
|
49
|
+
# params[:name] is 'foo' or 'bar'
|
50
|
+
"Hello #{params[:name]}!"
|
58
51
|
end
|
59
52
|
|
60
|
-
|
53
|
+
Route patterns may also include splat (or wildcard) parameters, accessible
|
54
|
+
via the <tt>params[:splat]</tt> array.
|
61
55
|
|
62
56
|
get '/say/*/to/*' do
|
63
57
|
# matches /say/hello/to/world
|
64
|
-
params[
|
58
|
+
params[:splat] # => ["hello", "world"]
|
65
59
|
end
|
66
60
|
|
67
61
|
get '/download/*.*' do
|
68
62
|
# matches /download/path/to/file.xml
|
69
|
-
params[
|
63
|
+
params[:splat] # => ["path/to/file", "xml"]
|
64
|
+
end
|
65
|
+
|
66
|
+
Route matching with Regular Expressions:
|
67
|
+
|
68
|
+
get %r{/hello/([\w]+)} do
|
69
|
+
"Hello, #{params[:captures].first}!"
|
70
70
|
end
|
71
71
|
|
72
|
-
|
72
|
+
Routes may include a variety of matching conditions, such as the user agent:
|
73
73
|
|
74
74
|
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
|
75
75
|
"You're using Songbird version #{params[:agent][0]}"
|
76
76
|
end
|
77
77
|
|
78
78
|
get '/foo' do
|
79
|
-
#
|
79
|
+
# Matches non-songbird browsers
|
80
80
|
end
|
81
81
|
|
82
|
-
== Static
|
82
|
+
== Static Files
|
83
83
|
|
84
|
-
|
84
|
+
Static files are served from the <tt>./public</tt> directory. You can specify
|
85
|
+
a different location by setting the <tt>:public</tt> option:
|
85
86
|
|
86
|
-
|
87
|
-
\ public
|
87
|
+
set :public, File.dirname(__FILE__) + '/static'
|
88
88
|
|
89
|
-
|
90
|
-
request ends. Otherwise, Sinatra will look for an event that matches the
|
91
|
-
path.
|
89
|
+
== Views / Templates
|
92
90
|
|
93
|
-
|
91
|
+
Templates are assumed to be located directly under a <tt>./views</tt>
|
92
|
+
directory. To use a different views directory:
|
94
93
|
|
95
|
-
|
96
|
-
your main application.
|
94
|
+
set :views, File.dirname(__FILE__) + '/templates'
|
97
95
|
|
98
96
|
=== Haml Templates
|
99
97
|
|
98
|
+
The haml gem/library is required to render HAML templates:
|
99
|
+
|
100
100
|
get '/' do
|
101
101
|
haml :index
|
102
102
|
end
|
103
103
|
|
104
104
|
Renders <tt>./views/index.haml</tt>.
|
105
105
|
|
106
|
-
=== Erb
|
106
|
+
=== Erb Templates
|
107
107
|
|
108
108
|
get '/' do
|
109
109
|
erb :index
|
@@ -111,11 +111,20 @@ Renders <tt>./views/index.haml</tt>.
|
|
111
111
|
|
112
112
|
Renders <tt>./views/index.erb</tt>
|
113
113
|
|
114
|
-
=== Builder
|
114
|
+
=== Builder Templates
|
115
|
+
|
116
|
+
The builder gem/library is required to render builder templates:
|
117
|
+
|
118
|
+
get '/' do
|
119
|
+
content_type 'application/xml', :charset => 'utf-8'
|
120
|
+
builder :index
|
121
|
+
end
|
122
|
+
|
123
|
+
Renders <tt>./views/index.builder</tt>.
|
115
124
|
|
116
|
-
|
125
|
+
=== Sass Templates
|
117
126
|
|
118
|
-
|
127
|
+
The sass gem/library is required to render Sass templates:
|
119
128
|
|
120
129
|
get '/stylesheet.css' do
|
121
130
|
content_type 'text/css', :charset => 'utf-8'
|
@@ -134,20 +143,19 @@ Renders the inlined template string.
|
|
134
143
|
|
135
144
|
=== Accessing Variables
|
136
145
|
|
137
|
-
Templates are evaluated within the
|
138
|
-
|
139
|
-
blocks can be accessed direcly in views:
|
146
|
+
Templates are evaluated within the same context as the route blocks. Instance
|
147
|
+
variables set in route blocks are available in templates:
|
140
148
|
|
141
149
|
get '/:id' do
|
142
150
|
@foo = Foo.find(params[:id])
|
143
|
-
haml '%h1
|
151
|
+
haml '%h1= @foo.name'
|
144
152
|
end
|
145
153
|
|
146
154
|
Or, specify an explicit Hash of local variables:
|
147
155
|
|
148
156
|
get '/:id' do
|
149
157
|
foo = Foo.find(params[:id])
|
150
|
-
haml '%h1
|
158
|
+
haml '%h1= foo.name', :locals => { :foo => foo }
|
151
159
|
end
|
152
160
|
|
153
161
|
This is typically used when rendering templates as partials from within
|
@@ -166,9 +174,8 @@ Templates may be defined at the end of the source file:
|
|
166
174
|
__END__
|
167
175
|
|
168
176
|
@@ layout
|
169
|
-
|
170
|
-
|
171
|
-
X
|
177
|
+
%html
|
178
|
+
= yield
|
172
179
|
|
173
180
|
@@ index
|
174
181
|
%div.title Hello world!!!!!
|
@@ -177,7 +184,7 @@ It's also possible to define named templates using the top-level template
|
|
177
184
|
method:
|
178
185
|
|
179
186
|
template :layout do
|
180
|
-
"
|
187
|
+
"%html\n =yield\n"
|
181
188
|
end
|
182
189
|
|
183
190
|
template :index do
|
@@ -188,10 +195,17 @@ method:
|
|
188
195
|
haml :index
|
189
196
|
end
|
190
197
|
|
198
|
+
If a template named "layout" exists, it will be used each time a template
|
199
|
+
is rendered. You can disable layouts by passing <tt>:layout => false</tt>.
|
200
|
+
|
201
|
+
get '/' do
|
202
|
+
haml :index, :layout => !request.xhr?
|
203
|
+
end
|
204
|
+
|
191
205
|
== Helpers
|
192
206
|
|
193
|
-
|
194
|
-
|
207
|
+
Use the top-level <tt>helpers</tt> method to define helper methods for use in
|
208
|
+
route blocks and templates:
|
195
209
|
|
196
210
|
helpers do
|
197
211
|
def bar(name)
|
@@ -205,95 +219,89 @@ EventContext instances with the methods defined:
|
|
205
219
|
|
206
220
|
== Filters
|
207
221
|
|
208
|
-
|
222
|
+
Before filters are evaluated before each request within the context of the
|
223
|
+
request and can modify the request and response. Instance variables set in
|
224
|
+
filters are accessible by routes and templates.
|
209
225
|
|
210
226
|
before do
|
211
|
-
|
227
|
+
@note = 'Hi!'
|
228
|
+
request.path_info = '/foo/bar/baz'
|
212
229
|
end
|
213
230
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
throw :halt
|
219
|
-
|
220
|
-
Set the body to the result of a helper method
|
221
|
-
|
222
|
-
throw :halt, :helper_method
|
223
|
-
|
224
|
-
Set the body to the result of a helper method after sending it parameters from
|
225
|
-
the local scope
|
226
|
-
|
227
|
-
throw :halt, [:helper_method, foo, bar]
|
231
|
+
get '/foo/*' do
|
232
|
+
@note #=> 'Hi!'
|
233
|
+
params[:splat] #=> 'bar/baz'
|
234
|
+
end
|
228
235
|
|
229
|
-
|
236
|
+
== Halting
|
230
237
|
|
231
|
-
|
238
|
+
To immediately stop a request during a before filter or route use:
|
232
239
|
|
233
|
-
|
240
|
+
halt
|
234
241
|
|
235
|
-
|
242
|
+
You can also specify a body when halting ...
|
236
243
|
|
237
|
-
|
244
|
+
halt 'this will be the body'
|
238
245
|
|
239
|
-
|
246
|
+
Set the status and body ...
|
240
247
|
|
241
|
-
|
242
|
-
result
|
248
|
+
halt 401, 'go away!'
|
243
249
|
|
244
|
-
|
250
|
+
== Passing
|
245
251
|
|
246
|
-
|
252
|
+
A route can punt processing to the next matching route using the <tt>pass</tt>
|
253
|
+
statement:
|
247
254
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
end
|
255
|
+
get '/guess/:who' do
|
256
|
+
pass unless params[:who] == 'Frank'
|
257
|
+
"You got me!"
|
252
258
|
end
|
253
259
|
|
254
|
-
get '/' do
|
255
|
-
|
260
|
+
get '/guess/*' do
|
261
|
+
"You missed!"
|
256
262
|
end
|
257
263
|
|
258
|
-
|
259
|
-
|
264
|
+
The route block is immediately exited and control continues with the next
|
265
|
+
matching route. If no matching route is found, a 404 is returned.
|
260
266
|
|
261
267
|
== Configuration and Reloading
|
262
268
|
|
263
269
|
Sinatra supports multiple environments and reloading. Reloading happens
|
264
|
-
before
|
265
|
-
your configurations
|
266
|
-
etc.) to protect them from reloading or to
|
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.
|
267
274
|
|
268
|
-
|
275
|
+
Run once, at startup, in any environment:
|
269
276
|
|
270
277
|
configure do
|
271
278
|
...
|
272
279
|
end
|
273
280
|
|
274
|
-
|
281
|
+
Run only when the environment (RACK_ENV environment variable) is set to
|
282
|
+
<tt>:production</tt>.
|
275
283
|
|
276
284
|
configure :production do
|
277
285
|
...
|
278
286
|
end
|
279
287
|
|
280
|
-
|
288
|
+
Run when the environment (RACK_ENV environment variable) is set to
|
289
|
+
either <tt>:production</tt> or <tt>:test</tt>.
|
281
290
|
|
282
291
|
configure :production, :test do
|
283
292
|
...
|
284
293
|
end
|
285
294
|
|
286
|
-
This is also really nifty for error handling.
|
287
|
-
|
288
295
|
== Error handling
|
289
296
|
|
290
|
-
Error handlers run
|
291
|
-
means you get all the goodies it has to offer
|
292
|
-
etc.
|
297
|
+
Error handlers run within the same context as routes and before filters, which
|
298
|
+
means you get all the goodies it has to offer, like <tt>haml</tt>, <tt>erb</tt>,
|
299
|
+
<tt>halt</tt>, etc.
|
293
300
|
|
294
301
|
=== Not Found
|
295
302
|
|
296
|
-
When Sinatra::NotFound is raised, the
|
303
|
+
When a <tt>Sinatra::NotFound</tt> exception is raised, or the response's status
|
304
|
+
code is 404, the <tt>not_found</tt> handler is invoked:
|
297
305
|
|
298
306
|
not_found do
|
299
307
|
'This is nowhere to be found'
|
@@ -301,14 +309,12 @@ When Sinatra::NotFound is raised, the not_found handler is invoked:
|
|
301
309
|
|
302
310
|
=== Error
|
303
311
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
The exception can be obtained from the 'sinatra.error' variable in
|
308
|
-
request.env.
|
312
|
+
The +error+ handler is invoked any time an exception is raised from a route
|
313
|
+
block or before filter. The exception object can be obtained from the
|
314
|
+
'sinatra.error' Rack variable:
|
309
315
|
|
310
316
|
error do
|
311
|
-
'Sorry there was a nasty error - ' +
|
317
|
+
'Sorry there was a nasty error - ' + env['sinatra.error'].name
|
312
318
|
end
|
313
319
|
|
314
320
|
Custom errors:
|
@@ -328,12 +334,12 @@ You get this:
|
|
328
334
|
So what happened was... something bad
|
329
335
|
|
330
336
|
Sinatra installs special not_found and error handlers when running under
|
331
|
-
the development.
|
337
|
+
the development environment.
|
332
338
|
|
333
339
|
== Mime types
|
334
340
|
|
335
|
-
When using send_file or static files you may have mime types Sinatra
|
336
|
-
understand.
|
341
|
+
When using <tt>send_file</tt> or static files you may have mime types Sinatra
|
342
|
+
doesn't understand. Use +mime+ to register them by file extension:
|
337
343
|
|
338
344
|
mime :foo, 'text/foo'
|
339
345
|
|
@@ -374,59 +380,61 @@ typically don't have to +use+ them explicitly.
|
|
374
380
|
|
375
381
|
== Testing
|
376
382
|
|
377
|
-
|
383
|
+
The Sinatra::Test module includes a variety of helper methods for testing
|
384
|
+
your Sinatra app. Sinatra includes support for Test::Unit, test-spec, RSpec,
|
385
|
+
and Bacon through separate source files.
|
386
|
+
|
387
|
+
=== Test::Unit
|
378
388
|
|
379
|
-
require 'rubygems'
|
380
389
|
require 'sinatra'
|
381
390
|
require 'sinatra/test/unit'
|
382
391
|
require 'my_sinatra_app'
|
383
392
|
|
384
393
|
class MyAppTest < Test::Unit::TestCase
|
385
|
-
|
386
394
|
def test_my_default
|
387
|
-
|
395
|
+
get '/'
|
388
396
|
assert_equal 'My Default Page!', @response.body
|
389
397
|
end
|
390
398
|
|
391
399
|
def test_with_agent
|
392
|
-
|
400
|
+
get '/', :agent => 'Songbird'
|
393
401
|
assert_equal 'You're in Songbird!', @response.body
|
394
402
|
end
|
395
403
|
|
396
404
|
...
|
397
|
-
|
398
405
|
end
|
399
406
|
|
400
|
-
=== Test
|
407
|
+
=== Test::Spec
|
408
|
+
|
409
|
+
Install the test-spec gem and require <tt>'sinatra/test/spec'</tt> before
|
410
|
+
your app:
|
401
411
|
|
402
|
-
require 'rubygems'
|
403
412
|
require 'sinatra'
|
404
413
|
require 'sinatra/test/spec'
|
405
414
|
require 'my_sinatra_app'
|
406
415
|
|
407
416
|
describe 'My app' do
|
408
|
-
|
409
417
|
it "should show a default page" do
|
410
|
-
|
418
|
+
get '/'
|
411
419
|
should.be.ok
|
412
420
|
body.should.equal 'My Default Page!'
|
413
421
|
end
|
414
422
|
|
415
423
|
...
|
416
|
-
|
417
424
|
end
|
418
425
|
|
419
426
|
=== RSpec
|
420
427
|
|
421
|
-
|
422
|
-
|
428
|
+
Install the rspec gem and require <tt>'sinatra/test/rspec'</tt> before
|
429
|
+
your app:
|
430
|
+
|
423
431
|
require 'sinatra'
|
424
432
|
require 'sinatra/test/rspec'
|
425
433
|
require 'my_sinatra_app'
|
426
434
|
|
427
435
|
describe 'My app' do
|
428
436
|
it 'should show a default page' do
|
429
|
-
|
437
|
+
get '/'
|
430
438
|
@response.should be_ok
|
431
439
|
@response.body.should == 'My Default Page!'
|
432
440
|
end
|
@@ -435,8 +443,22 @@ typically don't have to +use+ them explicitly.
|
|
435
443
|
|
436
444
|
end
|
437
445
|
|
438
|
-
|
439
|
-
|
446
|
+
=== Bacon
|
447
|
+
|
448
|
+
require 'sinatra'
|
449
|
+
require 'sinatra/test/bacon'
|
450
|
+
require 'my_sinatra_app'
|
451
|
+
|
452
|
+
describe 'My app' do
|
453
|
+
it 'should be ok' do
|
454
|
+
get '/'
|
455
|
+
should.be.ok
|
456
|
+
body.should == 'Im OK'
|
457
|
+
end
|
458
|
+
end
|
459
|
+
|
460
|
+
See Sinatra::Test for more information on +get+, +post+, +put+, and
|
461
|
+
friends.
|
440
462
|
|
441
463
|
== Command line
|
442
464
|
|
@@ -481,7 +503,7 @@ screencasts about Git, which you can find here: http://www.gitcasts.com/
|
|
481
503
|
|
482
504
|
at the top of your sinatra_app.rb file:
|
483
505
|
|
484
|
-
|
506
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
|
485
507
|
require 'sinatra'
|
486
508
|
|
487
509
|
get '/about' do
|