rtomayko-sinatra 0.3.0

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.
Files changed (49) hide show
  1. data/ChangeLog +64 -0
  2. data/LICENSE +22 -0
  3. data/README.rdoc +533 -0
  4. data/Rakefile +111 -0
  5. data/images/404.png +0 -0
  6. data/images/500.png +0 -0
  7. data/lib/sinatra/rack/handler/mongrel.rb +85 -0
  8. data/lib/sinatra/test/methods.rb +76 -0
  9. data/lib/sinatra/test/rspec.rb +10 -0
  10. data/lib/sinatra/test/spec.rb +10 -0
  11. data/lib/sinatra/test/unit.rb +13 -0
  12. data/lib/sinatra.rb +1477 -0
  13. data/sinatra.gemspec +78 -0
  14. data/test/app_test.rb +299 -0
  15. data/test/application_test.rb +318 -0
  16. data/test/builder_test.rb +101 -0
  17. data/test/custom_error_test.rb +62 -0
  18. data/test/erb_test.rb +136 -0
  19. data/test/event_context_test.rb +15 -0
  20. data/test/events_test.rb +47 -0
  21. data/test/filter_test.rb +30 -0
  22. data/test/haml_test.rb +233 -0
  23. data/test/helper.rb +7 -0
  24. data/test/mapped_error_test.rb +72 -0
  25. data/test/pipeline_test.rb +66 -0
  26. data/test/public/foo.xml +1 -0
  27. data/test/sass_test.rb +57 -0
  28. data/test/sessions_test.rb +39 -0
  29. data/test/streaming_test.rb +118 -0
  30. data/test/sym_params_test.rb +19 -0
  31. data/test/template_test.rb +30 -0
  32. data/test/use_in_file_templates_test.rb +47 -0
  33. data/test/views/foo.builder +1 -0
  34. data/test/views/foo.erb +1 -0
  35. data/test/views/foo.haml +1 -0
  36. data/test/views/foo.sass +2 -0
  37. data/test/views/foo_layout.erb +2 -0
  38. data/test/views/foo_layout.haml +2 -0
  39. data/test/views/layout_test/foo.builder +1 -0
  40. data/test/views/layout_test/foo.erb +1 -0
  41. data/test/views/layout_test/foo.haml +1 -0
  42. data/test/views/layout_test/foo.sass +2 -0
  43. data/test/views/layout_test/layout.builder +3 -0
  44. data/test/views/layout_test/layout.erb +1 -0
  45. data/test/views/layout_test/layout.haml +1 -0
  46. data/test/views/layout_test/layout.sass +2 -0
  47. data/test/views/no_layout/no_layout.builder +1 -0
  48. data/test/views/no_layout/no_layout.haml +1 -0
  49. metadata +130 -0
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 ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2007 Blake Mizerany
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,533 @@
1
+ = Sinatra
2
+
3
+ Sinatra is a DSL for quickly creating web-applications in Ruby with minimal
4
+ effort.
5
+
6
+ == Sample App
7
+
8
+ # myapp.rb
9
+ require 'rubygems'
10
+ require 'sinatra'
11
+ get '/' do
12
+ 'Hello world!'
13
+ end
14
+
15
+ Run with <tt>ruby myapp.rb</tt> and view at <tt>http://localhost:4567</tt>
16
+
17
+ == HTTP Methods
18
+
19
+ get '/' do
20
+ .. show things ..
21
+ end
22
+
23
+ post '/' do
24
+ .. create something ..
25
+ end
26
+
27
+ put '/' do
28
+ .. update something ..
29
+ end
30
+
31
+ delete '/' do
32
+ .. annihilate something ..
33
+ end
34
+
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
+ == Routes
44
+
45
+ Routes are matched based on the order of declaration. The first route that
46
+ matches the request is invoked.
47
+
48
+ Simple:
49
+
50
+ get '/hi' do
51
+ ...
52
+ end
53
+
54
+ Named parameters:
55
+
56
+ get '/:name' do
57
+ # matches /sinatra and the like and sets params[:name]
58
+ end
59
+
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"]
70
+ end
71
+
72
+ User agent matching:
73
+
74
+ get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
75
+ "You're using Songbird version #{params[:agent][0]}"
76
+ end
77
+
78
+ get '/foo' do
79
+ # matches non-songbird browsers
80
+ end
81
+
82
+ = Static files
83
+
84
+ Put all of your static content in the ./public directory
85
+
86
+ root
87
+ \ public
88
+
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.
92
+
93
+ == Views
94
+
95
+ Views are searched for in a "views" directory in the same location as
96
+ your main application.
97
+
98
+ === Haml Templates
99
+
100
+ get '/' do
101
+ haml :index
102
+ end
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
117
+
118
+ === Sass
119
+
120
+ get '/stylesheet.css' do
121
+ content_type 'text/css', :charset => 'utf-8'
122
+ sass :stylesheet
123
+ end
124
+
125
+ Renders <tt>./views/stylesheet.sass</tt>.
126
+
127
+ === Inline Templates
128
+
129
+ get '/' do
130
+ haml '%div.title Hello World'
131
+ end
132
+
133
+ Renders the inlined template string.
134
+
135
+ === Accessing Variables
136
+
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:
140
+
141
+ get '/:id' do
142
+ @foo = Foo.find(params[:id])
143
+ haml '%h1== @foo.name'
144
+ end
145
+
146
+ Or, specify an explicit Hash of local variables:
147
+
148
+ get '/:id' do
149
+ foo = Foo.find(params[:id])
150
+ haml '%h1== foo.name', :locals => { :foo => foo }
151
+ end
152
+
153
+ This is typically used when rendering templates as partials from within
154
+ other templates.
155
+
156
+ === In-file Templates
157
+
158
+ Templates may be defined at the end of the source file:
159
+
160
+ get '/' do
161
+ haml :index
162
+ end
163
+
164
+ use_in_file_templates!
165
+
166
+ __END__
167
+
168
+ @@ layout
169
+ X
170
+ = yield
171
+ X
172
+
173
+ @@ index
174
+ %div.title Hello world!!!!!
175
+
176
+ It's also possible to define named templates using the top-level template
177
+ method:
178
+
179
+ template :layout do
180
+ "X\n=yield\nX"
181
+ end
182
+
183
+ template :index do
184
+ '%div.title Hello World!'
185
+ end
186
+
187
+ get '/' do
188
+ haml :index
189
+ end
190
+
191
+ == Helpers
192
+
193
+ The top-level <tt>helpers</tt> method takes a block and extends all
194
+ EventContext instances with the methods defined:
195
+
196
+ helpers do
197
+ def bar(name)
198
+ "#{name}bar"
199
+ end
200
+ end
201
+
202
+ get '/:name' do
203
+ bar(params[:name])
204
+ end
205
+
206
+ == Filters
207
+
208
+ These are run in Sinatra::EventContext before every event.
209
+
210
+ before do
211
+ .. this code will run before each event ..
212
+ end
213
+
214
+ == Halt!
215
+
216
+ To immediately stop a request during a before filter or event use:
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]
228
+
229
+ Set the body to a simple string
230
+
231
+ throw :halt, 'this will be the body'
232
+
233
+ Set status then the body
234
+
235
+ throw :halt, [401, 'go away!']
236
+
237
+ Set the status then call a helper method with params from local scope
238
+
239
+ throw :halt, [401, [:helper_method, foo, bar]]
240
+
241
+ Run a proc inside the Sinatra::EventContext instance and set the body to the
242
+ result
243
+
244
+ throw :halt, lambda { puts 'In a proc!'; 'I just wrote to $stdout!' }
245
+
246
+ Create you own to_result
247
+
248
+ class MyResultObject
249
+ def to_result(event_context, *args)
250
+ event_context.body = 'This will be the body!
251
+ end
252
+ end
253
+
254
+ get '/' do
255
+ throw :halt, MyResultObject.new
256
+ end
257
+
258
+ Get the gist? If you want more fun with this then checkout <tt>to_result</tt>
259
+ on Array, Symbol, Fixnum, NilClass.
260
+
261
+ == Configuration and Reloading
262
+
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.
267
+
268
+ All environments:
269
+
270
+ configure do
271
+
272
+ end
273
+
274
+ Production
275
+
276
+ configure :production do
277
+
278
+ end
279
+
280
+ Two at a time:
281
+
282
+ configure :production, :test do
283
+
284
+ end
285
+
286
+ This is also really nifty for error handling.
287
+
288
+ = Error handling
289
+
290
+ == Not Found
291
+
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.)
294
+
295
+ Whenever NotFound is raised this will be called
296
+
297
+ not_found do
298
+ 'This is nowhere to be found'
299
+ end
300
+
301
+ == Error
302
+
303
+ By default +error+ will catch Sinatra::ServerError
304
+
305
+ Sinatra will pass you the error via the 'sinatra.error' in request.env
306
+
307
+ error do
308
+ 'Sorry there was a nasty error - ' + request.env['sinatra.error'].name
309
+ end
310
+
311
+ Custom error mapping:
312
+
313
+ error MyCustomError do
314
+ 'So what happened was...' + request.env['sinatra.error'].message
315
+ end
316
+
317
+ then if this happens:
318
+
319
+ get '/' do
320
+ raise MyCustomError, 'something bad'
321
+ end
322
+
323
+ you gets this:
324
+
325
+ So what happened was... something bad
326
+
327
+ one guess what this does ;)
328
+
329
+ not_found do
330
+ 'I have no clue what you're looking for'
331
+ end
332
+
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:
336
+
337
+ configure :production do
338
+
339
+ not_found do
340
+ "We're so sorry, but we don't what this is"
341
+ end
342
+
343
+ error do
344
+ "Something really nasty happened. We're on it!"
345
+ end
346
+
347
+ end
348
+
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.
353
+
354
+ mime :foo, 'text/foo'
355
+
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
409
+
410
+ === Test/Unit
411
+
412
+ require 'my_sinatra_app'
413
+ require 'sinatra/test/unit'
414
+
415
+ class MyAppTest < Test::Unit::TestCase
416
+
417
+ def test_my_default
418
+ get_it '/'
419
+ assert_equal 'My Default Page!', @response.body
420
+ end
421
+
422
+ def test_with_agent
423
+ get_it '/', :agent => 'Songbird'
424
+ assert_equal 'You're in Songbird!', @response.body
425
+ end
426
+
427
+ ...
428
+
429
+ end
430
+
431
+ === Specs
432
+
433
+ require 'my_sinatra_app'
434
+ require 'sinatra/test/spec'
435
+
436
+ context 'My app'
437
+
438
+ should "show a default page" do
439
+ get_it '/'
440
+ should.be.ok
441
+ body.should.equal 'My Default Page!'
442
+ end
443
+ ...
444
+
445
+ end
446
+
447
+ === Test Helpers
448
+
449
+ See Sinatra::Test::Methods
450
+
451
+ == Command line
452
+
453
+ Run your sinatra file like:
454
+
455
+ ruby myapp.rb [options]
456
+
457
+ Options are:
458
+
459
+ -h # help
460
+ -p # set the port (default is 4567)
461
+ -e # set the environment (default is development)
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/
476
+
477
+ === First Time: Cloning The Sinatra Repo
478
+
479
+ cd where/you/keep/your/projects
480
+ git clone git://github.com/bmizerany/sinatra.git
481
+ cd sinatra
482
+ cd path/to/your_project
483
+ ln -s ../sinatra/
484
+
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:
493
+
494
+ $:.unshift File.dirname(__FILE__) + '/sinatra/lib'
495
+ require 'sinatra'
496
+
497
+ get '/about' do
498
+ "I'm running on Version " + Sinatra::VERSION
499
+ end
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
513
+
514
+ http://sinatra.lighthouseapp.com/
515
+
516
+ == Community
517
+
518
+ === Mailing List
519
+
520
+ http://groups.google.com/group/sinatrarb
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.
525
+
526
+ === IRC Channel
527
+
528
+ You can find us on the Freenode network in the channel #sinatra
529
+ (irc://chat.freenode.net/#sinatra)
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.