rtomayko-sinatra 0.3.3 → 0.8.9
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 +40 -0
- data/CHANGES +167 -0
- data/README.rdoc +104 -98
- data/Rakefile +27 -9
- data/{test → compat}/app_test.rb +11 -10
- data/{test → compat}/application_test.rb +10 -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 +12 -2
- data/compat/filter_test.rb +30 -0
- data/compat/haml_test.rb +233 -0
- data/compat/helper.rb +21 -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/rspec.rb +1 -9
- data/lib/sinatra/test/spec.rb +1 -9
- data/lib/sinatra/test/unit.rb +3 -5
- data/lib/sinatra/test.rb +112 -0
- data/lib/sinatra.rb +3 -1486
- data/sinatra.gemspec +66 -36
- data/test/base_test.rb +72 -0
- data/test/builder_test.rb +56 -89
- data/test/data/reload_app_file.rb +3 -0
- data/test/erb_test.rb +44 -125
- data/test/filter_test.rb +29 -20
- data/test/haml_test.rb +57 -218
- data/test/helpers_test.rb +368 -0
- data/test/mapped_error_test.rb +141 -49
- data/test/middleware_test.rb +63 -0
- data/test/options_test.rb +103 -0
- data/test/reload_test.rb +65 -0
- data/test/request_test.rb +11 -0
- data/test/result_test.rb +92 -0
- data/test/routing_test.rb +338 -0
- data/test/sass_test.rb +33 -50
- data/test/sinatra_test.rb +15 -0
- data/test/static_test.rb +60 -0
- data/test/templates_test.rb +92 -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 -48
- data/ChangeLog +0 -96
- data/lib/sinatra/test/methods.rb +0 -76
- data/test/event_context_test.rb +0 -15
- data/test/helper.rb +0 -9
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,167 @@
|
|
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
|
+
* Deprecated "set_option" and "set_options"; use "set" instead.
|
45
|
+
|
46
|
+
* Deprecated the "env" option ("options.env"); use "environment" instead.
|
47
|
+
|
48
|
+
* Deprecated the request level "stop" method; use "halt" instead.
|
49
|
+
|
50
|
+
* Deprecated the request level "entity_tag" method; use "etag" instead.
|
51
|
+
Both "entity_tag" and "etag" were previously supported.
|
52
|
+
|
53
|
+
* Deprecated the request level "headers" method (HTTP response headers);
|
54
|
+
use "response['Header-Name']" instead.
|
55
|
+
|
56
|
+
* Deprecated "Sinatra.application"; use "Sinatra::Application" instead.
|
57
|
+
|
58
|
+
* Deprecated setting Sinatra.application = nil to reset an application.
|
59
|
+
This should no longer be necessary.
|
60
|
+
|
61
|
+
* Deprecated "Sinatra.default_options"; use
|
62
|
+
"Sinatra::Default.set(key, value)" instead.
|
63
|
+
|
64
|
+
* Deprecated the "ServerError" exception. All Exceptions are now
|
65
|
+
treated as internal server errors and result in a 500 response
|
66
|
+
status.
|
67
|
+
|
68
|
+
* Removed Event and EventContext classes. Applications are defined in a
|
69
|
+
subclass of Sinatra::Base; each request is processed within an
|
70
|
+
instance.
|
71
|
+
|
72
|
+
= 0.3.3 / 2009-01-06
|
73
|
+
|
74
|
+
* Pin to Rack 0.4.0 (this is the last release on Rack 0.4)
|
75
|
+
|
76
|
+
* Log unhandled exception backtraces to rack.errors.
|
77
|
+
|
78
|
+
* Use RACK_ENV environment variable to establish Sinatra
|
79
|
+
environment when given. Thin sets this when started with
|
80
|
+
the -e argument.
|
81
|
+
|
82
|
+
* BUG: raising Sinatra::NotFound resulted in a 500 response
|
83
|
+
code instead of 404.
|
84
|
+
|
85
|
+
* BUG: use_in_file_templates! fails with CR/LF (#45)
|
86
|
+
|
87
|
+
* BUG: Sinatra detects the app file and root path when run under
|
88
|
+
thin/passenger.
|
89
|
+
|
90
|
+
= 0.3.2
|
91
|
+
|
92
|
+
* BUG: Static and send_file read entire file into String before
|
93
|
+
sending. Updated to stream with 8K chunks instead.
|
94
|
+
|
95
|
+
* Rake tasks and assets for building basic documentation website.
|
96
|
+
See http://sinatra.rubyforge.org
|
97
|
+
|
98
|
+
* Various minor doc fixes.
|
99
|
+
|
100
|
+
= 0.3.1
|
101
|
+
|
102
|
+
* Unbreak optional path parameters [jeremyevans]
|
103
|
+
|
104
|
+
= 0.3.0
|
105
|
+
|
106
|
+
* Add sinatra.gemspec w/ support for github gem builds. Forks can now
|
107
|
+
enable the build gem option in github to get free username-sinatra.gem
|
108
|
+
builds: gem install username-sinatra.gem --source=http://gems.github.com/
|
109
|
+
|
110
|
+
* Require rack-0.4 gem; removes frozen rack dir.
|
111
|
+
|
112
|
+
* Basic RSpec support; require 'sinatra/test/rspec' instead of
|
113
|
+
'sinatra/test/spec' to use. [avdi]
|
114
|
+
|
115
|
+
* before filters can modify request environment vars used for
|
116
|
+
routing (e.g., PATH_INFO, REQUEST_METHOD, etc.) for URL rewriting
|
117
|
+
type functionality.
|
118
|
+
|
119
|
+
* In-file templates now uses @@ instead of ## as template separator.
|
120
|
+
|
121
|
+
* Top-level environment test predicates: development?, test?, production?
|
122
|
+
|
123
|
+
* Top-level "set", "enable", and "disable" methods for tweaking
|
124
|
+
app options. [rtomayko]
|
125
|
+
|
126
|
+
* Top-level "use" method for building Rack middleware pipelines
|
127
|
+
leading to app. See README for usage. [rtomayko]
|
128
|
+
|
129
|
+
* New "reload" option - set false to disable reloading in development.
|
130
|
+
|
131
|
+
* New "host" option - host/ip to bind to [cschneid]
|
132
|
+
|
133
|
+
* New "app_file" option - override the file to reload in development
|
134
|
+
mode [cschneid]
|
135
|
+
|
136
|
+
* Development error/not_found page cleanup [sr, adamwiggins]
|
137
|
+
|
138
|
+
* Remove a bunch of core extensions (String#to_param, String#from_param,
|
139
|
+
Hash#from_params, Hash#to_params, Hash#symbolize_keys, Hash#pass)
|
140
|
+
|
141
|
+
* Various grammar and formatting fixes to README; additions on
|
142
|
+
community and contributing [cypher]
|
143
|
+
|
144
|
+
* Build RDoc using Hanna template: http://sinatrarb.rubyforge.org/api
|
145
|
+
|
146
|
+
* Specs, documentation and fixes for splat'n routes [vic]
|
147
|
+
|
148
|
+
* Fix whitespace errors across all source files. [rtomayko]
|
149
|
+
|
150
|
+
* Fix streaming issues with Mongrel (body not closed). [bmizerany]
|
151
|
+
|
152
|
+
* Fix various issues with environment not being set properly (configure
|
153
|
+
blocks not running, error pages not registering, etc.) [cypher]
|
154
|
+
|
155
|
+
* Fix to allow locals to be passed to ERB templates [cschneid]
|
156
|
+
|
157
|
+
* Fix locking issues causing random errors during reload in development.
|
158
|
+
|
159
|
+
* Fix for escaped paths not resolving static files [Matthew Walker]
|
160
|
+
|
161
|
+
= 0.2.1
|
162
|
+
|
163
|
+
* File upload fix and minor tweaks.
|
164
|
+
|
165
|
+
= 0.2.0
|
166
|
+
|
167
|
+
* 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:
|
115
117
|
|
116
|
-
|
118
|
+
get '/' do
|
119
|
+
content_type 'application/xml', :charset => 'utf-8'
|
120
|
+
builder :index
|
121
|
+
end
|
117
122
|
|
118
|
-
|
123
|
+
Renders <tt>./views/index.builder</tt>.
|
124
|
+
|
125
|
+
=== Sass Templates
|
126
|
+
|
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,9 +143,8 @@ 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])
|
@@ -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
|
|
@@ -481,7 +487,7 @@ screencasts about Git, which you can find here: http://www.gitcasts.com/
|
|
481
487
|
|
482
488
|
at the top of your sinatra_app.rb file:
|
483
489
|
|
484
|
-
|
490
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
|
485
491
|
require 'sinatra'
|
486
492
|
|
487
493
|
get '/about' do
|
data/Rakefile
CHANGED
@@ -8,13 +8,19 @@ task :default => :test
|
|
8
8
|
|
9
9
|
desc 'Run specs with story style output'
|
10
10
|
task :spec do
|
11
|
-
|
11
|
+
pattern = ENV['TEST'] || '.*'
|
12
|
+
sh "specrb --testcase '#{pattern}' --specdox -Ilib:test test/*_test.rb"
|
12
13
|
end
|
13
14
|
|
14
15
|
desc 'Run specs with unit test style output'
|
15
|
-
task :test
|
16
|
-
|
17
|
-
|
16
|
+
task :test do |t|
|
17
|
+
sh "specrb -Ilib:test test/*_test.rb"
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'Run compatibility specs'
|
21
|
+
task :compat do |t|
|
22
|
+
pattern = ENV['TEST'] || '.*'
|
23
|
+
sh "specrb --testcase '#{pattern}' -Ilib:test compat/*_test.rb"
|
18
24
|
end
|
19
25
|
|
20
26
|
# PACKAGING ============================================================
|
@@ -141,11 +147,23 @@ task 'doc:book' => ['book/output/sinatra-book.html']
|
|
141
147
|
|
142
148
|
# Gemspec Helpers ====================================================
|
143
149
|
|
144
|
-
|
150
|
+
def source_version
|
151
|
+
line = File.read('lib/sinatra/base.rb')[/^\s*VERSION = .*/]
|
152
|
+
line.match(/.*VERSION = '(.*)'/)[1]
|
153
|
+
end
|
154
|
+
|
155
|
+
project_files =
|
156
|
+
FileList[
|
157
|
+
'{lib,test,compat,images}/**',
|
158
|
+
'Rakefile', 'CHANGES', 'README.rdoc'
|
159
|
+
]
|
160
|
+
file 'sinatra.gemspec' => project_files do |f|
|
145
161
|
# read spec file and split out manifest section
|
146
162
|
spec = File.read(f.name)
|
147
|
-
|
148
|
-
|
163
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
164
|
+
# replace version and date
|
165
|
+
head.sub!(/\.version = '.*'/, ".version = '#{source_version}'")
|
166
|
+
head.sub!(/\.date = '.*'/, ".date = '#{Date.today.to_s}'")
|
149
167
|
# determine file list from git ls-files
|
150
168
|
files = `git ls-files`.
|
151
169
|
split("\n").
|
@@ -155,8 +173,8 @@ file 'sinatra.gemspec' => FileList['{lib,test,images}/**','Rakefile'] do |f|
|
|
155
173
|
map{ |file| " #{file}" }.
|
156
174
|
join("\n")
|
157
175
|
# piece file back together and write...
|
158
|
-
|
159
|
-
spec =
|
176
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
177
|
+
spec = [head,manifest,tail].join(" # = MANIFEST =\n")
|
160
178
|
File.open(f.name, 'w') { |io| io.write(spec) }
|
161
179
|
puts "updated #{f.name}"
|
162
180
|
end
|