sinatra 0.1.7 → 0.2.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.

Files changed (95) hide show
  1. data/CHANGELOG +1 -8
  2. data/Manifest +42 -49
  3. data/README.rdoc +430 -0
  4. data/Rakefile +22 -28
  5. data/images/404.png +0 -0
  6. data/images/500.png +0 -0
  7. data/index.html +9 -0
  8. data/lib/sinatra.rb +1144 -46
  9. data/lib/sinatra/test/methods.rb +56 -0
  10. data/lib/sinatra/test/spec.rb +10 -0
  11. data/lib/sinatra/test/unit.rb +13 -0
  12. data/sinatra.gemspec +44 -40
  13. data/test/app_test.rb +150 -0
  14. data/test/application_test.rb +175 -0
  15. data/test/builder_test.rb +101 -0
  16. data/test/custom_error_test.rb +67 -0
  17. data/test/diddy_test.rb +41 -0
  18. data/test/erb_test.rb +116 -0
  19. data/test/event_context_test.rb +15 -0
  20. data/test/events_test.rb +50 -0
  21. data/test/haml_test.rb +181 -0
  22. data/test/helper.rb +3 -16
  23. data/test/mapped_error_test.rb +61 -0
  24. data/test/public/foo.xml +1 -0
  25. data/test/rest_test.rb +16 -0
  26. data/test/sass_test.rb +57 -0
  27. data/test/sessions_test.rb +40 -0
  28. data/test/streaming_test.rb +112 -0
  29. data/test/sym_params_test.rb +19 -0
  30. data/test/template_test.rb +30 -0
  31. data/test/use_in_file_templates_test.rb +48 -0
  32. data/test/views/foo.builder +1 -0
  33. data/test/views/foo.erb +1 -0
  34. data/test/views/foo.haml +1 -0
  35. data/test/views/foo.sass +2 -0
  36. data/test/views/foo_layout.erb +2 -0
  37. data/test/views/foo_layout.haml +2 -0
  38. data/test/views/layout_test/foo.builder +1 -0
  39. data/test/views/layout_test/foo.erb +1 -0
  40. data/test/views/layout_test/foo.haml +1 -0
  41. data/test/views/layout_test/foo.sass +2 -0
  42. data/test/views/layout_test/layout.builder +3 -0
  43. data/test/views/layout_test/layout.erb +1 -0
  44. data/test/views/layout_test/layout.haml +1 -0
  45. data/test/views/layout_test/layout.sass +2 -0
  46. data/test/views/no_layout/no_layout.builder +1 -0
  47. data/test/views/no_layout/no_layout.haml +1 -0
  48. metadata +122 -98
  49. data/LICENSE +0 -22
  50. data/README +0 -100
  51. data/RakeFile +0 -35
  52. data/examples/hello/hello.rb +0 -28
  53. data/examples/hello/views/hello.erb +0 -1
  54. data/examples/todo/todo.rb +0 -38
  55. data/files/default_index.erb +0 -42
  56. data/files/error.erb +0 -9
  57. data/files/logo.png +0 -0
  58. data/files/not_found.erb +0 -52
  59. data/lib/sinatra/context.rb +0 -88
  60. data/lib/sinatra/context/renderer.rb +0 -75
  61. data/lib/sinatra/core_ext/array.rb +0 -5
  62. data/lib/sinatra/core_ext/class.rb +0 -49
  63. data/lib/sinatra/core_ext/hash.rb +0 -7
  64. data/lib/sinatra/core_ext/kernel.rb +0 -16
  65. data/lib/sinatra/core_ext/metaid.rb +0 -18
  66. data/lib/sinatra/core_ext/module.rb +0 -11
  67. data/lib/sinatra/core_ext/symbol.rb +0 -5
  68. data/lib/sinatra/dispatcher.rb +0 -27
  69. data/lib/sinatra/dsl.rb +0 -176
  70. data/lib/sinatra/environment.rb +0 -15
  71. data/lib/sinatra/event.rb +0 -238
  72. data/lib/sinatra/irb.rb +0 -56
  73. data/lib/sinatra/loader.rb +0 -31
  74. data/lib/sinatra/logger.rb +0 -22
  75. data/lib/sinatra/options.rb +0 -49
  76. data/lib/sinatra/rack_ext/request.rb +0 -15
  77. data/lib/sinatra/route.rb +0 -65
  78. data/lib/sinatra/server.rb +0 -57
  79. data/lib/sinatra/sessions.rb +0 -21
  80. data/lib/sinatra/test_methods.rb +0 -55
  81. data/site/index.htm +0 -104
  82. data/site/index.html +0 -104
  83. data/site/logo.png +0 -0
  84. data/test/sinatra/dispatcher_test.rb +0 -91
  85. data/test/sinatra/event_test.rb +0 -46
  86. data/test/sinatra/renderer_test.rb +0 -47
  87. data/test/sinatra/request_test.rb +0 -21
  88. data/test/sinatra/route_test.rb +0 -21
  89. data/test/sinatra/static_files/foo.txt +0 -1
  90. data/test/sinatra/static_files_test.rb +0 -48
  91. data/test/sinatra/url_test.rb +0 -18
  92. data/vendor/erb/init.rb +0 -3
  93. data/vendor/erb/lib/erb.rb +0 -41
  94. data/vendor/haml/init.rb +0 -3
  95. data/vendor/haml/lib/haml.rb +0 -41
