sinatra 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sinatra might be problematic. Click here for more details.
- data/ChangeLog +64 -0
- data/LICENSE +1 -1
- data/README.rdoc +244 -141
- data/Rakefile +111 -0
- data/lib/sinatra.rb +942 -627
- data/{vendor/rack/lib → lib/sinatra}/rack/handler/mongrel.rb +10 -3
- data/lib/sinatra/test/methods.rb +56 -36
- data/lib/sinatra/test/rspec.rb +10 -0
- data/lib/sinatra/test/spec.rb +2 -2
- data/lib/sinatra/test/unit.rb +1 -1
- data/sinatra.gemspec +71 -43
- data/test/app_test.rb +181 -32
- data/test/application_test.rb +179 -36
- data/test/custom_error_test.rb +17 -22
- data/test/erb_test.rb +42 -22
- data/test/event_context_test.rb +3 -3
- data/test/events_test.rb +8 -11
- data/test/filter_test.rb +30 -0
- data/test/haml_test.rb +83 -31
- data/test/helper.rb +6 -3
- data/test/mapped_error_test.rb +32 -21
- data/test/pipeline_test.rb +66 -0
- data/test/sass_test.rb +7 -7
- data/test/sessions_test.rb +8 -9
- data/test/streaming_test.rb +10 -4
- data/test/sym_params_test.rb +1 -1
- data/test/template_test.rb +11 -11
- data/test/use_in_file_templates_test.rb +9 -10
- metadata +22 -101
- data/CHANGELOG +0 -3
- data/Manifest +0 -122
- data/index.html +0 -9
- data/test/diddy_test.rb +0 -41
- data/test/rest_test.rb +0 -16
- data/vendor/rack/AUTHORS +0 -7
- data/vendor/rack/COPYING +0 -18
- data/vendor/rack/KNOWN-ISSUES +0 -18
- data/vendor/rack/README +0 -242
- data/vendor/rack/Rakefile +0 -174
- data/vendor/rack/bin/rackup +0 -153
- data/vendor/rack/contrib/rack_logo.svg +0 -111
- data/vendor/rack/example/lobster.ru +0 -4
- data/vendor/rack/example/protectedlobster.rb +0 -14
- data/vendor/rack/example/protectedlobster.ru +0 -8
- data/vendor/rack/lib/rack.rb +0 -92
- data/vendor/rack/lib/rack/adapter/camping.rb +0 -22
- data/vendor/rack/lib/rack/auth/abstract/handler.rb +0 -28
- data/vendor/rack/lib/rack/auth/abstract/request.rb +0 -37
- data/vendor/rack/lib/rack/auth/basic.rb +0 -58
- data/vendor/rack/lib/rack/auth/digest/md5.rb +0 -124
- data/vendor/rack/lib/rack/auth/digest/nonce.rb +0 -51
- data/vendor/rack/lib/rack/auth/digest/params.rb +0 -55
- data/vendor/rack/lib/rack/auth/digest/request.rb +0 -40
- data/vendor/rack/lib/rack/auth/openid.rb +0 -116
- data/vendor/rack/lib/rack/builder.rb +0 -56
- data/vendor/rack/lib/rack/cascade.rb +0 -36
- data/vendor/rack/lib/rack/commonlogger.rb +0 -56
- data/vendor/rack/lib/rack/file.rb +0 -112
- data/vendor/rack/lib/rack/handler/cgi.rb +0 -57
- data/vendor/rack/lib/rack/handler/fastcgi.rb +0 -83
- data/vendor/rack/lib/rack/handler/lsws.rb +0 -52
- data/vendor/rack/lib/rack/handler/scgi.rb +0 -57
- data/vendor/rack/lib/rack/handler/webrick.rb +0 -57
- data/vendor/rack/lib/rack/lint.rb +0 -394
- data/vendor/rack/lib/rack/lobster.rb +0 -65
- data/vendor/rack/lib/rack/mock.rb +0 -160
- data/vendor/rack/lib/rack/recursive.rb +0 -57
- data/vendor/rack/lib/rack/reloader.rb +0 -64
- data/vendor/rack/lib/rack/request.rb +0 -197
- data/vendor/rack/lib/rack/response.rb +0 -166
- data/vendor/rack/lib/rack/session/abstract/id.rb +0 -126
- data/vendor/rack/lib/rack/session/cookie.rb +0 -71
- data/vendor/rack/lib/rack/session/memcache.rb +0 -83
- data/vendor/rack/lib/rack/session/pool.rb +0 -67
- data/vendor/rack/lib/rack/showexceptions.rb +0 -344
- data/vendor/rack/lib/rack/showstatus.rb +0 -103
- data/vendor/rack/lib/rack/static.rb +0 -38
- data/vendor/rack/lib/rack/urlmap.rb +0 -48
- data/vendor/rack/lib/rack/utils.rb +0 -240
- data/vendor/rack/test/cgi/lighttpd.conf +0 -20
- data/vendor/rack/test/cgi/test +0 -9
- data/vendor/rack/test/cgi/test.fcgi +0 -7
- data/vendor/rack/test/cgi/test.ru +0 -7
- data/vendor/rack/test/spec_rack_auth_basic.rb +0 -69
- data/vendor/rack/test/spec_rack_auth_digest.rb +0 -169
- data/vendor/rack/test/spec_rack_builder.rb +0 -50
- data/vendor/rack/test/spec_rack_camping.rb +0 -47
- data/vendor/rack/test/spec_rack_cascade.rb +0 -50
- data/vendor/rack/test/spec_rack_cgi.rb +0 -91
- data/vendor/rack/test/spec_rack_commonlogger.rb +0 -32
- data/vendor/rack/test/spec_rack_fastcgi.rb +0 -91
- data/vendor/rack/test/spec_rack_file.rb +0 -40
- data/vendor/rack/test/spec_rack_lint.rb +0 -317
- data/vendor/rack/test/spec_rack_lobster.rb +0 -45
- data/vendor/rack/test/spec_rack_mock.rb +0 -152
- data/vendor/rack/test/spec_rack_mongrel.rb +0 -165
- data/vendor/rack/test/spec_rack_recursive.rb +0 -77
- data/vendor/rack/test/spec_rack_request.rb +0 -384
- data/vendor/rack/test/spec_rack_response.rb +0 -167
- data/vendor/rack/test/spec_rack_session_cookie.rb +0 -49
- data/vendor/rack/test/spec_rack_session_memcache.rb +0 -100
- data/vendor/rack/test/spec_rack_session_pool.rb +0 -84
- data/vendor/rack/test/spec_rack_showexceptions.rb +0 -21
- data/vendor/rack/test/spec_rack_showstatus.rb +0 -71
- data/vendor/rack/test/spec_rack_static.rb +0 -37
- data/vendor/rack/test/spec_rack_urlmap.rb +0 -175
- data/vendor/rack/test/spec_rack_utils.rb +0 -57
- data/vendor/rack/test/spec_rack_webrick.rb +0 -106
- data/vendor/rack/test/testrequest.rb +0 -43
data/ChangeLog
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
= 0.3.0
|
2
|
+
|
3
|
+
* Add sinatra.gemspec w/ support for github gem builds. Forks can now
|
4
|
+
enable the build gem option in github to get free username-sinatra.gem
|
5
|
+
builds: gem install username-sinatra.gem --source=http://gems.github.com/
|
6
|
+
|
7
|
+
* Require rack-0.4 gem; removes frozen rack dir.
|
8
|
+
|
9
|
+
* Basic RSpec support; require 'sinatra/test/rspec' instead of
|
10
|
+
'sinatra/test/spec' to use. [avdi]
|
11
|
+
|
12
|
+
* before filters can modify request environment vars used for
|
13
|
+
routing (e.g., PATH_INFO, REQUEST_METHOD, etc.) for URL rewriting
|
14
|
+
type functionality.
|
15
|
+
|
16
|
+
* In-file templates now uses @@ instead of ## as template separator.
|
17
|
+
|
18
|
+
* Top-level environment test predicates: development?, test?, production?
|
19
|
+
|
20
|
+
* Top-level "set", "enable", and "disable" methods for tweaking
|
21
|
+
app options. [rtomayko]
|
22
|
+
|
23
|
+
* Top-level "use" method for building Rack middleware pipelines
|
24
|
+
leading to app. See README for usage. [rtomayko]
|
25
|
+
|
26
|
+
* New "reload" option - set false to disable reloading in development.
|
27
|
+
|
28
|
+
* New "host" option - host/ip to bind to [cschneid]
|
29
|
+
|
30
|
+
* New "app_file" option - override the file to reload in development
|
31
|
+
mode [cschneid]
|
32
|
+
|
33
|
+
* Development error/not_found page cleanup [sr, adamwiggins]
|
34
|
+
|
35
|
+
* Remove a bunch of core extensions (String#to_param, String#from_param,
|
36
|
+
Hash#from_params, Hash#to_params, Hash#symbolize_keys, Hash#pass)
|
37
|
+
|
38
|
+
* Various grammar and formatting fixes to README; additions on
|
39
|
+
community and contributing [cypher]
|
40
|
+
|
41
|
+
* Build RDoc using Hanna template: http://sinatrarb.rubyforge.org/api
|
42
|
+
|
43
|
+
* Specs, documentation and fixes for splat'n routes [vic]
|
44
|
+
|
45
|
+
* Fix whitespace errors across all source files. [rtomayko]
|
46
|
+
|
47
|
+
* Fix streaming issues with Mongrel (body not closed). [bmizerany]
|
48
|
+
|
49
|
+
* Fix various issues with environment not being set properly (configure
|
50
|
+
blocks not running, error pages not registering, etc.) [cypher]
|
51
|
+
|
52
|
+
* Fix to allow locals to be passed to ERB templates [cschneid]
|
53
|
+
|
54
|
+
* Fix locking issues causing random errors during reload in development.
|
55
|
+
|
56
|
+
* Fix for escaped paths not resolving static files [Matthew Walker]
|
57
|
+
|
58
|
+
= 0.2.1
|
59
|
+
|
60
|
+
* File upload fix and minor tweaks.
|
61
|
+
|
62
|
+
= 0.2.0
|
63
|
+
|
64
|
+
* Initial gem release of 0.2 coebase.
|
data/LICENSE
CHANGED
@@ -19,4 +19,4 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
19
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
20
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
21
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
CHANGED
@@ -1,68 +1,76 @@
|
|
1
1
|
= Sinatra
|
2
2
|
|
3
|
-
Sinatra a DSL for quickly creating web-applications in Ruby with minimal
|
3
|
+
Sinatra is a DSL for quickly creating web-applications in Ruby with minimal
|
4
|
+
effort.
|
4
5
|
|
5
|
-
|
6
|
+
== Sample App
|
6
7
|
|
7
8
|
# myapp.rb
|
8
|
-
|
9
9
|
require 'rubygems'
|
10
10
|
require 'sinatra'
|
11
|
-
|
12
11
|
get '/' do
|
13
12
|
'Hello world!'
|
14
13
|
end
|
15
14
|
|
16
|
-
|
15
|
+
Run with <tt>ruby myapp.rb</tt> and view at <tt>http://localhost:4567</tt>
|
17
16
|
|
18
|
-
|
17
|
+
== HTTP Methods
|
19
18
|
|
20
19
|
get '/' do
|
21
20
|
.. show things ..
|
22
21
|
end
|
23
|
-
|
22
|
+
|
24
23
|
post '/' do
|
25
24
|
.. create something ..
|
26
25
|
end
|
27
|
-
|
26
|
+
|
28
27
|
put '/' do
|
29
28
|
.. update something ..
|
30
29
|
end
|
31
|
-
|
30
|
+
|
32
31
|
delete '/' do
|
33
32
|
.. annihilate something ..
|
34
33
|
end
|
35
|
-
|
34
|
+
|
36
35
|
head '/' do
|
37
|
-
|
36
|
+
|
38
37
|
end
|
39
38
|
|
40
|
-
NOTE:
|
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
|
41
42
|
|
42
|
-
|
43
|
+
== Routes
|
43
44
|
|
44
|
-
|
45
|
+
Routes are matched based on the order of declaration. The first route that
|
46
|
+
matches the request is invoked.
|
45
47
|
|
46
|
-
Simple
|
48
|
+
Simple:
|
47
49
|
|
48
50
|
get '/hi' do
|
49
51
|
...
|
50
52
|
end
|
51
|
-
|
52
|
-
|
53
|
+
|
54
|
+
Named parameters:
|
53
55
|
|
54
56
|
get '/:name' do
|
55
57
|
# matches /sinatra and the like and sets params[:name]
|
56
58
|
end
|
57
|
-
|
58
|
-
Splat'n
|
59
59
|
|
60
|
-
|
61
|
-
|
60
|
+
Splat parameters:
|
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"]
|
62
70
|
end
|
63
|
-
|
64
|
-
|
65
|
-
|
71
|
+
|
72
|
+
User agent matching:
|
73
|
+
|
66
74
|
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
|
67
75
|
"You're using Songbird version #{params[:agent][0]}"
|
68
76
|
end
|
@@ -77,84 +85,96 @@ Put all of your static content in the ./public directory
|
|
77
85
|
|
78
86
|
root
|
79
87
|
\ public
|
80
|
-
|
81
|
-
If a file exists that maps to the REQUEST_PATH then it is served and the request end; Sinatra will look for and event that matches the path otherwise
|
82
88
|
|
83
|
-
|
89
|
+
If a file exists that maps to the REQUEST_PATH then it is served and the
|
90
|
+
request ends. Otherwise, Sinatra will look for an event that matches the
|
91
|
+
path.
|
84
92
|
|
85
|
-
|
93
|
+
== Views
|
86
94
|
|
87
|
-
|
88
|
-
|
89
|
-
|
95
|
+
Views are searched for in a "views" directory in the same location as
|
96
|
+
your main application.
|
90
97
|
|
91
|
-
|
92
|
-
|
93
|
-
=== Haml
|
98
|
+
=== Haml Templates
|
94
99
|
|
95
100
|
get '/' do
|
96
101
|
haml :index
|
97
102
|
end
|
98
|
-
|
99
|
-
|
103
|
+
|
104
|
+
Renders <tt>./views/index.haml</tt>.
|
105
|
+
|
106
|
+
=== Erb
|
107
|
+
|
108
|
+
get '/' do
|
109
|
+
erb :index
|
110
|
+
end
|
111
|
+
|
112
|
+
Renders <tt>./views/index.erb</tt>
|
113
|
+
|
114
|
+
=== Builder
|
115
|
+
|
116
|
+
See Sinatra::Builder
|
100
117
|
|
101
118
|
=== Sass
|
119
|
+
|
102
120
|
get '/stylesheet.css' do
|
103
|
-
|
121
|
+
content_type 'text/css', :charset => 'utf-8'
|
104
122
|
sass :stylesheet
|
105
123
|
end
|
106
|
-
|
107
|
-
This will render <tt>./views/stylesheet.sass</tt>
|
108
124
|
|
109
|
-
|
125
|
+
Renders <tt>./views/stylesheet.sass</tt>.
|
126
|
+
|
127
|
+
=== Inline Templates
|
110
128
|
|
111
129
|
get '/' do
|
112
130
|
haml '%div.title Hello World'
|
113
131
|
end
|
114
|
-
|
115
|
-
|
132
|
+
|
133
|
+
Renders the inlined template string.
|
116
134
|
|
117
135
|
=== Accessing Variables
|
118
136
|
|
119
|
-
Templates are
|
137
|
+
Templates are evaluated within the Sinatra::EventContext instance
|
138
|
+
used to evaluate event blocks. Instance variables set in event
|
139
|
+
blocks can be accessed direcly in views:
|
120
140
|
|
121
141
|
get '/:id' do
|
122
142
|
@foo = Foo.find(params[:id])
|
123
143
|
haml '%h1== @foo.name'
|
124
144
|
end
|
125
145
|
|
126
|
-
|
146
|
+
Or, specify an explicit Hash of local variables:
|
127
147
|
|
128
148
|
get '/:id' do
|
129
|
-
|
130
|
-
haml '%h1==
|
149
|
+
foo = Foo.find(params[:id])
|
150
|
+
haml '%h1== foo.name', :locals => { :foo => foo }
|
131
151
|
end
|
132
|
-
|
133
|
-
This is more ideal for rendering templates as partials from within templates
|
134
152
|
|
135
|
-
|
153
|
+
This is typically used when rendering templates as partials from within
|
154
|
+
other templates.
|
155
|
+
|
156
|
+
=== In-file Templates
|
136
157
|
|
137
|
-
|
158
|
+
Templates may be defined at the end of the source file:
|
138
159
|
|
139
160
|
get '/' do
|
140
161
|
haml :index
|
141
162
|
end
|
142
|
-
|
163
|
+
|
143
164
|
use_in_file_templates!
|
144
|
-
|
165
|
+
|
145
166
|
__END__
|
146
|
-
|
147
|
-
|
167
|
+
|
168
|
+
@@ layout
|
148
169
|
X
|
149
170
|
= yield
|
150
171
|
X
|
151
|
-
|
152
|
-
|
172
|
+
|
173
|
+
@@ index
|
153
174
|
%div.title Hello world!!!!!
|
154
|
-
|
155
|
-
Try it!
|
156
175
|
|
157
|
-
|
176
|
+
It's also possible to define named templates using the top-level template
|
177
|
+
method:
|
158
178
|
|
159
179
|
template :layout do
|
160
180
|
"X\n=yield\nX"
|
@@ -168,73 +188,58 @@ Try it!
|
|
168
188
|
haml :index
|
169
189
|
end
|
170
190
|
|
171
|
-
|
172
|
-
|
173
|
-
This works like Haml except you use <tt>erb</tt> instead of <tt>haml</tt>
|
174
|
-
|
175
|
-
=== Sass
|
176
|
-
|
177
|
-
This works like Haml except you use <tt>sass</tt> instead of <tt>haml</tt>. It's also a good idea to add <tt>header 'Content-Type' => 'text/css; charset=utf-8'</tt> before your call to <tt>sass</tt> so Sinatra returns the proper content type header with the file.
|
178
|
-
|
179
|
-
=== Builder
|
180
|
-
|
181
|
-
See Sinatra::Builder
|
182
|
-
|
183
|
-
= Helpers
|
184
|
-
|
185
|
-
It is ill-advised to create helpers on (main). Use the handy <tt>helpers</tt> to install helper methods on Sinatra::EventContext for use inside events and templates.
|
191
|
+
== Helpers
|
186
192
|
|
187
|
-
|
193
|
+
The top-level <tt>helpers</tt> method takes a block and extends all
|
194
|
+
EventContext instances with the methods defined:
|
188
195
|
|
189
196
|
helpers do
|
190
|
-
|
191
197
|
def bar(name)
|
192
198
|
"#{name}bar"
|
193
199
|
end
|
194
|
-
|
195
200
|
end
|
196
|
-
|
201
|
+
|
197
202
|
get '/:name' do
|
198
203
|
bar(params[:name])
|
199
204
|
end
|
200
205
|
|
201
|
-
|
206
|
+
== Filters
|
202
207
|
|
203
|
-
These are run in Sinatra::EventContext
|
208
|
+
These are run in Sinatra::EventContext before every event.
|
204
209
|
|
205
210
|
before do
|
206
211
|
.. this code will run before each event ..
|
207
212
|
end
|
208
|
-
|
209
|
-
|
213
|
+
|
214
|
+
== Halt!
|
210
215
|
|
211
216
|
To immediately stop a request during a before filter or event use:
|
212
|
-
|
217
|
+
|
213
218
|
throw :halt
|
214
|
-
|
215
|
-
=== Variations
|
216
219
|
|
217
220
|
Set the body to the result of a helper method
|
218
221
|
|
219
222
|
throw :halt, :helper_method
|
220
|
-
|
221
|
-
Set the body to the result of a helper method after sending it parameters from
|
222
|
-
|
223
|
+
|
224
|
+
Set the body to the result of a helper method after sending it parameters from
|
225
|
+
the local scope
|
226
|
+
|
223
227
|
throw :halt, [:helper_method, foo, bar]
|
224
|
-
|
228
|
+
|
225
229
|
Set the body to a simple string
|
226
230
|
|
227
231
|
throw :halt, 'this will be the body'
|
228
|
-
|
232
|
+
|
229
233
|
Set status then the body
|
230
234
|
|
231
235
|
throw :halt, [401, 'go away!']
|
232
|
-
|
236
|
+
|
233
237
|
Set the status then call a helper method with params from local scope
|
234
238
|
|
235
239
|
throw :halt, [401, [:helper_method, foo, bar]]
|
236
240
|
|
237
|
-
Run a proc inside the Sinatra::EventContext instance and set the body to the
|
241
|
+
Run a proc inside the Sinatra::EventContext instance and set the body to the
|
242
|
+
result
|
238
243
|
|
239
244
|
throw :halt, lambda { puts 'In a proc!'; 'I just wrote to $stdout!' }
|
240
245
|
|
@@ -245,50 +250,55 @@ Create you own to_result
|
|
245
250
|
event_context.body = 'This will be the body!
|
246
251
|
end
|
247
252
|
end
|
248
|
-
|
253
|
+
|
249
254
|
get '/' do
|
250
255
|
throw :halt, MyResultObject.new
|
251
256
|
end
|
252
257
|
|
253
|
-
Get the gist? If you want more fun with this then checkout <tt>to_result</tt>
|
258
|
+
Get the gist? If you want more fun with this then checkout <tt>to_result</tt>
|
259
|
+
on Array, Symbol, Fixnum, NilClass.
|
254
260
|
|
255
|
-
|
261
|
+
== Configuration and Reloading
|
256
262
|
|
257
|
-
Sinatra supports multiple environments and re-loading. Re-loading happens on
|
263
|
+
Sinatra supports multiple environments and re-loading. Re-loading happens on
|
264
|
+
every request when in :development. Wrap your configurations in
|
265
|
+
<tt>configure</tt> (i.e. Database connections, Constants, etc.) to protect
|
266
|
+
them from re-loading and to only work in certain environments.
|
258
267
|
|
259
268
|
All environments:
|
260
269
|
|
261
270
|
configure do
|
262
|
-
|
271
|
+
|
263
272
|
end
|
264
|
-
|
273
|
+
|
265
274
|
Production
|
266
275
|
|
267
276
|
configure :production do
|
268
|
-
|
277
|
+
|
269
278
|
end
|
270
|
-
|
279
|
+
|
271
280
|
Two at a time:
|
272
281
|
|
273
282
|
configure :production, :test do
|
274
|
-
|
283
|
+
|
275
284
|
end
|
276
|
-
|
285
|
+
|
277
286
|
This is also really nifty for error handling.
|
278
287
|
|
279
288
|
= Error handling
|
280
289
|
|
281
|
-
|
290
|
+
== Not Found
|
282
291
|
|
283
|
-
Remember: These are run inside the Sinatra::EventContext which means you get
|
292
|
+
Remember: These are run inside the Sinatra::EventContext which means you get
|
293
|
+
all the goodies is has to offer (i.e. haml, erb, :halt, etc.)
|
284
294
|
|
285
295
|
Whenever NotFound is raised this will be called
|
286
296
|
|
287
297
|
not_found do
|
288
298
|
'This is nowhere to be found'
|
289
299
|
end
|
290
|
-
|
291
|
-
|
300
|
+
|
301
|
+
== Error
|
292
302
|
|
293
303
|
By default +error+ will catch Sinatra::ServerError
|
294
304
|
|
@@ -301,7 +311,7 @@ Sinatra will pass you the error via the 'sinatra.error' in request.env
|
|
301
311
|
Custom error mapping:
|
302
312
|
|
303
313
|
error MyCustomError do
|
304
|
-
'So what happened was...' + request.env['sinatra.
|
314
|
+
'So what happened was...' + request.env['sinatra.error'].message
|
305
315
|
end
|
306
316
|
|
307
317
|
then if this happens:
|
@@ -313,87 +323,135 @@ then if this happens:
|
|
313
323
|
you gets this:
|
314
324
|
|
315
325
|
So what happened was... something bad
|
316
|
-
|
326
|
+
|
317
327
|
one guess what this does ;)
|
318
328
|
|
319
329
|
not_found do
|
320
330
|
'I have no clue what you're looking for'
|
321
331
|
end
|
322
332
|
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
Because Sinatra give you a default <tt>not_found</tt> and <tt>error</tt> do :production that are secure. If you want to customize only for :production but want to keep the friendly helper screens for :development then do this:
|
333
|
+
Because Sinatra gives you a default <tt>not_found</tt> and <tt>error</tt> do
|
334
|
+
:production that are secure. If you want to customize only for :production
|
335
|
+
but want to keep the friendly helper screens for :development then do this:
|
327
336
|
|
328
337
|
configure :production do
|
329
338
|
|
330
339
|
not_found do
|
331
340
|
"We're so sorry, but we don't what this is"
|
332
341
|
end
|
333
|
-
|
342
|
+
|
334
343
|
error do
|
335
344
|
"Something really nasty happened. We're on it!"
|
336
345
|
end
|
337
|
-
|
346
|
+
|
338
347
|
end
|
339
|
-
|
340
|
-
= Mime types
|
341
348
|
|
342
|
-
|
349
|
+
== Mime types
|
350
|
+
|
351
|
+
When using send_file or static files you may have mime types Sinatra doesn't
|
352
|
+
understand. Use +mime+ in those cases.
|
343
353
|
|
344
354
|
mime :foo, 'text/foo'
|
345
355
|
|
346
|
-
|
356
|
+
== Rack Middleware
|
357
|
+
|
358
|
+
Sinatra rides on Rack[http://rack.rubyforge.org/], a minimal standard
|
359
|
+
interface for Ruby web frameworks. One of Rack's most interesting capabilities
|
360
|
+
for application developers is support for "middleware" -- components that sit
|
361
|
+
between the server and your application monitoring and/or manipulating the
|
362
|
+
HTTP request/response to provide various types of common functionality.
|
363
|
+
What's more, middleware is portable between web frameworks, so middleware
|
364
|
+
components developed under, e.g., Merb, can be used with Sinatra and vice
|
365
|
+
versa.
|
366
|
+
|
367
|
+
Sinatra makes building Rack middleware pipelines a cinch via a top-level +use+
|
368
|
+
method:
|
369
|
+
|
370
|
+
require 'sinatra'
|
371
|
+
require 'my_custom_middleware'
|
372
|
+
|
373
|
+
use Rack::Lint
|
374
|
+
use MyCustomMiddleware
|
375
|
+
|
376
|
+
get '/hello' do
|
377
|
+
'Hello World'
|
378
|
+
end
|
379
|
+
|
380
|
+
The semantics of +use+ are identical to those defined for the
|
381
|
+
Rack::Builder[http://rack.rubyforge.org/doc/classes/Rack/Builder.html] DSL
|
382
|
+
(most frequently used from rackup files). For example, the +use+ method
|
383
|
+
accepts multiple/variable args as well as blocks:
|
384
|
+
|
385
|
+
use Rack::Auth::Basic do |username, password|
|
386
|
+
username == 'admin' && password == 'secret'
|
387
|
+
end
|
388
|
+
|
389
|
+
Rack is distributed with a variety of standard middleware for logging,
|
390
|
+
debugging, URL routing, authentication, and session handling. Sinatra uses
|
391
|
+
many of of these components automatically based on configuration so you
|
392
|
+
typically don't have to +use+ them explicitly.
|
393
|
+
|
394
|
+
== Testing
|
395
|
+
|
396
|
+
=== Methods
|
397
|
+
|
398
|
+
get_it path, params
|
399
|
+
get_it path, params.merge(:env => { 'HTTP_HOST' => 'www.sinatrarb.com' }) or
|
400
|
+
get_it path, params.merge(:env => { :host => 'www.sinatrarb.com' })
|
401
|
+
|
402
|
+
RESTful:
|
403
|
+
|
404
|
+
post_it '/foo', '<myxml></myxml>', 'HTTP_ACCEPT' => 'application/xml'
|
405
|
+
|
406
|
+
also works with:
|
407
|
+
|
408
|
+
get_it, post_it, put_it, delete_it, head_it
|
347
409
|
|
348
410
|
=== Test/Unit
|
349
411
|
|
350
412
|
require 'my_sinatra_app'
|
351
413
|
require 'sinatra/test/unit'
|
352
|
-
|
414
|
+
|
353
415
|
class MyAppTest < Test::Unit::TestCase
|
354
|
-
|
416
|
+
|
355
417
|
def test_my_default
|
356
418
|
get_it '/'
|
357
419
|
assert_equal 'My Default Page!', @response.body
|
358
420
|
end
|
359
|
-
|
421
|
+
|
360
422
|
def test_with_agent
|
361
423
|
get_it '/', :agent => 'Songbird'
|
362
424
|
assert_equal 'You're in Songbird!', @response.body
|
363
425
|
end
|
364
|
-
|
426
|
+
|
365
427
|
...
|
366
|
-
|
428
|
+
|
367
429
|
end
|
368
|
-
|
369
|
-
===
|
430
|
+
|
431
|
+
=== Specs
|
370
432
|
|
371
433
|
require 'my_sinatra_app'
|
372
434
|
require 'sinatra/test/spec'
|
373
435
|
|
374
436
|
context 'My app'
|
375
|
-
|
437
|
+
|
376
438
|
should "show a default page" do
|
377
439
|
get_it '/'
|
378
440
|
should.be.ok
|
379
441
|
body.should.equal 'My Default Page!'
|
380
442
|
end
|
381
443
|
...
|
382
|
-
|
383
|
-
end
|
384
|
-
|
385
|
-
== Test helpers
|
386
444
|
|
387
|
-
|
445
|
+
end
|
388
446
|
|
389
|
-
|
447
|
+
=== Test Helpers
|
390
448
|
|
391
|
-
|
449
|
+
See Sinatra::Test::Methods
|
392
450
|
|
393
|
-
|
451
|
+
== Command line
|
394
452
|
|
395
453
|
Run your sinatra file like:
|
396
|
-
|
454
|
+
|
397
455
|
ruby myapp.rb [options]
|
398
456
|
|
399
457
|
Options are:
|
@@ -401,30 +459,75 @@ Options are:
|
|
401
459
|
-h # help
|
402
460
|
-p # set the port (default is 4567)
|
403
461
|
-e # set the environment (default is development)
|
404
|
-
-x # turn on the
|
462
|
+
-x # turn on the mutex lock (default is off)
|
463
|
+
|
464
|
+
== Contributing
|
465
|
+
|
466
|
+
=== Tools
|
467
|
+
|
468
|
+
Besides Ruby itself, you only need a text editor, preferably one that supports
|
469
|
+
Ruby syntax hilighting. VIM and Emacs are a fine choice on any platform, but
|
470
|
+
feel free to use whatever you're familiar with.
|
471
|
+
|
472
|
+
Sinatra uses the Git source code management system. If you're unfamiliar with
|
473
|
+
Git, you can find more information and tutorials on http://git.or.cz/ as well
|
474
|
+
as http://git-scm.com/. Scott Chacon created a great series of introductory
|
475
|
+
screencasts about Git, which you can find here: http://www.gitcasts.com/
|
405
476
|
|
406
|
-
|
477
|
+
=== First Time: Cloning The Sinatra Repo
|
407
478
|
|
408
479
|
cd where/you/keep/your/projects
|
409
480
|
git clone git://github.com/bmizerany/sinatra.git
|
410
|
-
cd
|
481
|
+
cd sinatra
|
482
|
+
cd path/to/your_project
|
411
483
|
ln -s ../sinatra/
|
412
484
|
|
413
|
-
|
485
|
+
=== Updating Your Existing Sinatra Clone
|
486
|
+
|
487
|
+
cd where/you/keep/sinatra
|
488
|
+
git pull
|
489
|
+
|
490
|
+
=== Using Edge Sinatra in Your App
|
491
|
+
|
492
|
+
at the top of your sinatra_app.rb file:
|
414
493
|
|
415
494
|
$:.unshift File.dirname(__FILE__) + '/sinatra/lib'
|
416
495
|
require 'sinatra'
|
417
496
|
|
418
497
|
get '/about' do
|
419
|
-
"I'm running on Version " + Sinatra::
|
498
|
+
"I'm running on Version " + Sinatra::VERSION
|
420
499
|
end
|
421
|
-
|
422
500
|
|
501
|
+
=== Contributing a Patch
|
502
|
+
|
503
|
+
There are several ways to do this. Probably the easiest (and preferred) way is
|
504
|
+
to fork Sinatra on GitHub (http://github.com/bmizerany/sinatra), push your
|
505
|
+
changes to your Sinatra repo, and then send Blake Mizerany (bmizerany on
|
506
|
+
GitHub) a pull request.
|
507
|
+
|
508
|
+
You can also create a patch file and attach it to a feature request or bug fix
|
509
|
+
on the issue tracker (see below) or send it to the mailing list (see Community
|
510
|
+
section).
|
511
|
+
|
512
|
+
=== Issue Tracking and Feature Requests
|
423
513
|
|
514
|
+
http://sinatra.lighthouseapp.com/
|
424
515
|
|
516
|
+
== Community
|
425
517
|
|
518
|
+
=== Mailing List
|
426
519
|
|
520
|
+
http://groups.google.com/group/sinatrarb
|
427
521
|
|
522
|
+
If you have a problem or question, please make sure to include all the
|
523
|
+
relevant information in your mail, like the Sinatra version you're using, what
|
524
|
+
version of Ruby you have, and so on.
|
428
525
|
|
526
|
+
=== IRC Channel
|
429
527
|
|
528
|
+
You can find us on the Freenode network in the channel #sinatra
|
529
|
+
(irc://chat.freenode.net/#sinatra)
|
430
530
|
|
531
|
+
There's usually someone online at any given time, but we cannot pay attention
|
532
|
+
to the channel all the time, so please stick around for a while after asking a
|
533
|
+
question.
|