public
Rubygem
Description: Classy web-development dressed in a DSL
Homepage: http://sinatrarb.com
Clone URL: git://github.com/bmizerany/sinatra.git
Search Repo:
Added Version and Docs
Blake Mizerany (author)
Mon Mar 24 18:28:24 -0700 2008
commit  1776a80d2c0e489a38d18afb58c3cc48cefacbf4
tree    06dea6c1562c69c156c43764e94e1ed4d8155f85
parent  0b485a96a3dca775e9011e19698af11b58b43e8b
...
32
33
34
 
 
 
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
37
38
39
...
98
99
100
 
 
 
 
101
102
103
 
 
104
105
106
...
150
151
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
0
@@ -32,7 +32,45 @@
0
   delete '/' do
0
     .. annihilate something ..
0
   end
0
+
0
+ head '/' do
0
+
0
+ end
0
 
0
+NOTE: <tt>put</tt> and <tt>delete</tt> are triggered when a <tt>_method</tt> param is set to PUT or DELETE and the HTTP_REQUEST_METHOD is a POST
0
+
0
+= Routes
0
+
0
+NOTE: Routes are looked up in order of declaration
0
+
0
+Simple
0
+
0
+ get '/hi' do
0
+ ...
0
+ end
0
+
0
+With params
0
+
0
+ get '/:name' do
0
+ # matches /sinatra and the like and sets params[:name]
0
+ end
0
+
0
+Splat'n
0
+
0
+ get '/message/*' do
0
+ # matches /message/1/2/3/4/5
0
+ end
0
+
0
+Get an agent!
0
+
0
+ get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
0
+ "You're using Songbird version #{params[:agent][0]}"
0
+ end
0
+
0
+ get '/foo' do
0
+ # matches non-songbird browsers
0
+ end
0
+
0
 = Views (if you need MVC)
0
 
0
 All views are looked up in:
0
0
@@ -98,9 +136,15 @@
0
     end
0
     
0
   end
0
+
0
+ get '/:name' do
0
+ bar(params[:name])
0
+ end
0
 
0
 = Before filters
0
 
0
+These are run in Sinatra::EventContext
0
+
0
   before do
0
     .. this code will run before each event ..
0
   end
0
@@ -150,4 +194,139 @@
0
   end
0
 
0
 Get the gist? If you want more fun with this then checkout <tt>to_result</tt> on Array, Symbol, Fixnum, NilClass.
0
+
0
+= Configuration & Re-loading
0
+
0
+Sinatra supports multiple environments and re-loading. Re-loading happens on every request when in :development. Wrap your configurations in <tt>configure</tt> (i.e. Database connections, Constants, etc.) to protect them from re-loading and to only work in certain environments.
0
+
0
+All environments:
0
+
0
+ configure do
0
+
0
+ end
0
+
0
+Production
0
+
0
+ configure :production do
0
+
0
+ end
0
+
0
+Two at a time:
0
+
0
+ configure :production, :test do
0
+
0
+ end
0
+
0
+This is also really nifty for error handling.
0
+
0
+= Error handling
0
+
0
+=== Not Found
0
+
0
+Remember: These are run inside the Sinatra::EventContext which means you get all the goodies is has to offer (i.e. haml, erb, :halt, etc.)
0
+
0
+Whenever NotFound is raised this will be called
0
+
0
+ not_found do
0
+ 'This is nowhere to be found'
0
+ end
0
+
0
+=== Error
0
+
0
+ error do
0
+ # this is where the error is stored for you to grab
0
+ name = env['sinatra.error'].class.name
0
+ 'ah shizzle! ' + name
0
+ end
0
+
0
+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:
0
+
0
+ configure :production do
0
+
0
+ not_found do
0
+ "We're so sorry, but we don't what this is"
0
+ end
0
+
0
+ error do
0
+ "Something really nasty happened. We're on it!"
0
+ end
0
+
0
+ end
0
+
0
+= Testing
0
+
0
+=== Test/Unit
0
+
0
+ require 'my_sinatra_app'
0
+ require 'sinatra/test/unit'
0
+
0
+ class MyAppTest < Test::Unit::TestCase
0
+
0
+ def test_my_default
0
+ get_it '/'
0
+ assert_equal 'My Default Page!', @response.body
0
+ end
0
+
0
+ def test_with_agent
0
+ get_it '/', :agent => 'Songbird'
0
+ assert_equal 'You're in Songbird!', @response.body
0
+ end
0
+
0
+ ...
0
+
0
+ end
0
+
0
+=== Test/Spec
0
+
0
+ require 'my_sinatra_app'
0
+ require 'sinatra/test/spec'
0
+
0
+ context 'My app'
0
+
0
+ should "show a default page" do
0
+ get_it '/'
0
+ should.be.ok
0
+ @response.body.should.equal 'My Default Page!'
0
+ end
0
+ ...
0
+
0
+ end
0
+
0
+== Test helpers
0
+
0
+See Sinatra::Test::Methods
0
+
0
+= Irb
0
+
0
+This will be back in soon
0
+
0
+= Command line
0
+
0
+Run your sinatra file like:
0
+
0
+ ruby myapp.rb [options]
0
+
0
+Options are:
0
+
0
+ -h # help
0
+ -p # set the port (default is 4567)
0
+ -e # set the environment (default is development)
0
+ -x # turn on the mutext lock (default is off)
0
+
0
+= Contribute
0
+
1
+ cd where/you/keep/your/projects
0
+ git clone git://github.com/bmizerany/sinatra.git
0
+ cd your_project
0
+ ln -s ../sinatra/
0
+
0
+at the top of your sinatra.rb file
0
+
0
+ $:.unshift File.dirname(__FILE__) + '/sinatra/lib'
0
+ require 'sinatra'
0
+
0
+ get '/about' do
0
+ "I'm running on Version " + Sinatra::Version.combined
0
+ end
0
+
...
76
77
78
 
 
 
 
 
 
 
 
 
79
80
81
...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
0
@@ -76,6 +76,15 @@
0
 module Sinatra
0
   extend self
0
 
0
+ module Version
0
+ MAJOR = '0'
0
+ MINOR = '2'
0
+ REVISION = '0'
0
+ def self.combined
0
+ [MAJOR, MINOR, REVISION].join('.')
0
+ end
0
+ end
0
+
0
   class NotFound < RuntimeError; end
0
   class ServerError < RuntimeError; end
0
 

Comments

  • kastner Sat May 03 19:41:53 -0700 2008 at README.rdoc L219

    This should be ln -s ../sinatra (no trailing slash)

  • bmizerany Sun May 04 15:43:08 -0700 2008

    you’re right. thx.