data/CHANGELOG CHANGED
@@ -1,8 +1 @@
1
- v0.1.7 FIXES:
2
-
3
- * test/development/production helpers changed to config_for. There was a problem with "test" fubar'ing libraries such as the new release of Sequel
4
- * StaticEvent now makes sure the file it's looking for is really a file.
5
-
6
- v0.1.6 Additions: require users vendor directory on load
7
- v0.1.5 Mutex and before-filters
8
- v0.1.0 Released!
1
+ v0.2.0 Released!
data/Manifest CHANGED
@@ -1,52 +1,45 @@
1
1
  CHANGELOG
2
- examples/hello/hello.rb
3
- examples/hello/views/hello.erb
4
- examples/todo/todo.rb
5
- files/default_index.erb
6
- files/error.erb
7
- files/logo.png
8
- files/not_found.erb
9
- lib/sinatra/context/renderer.rb
10
- lib/sinatra/context.rb
11
- lib/sinatra/core_ext/array.rb
12
- lib/sinatra/core_ext/class.rb
13
- lib/sinatra/core_ext/hash.rb
14
- lib/sinatra/core_ext/kernel.rb
15
- lib/sinatra/core_ext/metaid.rb
16
- lib/sinatra/core_ext/module.rb
17
- lib/sinatra/core_ext/symbol.rb
18
- lib/sinatra/dispatcher.rb
19
- lib/sinatra/dsl.rb
20
- lib/sinatra/environment.rb
21
- lib/sinatra/event.rb
22
- lib/sinatra/irb.rb
23
- lib/sinatra/loader.rb
24
- lib/sinatra/logger.rb
25
- lib/sinatra/options.rb
26
- lib/sinatra/rack_ext/request.rb
27
- lib/sinatra/route.rb
28
- lib/sinatra/server.rb
29
- lib/sinatra/sessions.rb
30
- lib/sinatra/test_methods.rb
2
+ images/404.png
3
+ images/500.png
4
+ index.html
5
+ lib/sinatra/test/methods.rb
6
+ lib/sinatra/test/spec.rb
7
+ lib/sinatra/test/unit.rb
31
8
  lib/sinatra.rb
32
- LICENSE
33
- Manifest
34
- RakeFile
35
- README
36
- site/index.htm
37
- site/index.html
38
- site/logo.png
9
+ README.rdoc
10
+ test/app_test.rb
11
+ test/application_test.rb
12
+ test/builder_test.rb
13
+ test/custom_error_test.rb
14
+ test/diddy_test.rb
15
+ test/erb_test.rb
16
+ test/event_context_test.rb
17
+ test/events_test.rb
18
+ test/haml_test.rb
39
19
  test/helper.rb
