rtomayko-sinatra 0.9.0 → 0.9.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +40 -0
- data/CHANGES +189 -0
- data/README.rdoc +142 -95
- data/Rakefile +14 -49
- data/compat/events_test.rb +10 -7
- data/compat/helper.rb +13 -1
- data/lib/sinatra.rb +5 -0
- data/lib/sinatra/base.rb +138 -47
- data/lib/sinatra/compat.rb +142 -44
- data/lib/sinatra/test.rb +92 -91
- data/lib/sinatra/test/bacon.rb +17 -0
- data/lib/sinatra/test/rspec.rb +9 -0
- data/lib/sinatra/test/spec.rb +7 -0
- data/lib/sinatra/test/unit.rb +2 -2
- data/sinatra.gemspec +8 -5
- data/test/base_test.rb +33 -14
- data/test/builder_test.rb +12 -16
- data/test/erb_test.rb +11 -16
- data/test/filter_test.rb +48 -12
- data/test/haml_test.rb +14 -18
- data/test/helper.rb +25 -0
- data/test/helpers_test.rb +55 -62
- data/test/mapped_error_test.rb +43 -24
- data/test/middleware_test.rb +8 -13
- data/test/options_test.rb +29 -35
- data/test/reload_test.rb +16 -20
- data/test/request_test.rb +12 -5
- data/test/result_test.rb +16 -20
- data/test/routing_test.rb +150 -64
- data/test/sass_test.rb +8 -12
- data/test/sinatra_test.rb +2 -4
- data/test/static_test.rb +16 -19
- data/test/templates_test.rb +23 -19
- metadata +8 -6
- data/ChangeLog +0 -96
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,189 @@
|
|
1
|
+
= 0.9.0 / 2009-01-18
|
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
|
+
* Parameters with subscripts are now parsed into a nested/recursive
|
12
|
+
Hash structure. e.g., "post[title]=Hello&post[body]=World" yields
|
13
|
+
params: {'post' => {'title' => 'Hello', 'body' => 'World'}}.
|
14
|
+
|
15
|
+
* Regular expressions may now be used in route pattens; captures are
|
16
|
+
available at "params[:captures]".
|
17
|
+
|
18
|
+
* New ":provides" route condition takes an array of mime types and
|
19
|
+
matches only when an Accept request header is present with a
|
20
|
+
corresponding type. [cypher]
|
21
|
+
|
22
|
+
* New request-level "pass" method; immediately exits the current block
|
23
|
+
and passes control to the next matching route.
|
24
|
+
|
25
|
+
* The request-level "body" method now takes a block; evaluation is
|
26
|
+
deferred until an attempt is made to read the body. The block must
|
27
|
+
return a String or Array.
|
28
|
+
|
29
|
+
* New "route conditions" system for attaching rules for when a route
|
30
|
+
matches. The :agent and :host route options now use this system.
|
31
|
+
|
32
|
+
* New "dump_errors" option controls whether the backtrace is dumped to
|
33
|
+
rack.errors when an exception is raised from a route. The option is
|
34
|
+
enabled by default for top-level apps.
|
35
|
+
|
36
|
+
* Better default "app_file", "root", "public", and "views" location
|
37
|
+
detection; changes to "root" and "app_file" automatically cascade to
|
38
|
+
other options that depend on them.
|
39
|
+
|
40
|
+
* Error mappings are now split into two distinct layers: exception
|
41
|
+
mappings and custom error pages. Exception mappings are registered
|
42
|
+
with "error(Exception)" and are run only when the app raises an
|
43
|
+
exception. Custom error pages are registered with "error(status_code)",
|
44
|
+
where "status_code" is an integer, and are run any time the response
|
45
|
+
has the status code specified. It's also possible to register an error
|
46
|
+
page for a range of status codes: "error(500..599)".
|
47
|
+
|
48
|
+
* In-file templates are now automatically imported from the file that
|
49
|
+
requires 'sinatra'. The use_in_file_templates! method is still available
|
50
|
+
for loading templates from other files.
|
51
|
+
|
52
|
+
* Sinatra's testing support is no longer dependent on Test::Unit. Requiring
|
53
|
+
'sinatra/test' adds the Sinatra::Test module and Sinatra::TestHarness
|
54
|
+
class, which can be used with any test framework. The 'sinatra/test/unit',
|
55
|
+
'sinatra/test/spec', 'sinatra/test/rspec', or 'sinatra/test/bacon' files
|
56
|
+
can be required to setup a framework-specific testing environment. See the
|
57
|
+
README for more information.
|
58
|
+
|
59
|
+
* Added support for Bacon (test framework). The 'sinatra/test/bacon' file
|
60
|
+
can be required to setup Sinatra test helpers on Bacon::Context.
|
61
|
+
|
62
|
+
* Deprecated "set_option" and "set_options"; use "set" instead.
|
63
|
+
|
64
|
+
* Deprecated the "env" option ("options.env"); use "environment" instead.
|
65
|
+
|
66
|
+
* Deprecated the request level "stop" method; use "halt" instead.
|
67
|
+
|
68
|
+
* Deprecated the request level "entity_tag" method; use "etag" instead.
|
69
|
+
Both "entity_tag" and "etag" were previously supported.
|
70
|
+
|
71
|
+
* Deprecated the request level "headers" method (HTTP response headers);
|
72
|
+
use "response['Header-Name']" instead.
|
73
|
+
|
74
|
+
* Deprecated "Sinatra.application"; use "Sinatra::Application" instead.
|
75
|
+
|
76
|
+
* Deprecated setting Sinatra.application = nil to reset an application.
|
77
|
+
This should no longer be necessary.
|
78
|
+
|
79
|
+
* Deprecated "Sinatra.default_options"; use
|
80
|
+
"Sinatra::Default.set(key, value)" instead.
|
81
|
+
|
82
|
+
* Deprecated the "ServerError" exception. All Exceptions are now
|
83
|
+
treated as internal server errors and result in a 500 response
|
84
|
+
status.
|
85
|
+
|
86
|
+
* Deprecated the "get_it", "post_it", "put_it", "delete_it", and "head_it"
|
87
|
+
test helper methods. Use "get", "post", "put", "delete", and "head",
|
88
|
+
respectively, instead.
|
89
|
+
|
90
|
+
* Removed Event and EventContext classes. Applications are defined in a
|
91
|
+
subclass of Sinatra::Base; each request is processed within an
|
92
|
+
instance.
|
93
|
+
|
94
|
+
= 0.3.3 / 2009-01-06
|
95
|
+
|
96
|
+
* Pin to Rack 0.4.0 (this is the last release on Rack 0.4)
|
97
|
+
|
98
|
+
* Log unhandled exception backtraces to rack.errors.
|
99
|
+
|
100
|
+
* Use RACK_ENV environment variable to establish Sinatra
|
101
|
+
environment when given. Thin sets this when started with
|
102
|
+
the -e argument.
|
103
|
+
|
104
|
+
* BUG: raising Sinatra::NotFound resulted in a 500 response
|
105
|
+
code instead of 404.
|
106
|
+
|
107
|
+
* BUG: use_in_file_templates! fails with CR/LF (#45)
|
108
|
+
|
109
|
+
* BUG: Sinatra detects the app file and root path when run under
|
110
|
+
thin/passenger.
|
111
|
+
|
112
|
+
= 0.3.2
|
113
|
+
|
114
|
+
* BUG: Static and send_file read entire file into String before
|
115
|
+
sending. Updated to stream with 8K chunks instead.
|
116
|
+
|
117
|
+
* Rake tasks and assets for building basic documentation website.
|
118
|
+
See http://sinatra.rubyforge.org
|
119
|
+
|
120
|
+
* Various minor doc fixes.
|
121
|
+
|
122
|
+
= 0.3.1
|
123
|
+
|
124
|
+
* Unbreak optional path parameters [jeremyevans]
|
125
|
+
|
126
|
+
= 0.3.0
|
127
|
+
|
128
|
+
* Add sinatra.gemspec w/ support for github gem builds. Forks can now
|
129
|
+
enable the build gem option in github to get free username-sinatra.gem
|
130
|
+
builds: gem install username-sinatra.gem --source=http://gems.github.com/
|
131
|
+
|
132
|
+
* Require rack-0.4 gem; removes frozen rack dir.
|
133
|
+
|
134
|
+
* Basic RSpec support; require 'sinatra/test/rspec' instead of
|
135
|
+
'sinatra/test/spec' to use. [avdi]
|
136
|
+
|
137
|
+
* before filters can modify request environment vars used for
|
138
|
+
routing (e.g., PATH_INFO, REQUEST_METHOD, etc.) for URL rewriting
|
139
|
+
type functionality.
|
140
|
+
|
141
|
+
* In-file templates now uses @@ instead of ## as template separator.
|
142
|
+
|
143
|
+
* Top-level environment test predicates: development?, test?, production?
|
144
|
+
|
145
|
+
* Top-level "set", "enable", and "disable" methods for tweaking
|
146
|
+
app options. [rtomayko]
|
147
|
+
|
148
|
+
* Top-level "use" method for building Rack middleware pipelines
|
149
|
+
leading to app. See README for usage. [rtomayko]
|
150
|
+
|
151
|
+
* New "reload" option - set false to disable reloading in development.
|
152
|
+
|
153
|
+
* New "host" option - host/ip to bind to [cschneid]
|
154
|
+
|
155
|
+
* New "app_file" option - override the file to reload in development
|
156
|
+
mode [cschneid]
|
157
|
+
|
158
|
+
* Development error/not_found page cleanup [sr, adamwiggins]
|
159
|
+
|
160
|
+
* Remove a bunch of core extensions (String#to_param, String#from_param,
|
161
|
+
Hash#from_params, Hash#to_params, Hash#symbolize_keys, Hash#pass)
|
162
|
+
|
163
|
+
* Various grammar and formatting fixes to README; additions on
|
164
|
+
community and contributing [cypher]
|
165
|
+
|
166
|
+
* Build RDoc using Hanna template: http://sinatrarb.rubyforge.org/api
|
167
|
+
|
168
|
+
* Specs, documentation and fixes for splat'n routes [vic]
|
169
|
+
|
170
|
+
* Fix whitespace errors across all source files. [rtomayko]
|
171
|
+
|
172
|
+
* Fix streaming issues with Mongrel (body not closed). [bmizerany]
|
173
|
+
|
174
|
+
* Fix various issues with environment not being set properly (configure
|
175
|
+
blocks not running, error pages not registering, etc.) [cypher]
|
176
|
+
|
177
|
+
* Fix to allow locals to be passed to ERB templates [cschneid]
|
178
|
+
|
179
|
+
* Fix locking issues causing random errors during reload in development.
|
180
|
+
|
181
|
+
* Fix for escaped paths not resolving static files [Matthew Walker]
|
182
|
+
|
183
|
+
= 0.2.1
|
184
|
+
|
185
|
+
* File upload fix and minor tweaks.
|
186
|
+
|
187
|
+
= 0.2.0
|
188
|
+
|
189
|
+
* 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,10 +30,6 @@ 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
33
|
== Routes
|
40
34
|
|
41
35
|
Routes are matched based on the order of declaration. The first route that
|
@@ -47,14 +41,17 @@ Basic routes:
|
|
47
41
|
...
|
48
42
|
end
|
49
43
|
|
50
|
-
|
44
|
+
Route patterns may include named parameters, accessible via the
|
45
|
+
<tt>params</tt> hash:
|
51
46
|
|
52
47
|
get '/:name' do
|
53
48
|
# matches "GET /foo" and "GET /bar"
|
54
49
|
# params[:name] is 'foo' or 'bar'
|
50
|
+
"Hello #{params[:name]}!"
|
55
51
|
end
|
56
52
|
|
57
|
-
|
53
|
+
Route patterns may also include splat (or wildcard) parameters, accessible
|
54
|
+
via the <tt>params[:splat]</tt> array.
|
58
55
|
|
59
56
|
get '/say/*/to/*' do
|
60
57
|
# matches /say/hello/to/world
|
@@ -66,7 +63,13 @@ Splat parameters:
|
|
66
63
|
params[:splat] # => ["path/to/file", "xml"]
|
67
64
|
end
|
68
65
|
|
69
|
-
|
66
|
+
Route matching with Regular Expressions:
|
67
|
+
|
68
|
+
get %r{/hello/([\w]+)} do
|
69
|
+
"Hello, #{params[:captures].first}!"
|
70
|
+
end
|
71
|
+
|
72
|
+
Routes may include a variety of matching conditions, such as the user agent:
|
70
73
|
|
71
74
|
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
|
72
75
|
"You're using Songbird version #{params[:agent][0]}"
|
@@ -78,33 +81,29 @@ User agent matching:
|
|
78
81
|
|
79
82
|
== Static Files
|
80
83
|
|
81
|
-
|
82
|
-
|
83
|
-
root
|
84
|
-
\ public
|
85
|
-
|
86
|
-
To use a different static directory:
|
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:
|
87
86
|
|
88
87
|
set :public, File.dirname(__FILE__) + '/static'
|
89
88
|
|
90
|
-
== Views
|
91
|
-
|
92
|
-
Views are searched for in a ./views directory in the same location as
|
93
|
-
your main application.
|
89
|
+
== Views / Templates
|
94
90
|
|
95
|
-
|
91
|
+
Templates are assumed to be located directly under a <tt>./views</tt>
|
92
|
+
directory. To use a different views directory:
|
96
93
|
|
97
94
|
set :views, File.dirname(__FILE__) + '/templates'
|
98
95
|
|
99
96
|
=== Haml Templates
|
100
97
|
|
98
|
+
The haml gem/library is required to render HAML templates:
|
99
|
+
|
101
100
|
get '/' do
|
102
101
|
haml :index
|
103
102
|
end
|
104
103
|
|
105
104
|
Renders <tt>./views/index.haml</tt>.
|
106
105
|
|
107
|
-
=== Erb
|
106
|
+
=== Erb Templates
|
108
107
|
|
109
108
|
get '/' do
|
110
109
|
erb :index
|
@@ -112,11 +111,20 @@ Renders <tt>./views/index.haml</tt>.
|
|
112
111
|
|
113
112
|
Renders <tt>./views/index.erb</tt>
|
114
113
|
|
115
|
-
=== Builder
|
114
|
+
=== Builder Templates
|
116
115
|
|
117
|
-
|
116
|
+
The builder gem/library is required to render builder templates:
|
118
117
|
|
119
|
-
|
118
|
+
get '/' do
|
119
|
+
content_type 'application/xml', :charset => 'utf-8'
|
120
|
+
builder :index
|
121
|
+
end
|
122
|
+
|
123
|
+
Renders <tt>./views/index.builder</tt>.
|
124
|
+
|
125
|
+
=== Sass Templates
|
126
|
+
|
127
|
+
The sass gem/library is required to render Sass templates:
|
120
128
|
|
121
129
|
get '/stylesheet.css' do
|
122
130
|
content_type 'text/css', :charset => 'utf-8'
|
@@ -135,9 +143,8 @@ Renders the inlined template string.
|
|
135
143
|
|
136
144
|
=== Accessing Variables
|
137
145
|
|
138
|
-
Templates are evaluated within the
|
139
|
-
|
140
|
-
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:
|
141
148
|
|
142
149
|
get '/:id' do
|
143
150
|
@foo = Foo.find(params[:id])
|
@@ -158,27 +165,32 @@ other templates.
|
|
158
165
|
|
159
166
|
Templates may be defined at the end of the source file:
|
160
167
|
|
168
|
+
require 'rubygems'
|
169
|
+
require 'sinatra'
|
170
|
+
|
161
171
|
get '/' do
|
162
172
|
haml :index
|
163
173
|
end
|
164
174
|
|
165
|
-
use_in_file_templates!
|
166
|
-
|
167
175
|
__END__
|
168
176
|
|
169
177
|
@@ layout
|
170
|
-
|
171
|
-
|
172
|
-
X
|
178
|
+
%html
|
179
|
+
= yield
|
173
180
|
|
174
181
|
@@ index
|
175
182
|
%div.title Hello world!!!!!
|
176
183
|
|
184
|
+
NOTE: Sinatra will automaticly load any in-file-templates in the
|
185
|
+
source file that first required sinatra. If you have in-file-templates
|
186
|
+
in another source file you will need to explicitly call
|
187
|
+
+use_in_file_templates! on main in that file.
|
188
|
+
|
177
189
|
It's also possible to define named templates using the top-level template
|
178
190
|
method:
|
179
191
|
|
180
192
|
template :layout do
|
181
|
-
"
|
193
|
+
"%html\n =yield\n"
|
182
194
|
end
|
183
195
|
|
184
196
|
template :index do
|
@@ -189,9 +201,17 @@ method:
|
|
189
201
|
haml :index
|
190
202
|
end
|
191
203
|
|
204
|
+
If a template named "layout" exists, it will be used each time a template
|
205
|
+
is rendered. You can disable layouts by passing <tt>:layout => false</tt>.
|
206
|
+
|
207
|
+
get '/' do
|
208
|
+
haml :index, :layout => !request.xhr?
|
209
|
+
end
|
210
|
+
|
192
211
|
== Helpers
|
193
212
|
|
194
|
-
|
213
|
+
Use the top-level <tt>helpers</tt> method to define helper methods for use in
|
214
|
+
route blocks and templates:
|
195
215
|
|
196
216
|
helpers do
|
197
217
|
def bar(name)
|
@@ -205,7 +225,9 @@ The top-level <tt>helpers</tt> method
|
|
205
225
|
|
206
226
|
== Filters
|
207
227
|
|
208
|
-
Before filters are evaluated
|
228
|
+
Before filters are evaluated before each request within the context of the
|
229
|
+
request and can modify the request and response. Instance variables set in
|
230
|
+
filters are accessible by routes and templates.
|
209
231
|
|
210
232
|
before do
|
211
233
|
@note = 'Hi!'
|
@@ -217,64 +239,75 @@ Before filters are evaluated in request context before routing is performed:
|
|
217
239
|
params[:splat] #=> 'bar/baz'
|
218
240
|
end
|
219
241
|
|
220
|
-
|
221
|
-
available to routes.
|
242
|
+
== Halting
|
222
243
|
|
223
|
-
|
244
|
+
To immediately stop a request during a before filter or route use:
|
224
245
|
|
225
|
-
|
246
|
+
halt
|
226
247
|
|
227
|
-
|
248
|
+
You can also specify a body when halting ...
|
228
249
|
|
229
|
-
|
250
|
+
halt 'this will be the body'
|
230
251
|
|
231
|
-
|
252
|
+
Set the status and body ...
|
232
253
|
|
233
|
-
|
254
|
+
halt 401, 'go away!'
|
234
255
|
|
235
|
-
|
256
|
+
== Passing
|
236
257
|
|
237
|
-
|
238
|
-
|
258
|
+
A route can punt processing to the next matching route using the <tt>pass</tt>
|
259
|
+
statement:
|
239
260
|
|
240
|
-
|
261
|
+
get '/guess/:who' do
|
262
|
+
pass unless params[:who] == 'Frank'
|
263
|
+
"You got me!"
|
264
|
+
end
|
265
|
+
|
266
|
+
get '/guess/*' do
|
267
|
+
"You missed!"
|
268
|
+
end
|
269
|
+
|
270
|
+
The route block is immediately exited and control continues with the next
|
271
|
+
matching route. If no matching route is found, a 404 is returned.
|
241
272
|
|
242
273
|
== Configuration and Reloading
|
243
274
|
|
244
275
|
Sinatra supports multiple environments and reloading. Reloading happens
|
245
|
-
before
|
246
|
-
your configurations
|
247
|
-
etc.) to protect them from reloading or to
|
276
|
+
before each request when running under the <tt>:development</tt>
|
277
|
+
environment. Wrap your configurations (e.g., database connections, constants,
|
278
|
+
etc.) in <tt>configure</tt> blocks to protect them from reloading or to
|
279
|
+
target specific environments.
|
248
280
|
|
249
|
-
|
281
|
+
Run once, at startup, in any environment:
|
250
282
|
|
251
283
|
configure do
|
252
284
|
...
|
253
285
|
end
|
254
286
|
|
255
|
-
|
287
|
+
Run only when the environment (RACK_ENV environment variable) is set to
|
288
|
+
<tt>:production</tt>.
|
256
289
|
|
257
290
|
configure :production do
|
258
291
|
...
|
259
292
|
end
|
260
293
|
|
261
|
-
|
294
|
+
Run when the environment (RACK_ENV environment variable) is set to
|
295
|
+
either <tt>:production</tt> or <tt>:test</tt>.
|
262
296
|
|
263
297
|
configure :production, :test do
|
264
298
|
...
|
265
299
|
end
|
266
300
|
|
267
|
-
This is also really nifty for error handling.
|
268
|
-
|
269
301
|
== Error handling
|
270
302
|
|
271
|
-
Error handlers run
|
272
|
-
means you get all the goodies it has to offer
|
273
|
-
etc.
|
303
|
+
Error handlers run within the same context as routes and before filters, which
|
304
|
+
means you get all the goodies it has to offer, like <tt>haml</tt>, <tt>erb</tt>,
|
305
|
+
<tt>halt</tt>, etc.
|
274
306
|
|
275
307
|
=== Not Found
|
276
308
|
|
277
|
-
When Sinatra::NotFound is raised, the
|
309
|
+
When a <tt>Sinatra::NotFound</tt> exception is raised, or the response's status
|
310
|
+
code is 404, the <tt>not_found</tt> handler is invoked:
|
278
311
|
|
279
312
|
not_found do
|
280
313
|
'This is nowhere to be found'
|
@@ -282,14 +315,12 @@ When Sinatra::NotFound is raised, the not_found handler is invoked:
|
|
282
315
|
|
283
316
|
=== Error
|
284
317
|
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
The exception can be obtained from the 'sinatra.error' variable in
|
289
|
-
request.env.
|
318
|
+
The +error+ handler is invoked any time an exception is raised from a route
|
319
|
+
block or before filter. The exception object can be obtained from the
|
320
|
+
'sinatra.error' Rack variable:
|
290
321
|
|
291
322
|
error do
|
292
|
-
'Sorry there was a nasty error - ' +
|
323
|
+
'Sorry there was a nasty error - ' + env['sinatra.error'].name
|
293
324
|
end
|
294
325
|
|
295
326
|
Custom errors:
|
@@ -309,12 +340,12 @@ You get this:
|
|
309
340
|
So what happened was... something bad
|
310
341
|
|
311
342
|
Sinatra installs special not_found and error handlers when running under
|
312
|
-
the development.
|
343
|
+
the development environment.
|
313
344
|
|
314
345
|
== Mime types
|
315
346
|
|
316
|
-
When using send_file or static files you may have mime types Sinatra
|
317
|
-
understand.
|
347
|
+
When using <tt>send_file</tt> or static files you may have mime types Sinatra
|
348
|
+
doesn't understand. Use +mime+ to register them by file extension:
|
318
349
|
|
319
350
|
mime :foo, 'text/foo'
|
320
351
|
|
@@ -355,59 +386,61 @@ typically don't have to +use+ them explicitly.
|
|
355
386
|
|
356
387
|
== Testing
|
357
388
|
|
358
|
-
|
389
|
+
The Sinatra::Test module includes a variety of helper methods for testing
|
390
|
+
your Sinatra app. Sinatra includes support for Test::Unit, test-spec, RSpec,
|
391
|
+
and Bacon through separate source files.
|
392
|
+
|
393
|
+
=== Test::Unit
|
359
394
|
|
360
|
-
require 'rubygems'
|
361
395
|
require 'sinatra'
|
362
396
|
require 'sinatra/test/unit'
|
363
397
|
require 'my_sinatra_app'
|
364
398
|
|
365
399
|
class MyAppTest < Test::Unit::TestCase
|
366
|
-
|
367
400
|
def test_my_default
|
368
|
-
|
401
|
+
get '/'
|
369
402
|
assert_equal 'My Default Page!', @response.body
|
370
403
|
end
|
371
404
|
|
372
405
|
def test_with_agent
|
373
|
-
|
406
|
+
get '/', :agent => 'Songbird'
|
374
407
|
assert_equal 'You're in Songbird!', @response.body
|
375
408
|
end
|
376
409
|
|
377
410
|
...
|
378
|
-
|
379
411
|
end
|
380
412
|
|
381
|
-
=== Test
|
413
|
+
=== Test::Spec
|
414
|
+
|
415
|
+
Install the test-spec gem and require <tt>'sinatra/test/spec'</tt> before
|
416
|
+
your app:
|
382
417
|
|
383
|
-
require 'rubygems'
|
384
418
|
require 'sinatra'
|
385
419
|
require 'sinatra/test/spec'
|
386
420
|
require 'my_sinatra_app'
|
387
421
|
|
388
422
|
describe 'My app' do
|
389
|
-
|
390
423
|
it "should show a default page" do
|
391
|
-
|
424
|
+
get '/'
|
392
425
|
should.be.ok
|
393
426
|
body.should.equal 'My Default Page!'
|
394
427
|
end
|
395
428
|
|
396
429
|
...
|
397
|
-
|
398
430
|
end
|
399
431
|
|
400
432
|
=== RSpec
|
401
433
|
|
402
|
-
|
403
|
-
|
434
|
+
Install the rspec gem and require <tt>'sinatra/test/rspec'</tt> before
|
435
|
+
your app:
|
436
|
+
|
404
437
|
require 'sinatra'
|
405
438
|
require 'sinatra/test/rspec'
|
406
439
|
require 'my_sinatra_app'
|
407
440
|
|
408
441
|
describe 'My app' do
|
409
442
|
it 'should show a default page' do
|
410
|
-
|
443
|
+
get '/'
|
411
444
|
@response.should be_ok
|
412
445
|
@response.body.should == 'My Default Page!'
|
413
446
|
end
|
@@ -416,20 +449,35 @@ typically don't have to +use+ them explicitly.
|
|
416
449
|
|
417
450
|
end
|
418
451
|
|
419
|
-
|
420
|
-
|
452
|
+
=== Bacon
|
453
|
+
|
454
|
+
require 'sinatra'
|
455
|
+
require 'sinatra/test/bacon'
|
456
|
+
require 'my_sinatra_app'
|
457
|
+
|
458
|
+
describe 'My app' do
|
459
|
+
it 'should be ok' do
|
460
|
+
get '/'
|
461
|
+
should.be.ok
|
462
|
+
body.should == 'Im OK'
|
463
|
+
end
|
464
|
+
end
|
465
|
+
|
466
|
+
See Sinatra::Test for more information on +get+, +post+, +put+, and
|
467
|
+
friends.
|
421
468
|
|
422
469
|
== Command line
|
423
470
|
|
424
471
|
Sinatra applications can be run directly:
|
425
472
|
|
426
|
-
ruby myapp.rb [-h] [-x] [-p PORT] [-
|
473
|
+
ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-s HANDLER]
|
427
474
|
|
428
475
|
Options are:
|
429
476
|
|
430
477
|
-h # help
|
431
478
|
-p # set the port (default is 4567)
|
432
479
|
-e # set the environment (default is development)
|
480
|
+
-s # specify rack server/handler (default is thin)
|
433
481
|
-x # turn on the mutex lock (default is off)
|
434
482
|
|
435
483
|
== Contributing
|
@@ -441,14 +489,14 @@ Ruby syntax hilighting. VIM and Emacs are a fine choice on any platform, but
|
|
441
489
|
feel free to use whatever you're familiar with.
|
442
490
|
|
443
491
|
Sinatra uses the Git source code management system. If you're unfamiliar with
|
444
|
-
Git, you can find more information and tutorials on http://git.or.cz
|
445
|
-
as http://git-scm.com
|
446
|
-
screencasts about Git, which you can find here: http://www.gitcasts.com
|
492
|
+
Git, you can find more information and tutorials on http://git.or.cz as well
|
493
|
+
as http://git-scm.com. Scott Chacon created a great series of introductory
|
494
|
+
screencasts about Git, which you can find here: http://www.gitcasts.com
|
447
495
|
|
448
496
|
=== First Time: Cloning The Sinatra Repo
|
449
497
|
|
450
498
|
cd where/you/keep/your/projects
|
451
|
-
git clone git://github.com/
|
499
|
+
git clone git://github.com/sinatra/sinatra.git
|
452
500
|
cd sinatra
|
453
501
|
cd path/to/your_project
|
454
502
|
ln -s ../sinatra/
|
@@ -462,7 +510,7 @@ screencasts about Git, which you can find here: http://www.gitcasts.com/
|
|
462
510
|
|
463
511
|
at the top of your sinatra_app.rb file:
|
464
512
|
|
465
|
-
|
513
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
|
466
514
|
require 'sinatra'
|
467
515
|
|
468
516
|
get '/about' do
|
@@ -472,9 +520,8 @@ at the top of your sinatra_app.rb file:
|
|
472
520
|
=== Contributing a Patch
|
473
521
|
|
474
522
|
There are several ways to do this. Probably the easiest (and preferred) way is
|
475
|
-
to fork Sinatra on GitHub (http://github.com/
|
476
|
-
changes to your Sinatra repo
|
477
|
-
GitHub) a pull request.
|
523
|
+
to fork Sinatra on GitHub (http://github.com/sinatra/sinatra), push your
|
524
|
+
changes to your Sinatra repo and file a ticket in lighthouse (sinatra.lighthouseapp.com) where we can discuss it.
|
478
525
|
|
479
526
|
You can also create a patch file and attach it to a feature request or bug fix
|
480
527
|
on the issue tracker (see below) or send it to the mailing list (see Community
|
@@ -482,7 +529,7 @@ section).
|
|
482
529
|
|
483
530
|
=== Issue Tracking and Feature Requests
|
484
531
|
|
485
|
-
http://sinatra.lighthouseapp.com
|
532
|
+
http://sinatra.lighthouseapp.com
|
486
533
|
|
487
534
|
== Community
|
488
535
|
|