40
- test/sinatra/dispatcher_test.rb
41
- test/sinatra/event_test.rb
42
- test/sinatra/renderer_test.rb
43
- test/sinatra/request_test.rb
44
- test/sinatra/route_test.rb
45
- test/sinatra/static_files/foo.txt
46
- test/sinatra/static_files_test.rb
47
- test/sinatra/url_test.rb
48
- vendor/erb/init.rb
49
- vendor/erb/lib/erb.rb
50
- vendor/haml/init.rb
51
- vendor/haml/lib/haml.rb
52
- Rakefile
20
+ test/mapped_error_test.rb
21
+ test/public/foo.xml
22
+ test/rest_test.rb
23
+ test/sass_test.rb
24
+ test/sessions_test.rb
25
+ test/streaming_test.rb
26
+ test/sym_params_test.rb
27
+ test/template_test.rb
28
+ test/use_in_file_templates_test.rb
29
+ test/views/foo.builder
30
+ test/views/foo.erb
31
+ test/views/foo.haml
32
+ test/views/foo.sass
33
+ test/views/foo_layout.erb
34
+ test/views/foo_layout.haml
35
+ test/views/layout_test/foo.builder
36
+ test/views/layout_test/foo.erb
37
+ test/views/layout_test/foo.haml
38
+ test/views/layout_test/foo.sass
39
+ test/views/layout_test/layout.builder
40
+ test/views/layout_test/layout.erb
41
+ test/views/layout_test/layout.haml
42
+ test/views/layout_test/layout.sass
43
+ test/views/no_layout/no_layout.builder
44
+ test/views/no_layout/no_layout.haml
45
+ Manifest
@@ -0,0 +1,430 @@
1
+ = Sinatra
2
+
3
+ Sinatra a DSL for quickly creating web-applications in Ruby with minimal effort.
4
+
5
+ = Sample app:
6
+
7
+ # myapp.rb
8
+
9
+ require 'rubygems'
10
+ require 'sinatra'
11
+
12
+ get '/' do
13
+ 'Hello world!'
14
+ end
15
+
16
+ Ruby this as <tt>ruby myapp.rb</tt> and view at <tt>http://localhost:4567</tt>
17
+
18
+ = RESTful
19
+
20
+ get '/' do
21
+ .. show things ..
22
+ end
23
+
24
+ post '/' do
25
+ .. create something ..
26
+ end
27
+
28
+ put '/' do
29
+ .. update something ..
30
+ end
31
+
32
+ delete '/' do
33
+ .. annihilate something ..
34
+ end
35
+
36
+ head '/' do
37
+
38
+ end
39
+
40
+ 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
41
+
42
+ = Routes
43
+
44
+ NOTE: Routes are looked up in order of declaration
45
+
46
+ Simple
47
+
48
+ get '/hi' do
49
+ ...
50
+ end
51
+
52
+ With params
53
+
54
+ get '/:name' do
55
+ # matches /sinatra and the like and sets params[:name]
56
+ end
57
+
58
+ Splat'n
59
+
60
+ get '/message/*' do
61
+ # matches /message/1/2/3/4/5
62
+ end
63
+
64
+ Get an agent!
65
+
66
+ get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
67
+ "You're using Songbird version #{params[:agent][0]}"
68
+ end
69
+
70
+ get '/foo' do
71
+ # matches non-songbird browsers
72
+ end
73
+
74
+ = Static files
75
+
76
+ Put all of your static content in the ./public directory
77
+
78
+ root
79
+ \ 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
+
83
+ = Views (if you need MVC)
84
+
85
+ All views are looked up in:
86
+
87
+ root
88
+ | - views/
89
+
90
+
91
+ == Templates
92
+
93
+ === Haml
94
+
95
+ get '/' do
96
+ haml :index
97
+ end
98
+
99
+ This will render <tt>./views/index.haml</tt>
100
+
101
+ === Sass
102
+ get '/stylesheet.css' do
103
+ header 'Content-Type' => 'text/css; charset=utf-8'
104
+ sass :stylesheet
105
+ end
106
+
107
+ This will render <tt>./views/stylesheet.sass</tt>
108
+
109
+ === Inline
110
+
111
+ get '/' do
112
+ haml '%div.title Hello World'
113
+ end
114
+
115
+ This will render the inlined template string
116
+
117
+ === Accessing Variables
118
+
119
+ Templates are rendered in the context the current Sinatra::EventContext. This means you get all instance/class variables and methods it has access to.
120
+
121
+ get '/:id' do
122
+ @foo = Foo.find(params[:id])
123
+ haml '%h1== @foo.name'
124
+ end
125
+
126
+ Send local objects like:
127
+
128
+ get '/:id' do
129
+ localvar = Foo.find(params[:id])
130
+ haml '%h1== localvar.name', :locals => { :localvar => localvar }
131
+ end
132
+
133
+ This is more ideal for rendering templates as partials from within templates
134
+
135
+ == In file templates
136
+
137
+ This one is cool:
138
+
139
+ get '/' do
140
+ haml :index
141
+ end
142
+
143
+ use_in_file_templates!
144
+
145
+ __END__
146
+
147
+ ## layout
148
+ X
149
+ = yield
150
+ X
151
+
152
+ ## index
153
+ %div.title Hello world!!!!!
154
+
155
+ Try it!
156
+
157
+ = You can do this too but it's not as cool
158
+
159
+ template :layout do
160
+ "X\n=yield\nX"
161
+ end
162
+
163
+ template :index do
164
+ '%div.title Hello World!'
165
+ end
166
+
167
+ get '/' do
168
+ haml :index
169
+ end
170
+
171
+ === Erb
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.
186
+
187
+ Example:
188
+
189
+ helpers do
190
+
191
+ def bar(name)
192
+ "#{name}bar"
193
+ end
194
+
195
+ end
196
+
197
+ get '/:name' do
198
+ bar(params[:name])
199
+ end
200
+
201
+ = Before filters
202
+
203
+ These are run in Sinatra::EventContext
204
+
205
+ before do
206
+ .. this code will run before each event ..
207
+ end
208
+
209
+ = Halt!
210
+
211
+ To immediately stop a request during a before filter or event use:
212
+
213
+ throw :halt
214
+
215
+ === Variations
216
+
217
+ Set the body to the result of a helper method
218
+
219
+ throw :halt, :helper_method
220
+
221
+ Set the body to the result of a helper method after sending it parameters from the local scope
222
+
223
+ throw :halt, [:helper_method, foo, bar]
224
+
225
+ Set the body to a simple string
226
+
227
+ throw :halt, 'this will be the body'
228
+
229
+ Set status then the body
230
+
231
+ throw :halt, [401, 'go away!']
232
+
233
+ Set the status then call a helper method with params from local scope
234
+
235
+ throw :halt, [401, [:helper_method, foo, bar]]
236
+
237
+ Run a proc inside the Sinatra::EventContext instance and set the body to the result
238
+
239
+ throw :halt, lambda { puts 'In a proc!'; 'I just wrote to $stdout!' }
240
+
241
+ Create you own to_result
242
+
243
+ class MyResultObject
244
+ def to_result(event_context, *args)
245
+ event_context.body = 'This will be the body!
246
+ end
247
+ end
248
+
249
+ get '/' do
250
+ throw :halt, MyResultObject.new
251
+ end
252
+
253
+ Get the gist? If you want more fun with this then checkout <tt>to_result</tt> on Array, Symbol, Fixnum, NilClass.
254
+
255
+ = Configuration & Re-loading
256
+
257
+ 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.
258
+
259
+ All environments:
260
+
261
+ configure do
262
+
263
+ end
264
+
265
+ Production
266
+
267
+ configure :production do
268
+
269
+ end
270
+
271
+ Two at a time:
272
+
273
+ configure :production, :test do
274
+
275
+ end
276
+
277
+ This is also really nifty for error handling.
278
+
279
+ = Error handling
280
+
281
+ === Not Found
282
+
283
+ 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.)
284
+
285
+ Whenever NotFound is raised this will be called
286
+
287
+ not_found do
288
+ 'This is nowhere to be found'
289
+ end
290
+
291
+ === Error
292
+
293
+ By default +error+ will catch Sinatra::ServerError
294
+
295
+ Sinatra will pass you the error via the 'sinatra.error' in request.env
296
+
297
+ error do
298
+ 'Sorry there was a nasty error - ' + request.env['sinatra.error'].name
299
+ end
300
+
301
+ Custom error mapping:
302
+
303
+ error MyCustomError do
304
+ 'So what happened was...' + request.env['sinatra.env'].message
305
+ end
306
+
307
+ then if this happens:
308
+
309
+ get '/' do
310
+ raise MyCustomError, 'something bad'
311
+ end
312
+
313
+ you gets this:
314
+
315
+ So what happened was... something bad
316
+
317
+ one guess what this does ;)
318
+
319
+ not_found do
320
+ 'I have no clue what you're looking for'
321
+ end
322
+
323
+ Try it!
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:
327
+
328
+ configure :production do
329
+
330
+ not_found do
331
+ "We're so sorry, but we don't what this is"
332
+ end
333
+
334
+ error do
335
+ "Something really nasty happened. We're on it!"
336
+ end
337
+
338
+ end
339
+
340
+ = Mime types
341
+
342
+ When using send_file or static files you may have mime types Sinatra doesn't understand. Use +mime+ in those cases.
343
+
344
+ mime :foo, 'text/foo'
345
+
346
+ = Testing
347
+
348
+ === Test/Unit
349
+
350
+ require 'my_sinatra_app'
351
+ require 'sinatra/test/unit'
352
+
353
+ class MyAppTest < Test::Unit::TestCase
354
+
355
+ def test_my_default
356
+ get_it '/'
357
+ assert_equal 'My Default Page!', @response.body
358
+ end
359
+
360
+ def test_with_agent
361
+ get_it '/', :agent => 'Songbird'
362
+ assert_equal 'You're in Songbird!', @response.body
363
+ end
364
+
365
+ ...
366
+
367
+ end
368
+
369
+ === Test/Spec
370
+
371
+ require 'my_sinatra_app'
372
+ require 'sinatra/test/spec'
373
+
374
+ context 'My app'
375
+
376
+ should "show a default page" do
377
+ get_it '/'
378
+ should.be.ok
379
+ body.should.equal 'My Default Page!'
380
+ end
381
+ ...
382
+
383
+ end
384
+
385
+ == Test helpers
386
+
387
+ See Sinatra::Test::Methods
388
+
389
+ = Irb
390
+
391
+ This will be back in soon
392
+
393
+ = Command line
394
+
395
+ Run your sinatra file like:
396
+
397
+ ruby myapp.rb [options]
398
+
399
+ Options are:
400
+
401
+ -h # help
402
+ -p # set the port (default is 4567)
403
+ -e # set the environment (default is development)
404
+ -x # turn on the mutext lock (default is off)
405
+
406
+ = Contribute
407
+
408
+ cd where/you/keep/your/projects
409
+ git clone git://github.com/bmizerany/sinatra.git
410
+ cd your_project
411
+ ln -s ../sinatra/
412
+
413
+ at the top of your sinatra.rb file
414
+
415
+ $:.unshift File.dirname(__FILE__) + '/sinatra/lib'
416
+ require 'sinatra'
417
+
418
+ get '/about' do
419
+ "I'm running on Version " + Sinatra::Version.combined
420
+ end
421
+
422
+
423
+
424
+
425
+
426
+
427
+
428
+
429
+
430
+