Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 478 lines (328 sloc) 10.978 kB
df800b5 Docs are started
Blake Mizerany authored
1 = Sinatra
2
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
3 Sinatra is a DSL for quickly creating web-applications in Ruby with minimal
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
4 effort:
df800b5 Docs are started
Blake Mizerany authored
5
6 # myapp.rb
7 require 'rubygems'
8 require 'sinatra'
9 get '/' do
10 'Hello world!'
11 end
12
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
13 Install the gem and run with:
df800b5 Docs are started
Blake Mizerany authored
14
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
15 sudo gem install sinatra
16 ruby myapp.rb
17
18 View at: http://localhost:4567
19
20 == Routes
21
22 In Sinatra, a route is an HTTP method paired with an URL matching pattern.
23 Each route is associated with a block:
df800b5 Docs are started
Blake Mizerany authored
24
25 get '/' do
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
26 .. show something ..
df800b5 Docs are started
Blake Mizerany authored
27 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
28
df800b5 Docs are started
Blake Mizerany authored
29 post '/' do
30 .. create something ..
31 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
32
df800b5 Docs are started
Blake Mizerany authored
33 put '/' do
34 .. update something ..
35 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
36
df800b5 Docs are started
Blake Mizerany authored
37 delete '/' do
38 .. annihilate something ..
39 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
40
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
41 Routes are matched in the order they are defined. The first route that
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
42 matches the request is invoked.
1776a80 Added Version and Docs
Blake Mizerany authored
43
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
44 Route patterns may include named parameters, accessible via the
45 <tt>params</tt> hash:
1776a80 Added Version and Docs
Blake Mizerany authored
46
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
47 get '/hello/:name' do
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
48 # matches "GET /foo" and "GET /bar"
49 # params[:name] is 'foo' or 'bar'
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
50 "Hello #{params[:name]}!"
1776a80 Added Version and Docs
Blake Mizerany authored
51 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
52
6569d1b @bdimcheff Added route block params in routing statements [#140]
bdimcheff authored
53 You can also access named parameters via block parameters:
54
55 get '/hello/:name' do |n|
56 "Hello #{n}!"
57 end
58
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
59 Route patterns may also include splat (or wildcard) parameters, accessible
60 via the <tt>params[:splat]</tt> array.
1776a80 Added Version and Docs
Blake Mizerany authored
61
9c85e99 @vic Specs, documentation and fixes for splat'n routes
vic authored
62 get '/say/*/to/*' do
63 # matches /say/hello/to/world
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
64 params[:splat] # => ["hello", "world"]
9c85e99 @vic Specs, documentation and fixes for splat'n routes
vic authored
65 end
66
67 get '/download/*.*' do
68 # matches /download/path/to/file.xml
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
69 params[:splat] # => ["path/to/file", "xml"]
1776a80 Added Version and Docs
Blake Mizerany authored
70 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
71
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
72 Route matching with Regular Expressions:
73
74 get %r{/hello/([\w]+)} do
75 "Hello, #{params[:captures].first}!"
76 end
77
6569d1b @bdimcheff Added route block params in routing statements [#140]
bdimcheff authored
78 Or with a block parameter:
79
80 get %r{/hello/([\w]+)} do |c|
81 "Hello, #{c}!"
82 end
83
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
84 Routes may include a variety of matching conditions, such as the user agent:
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
85
1776a80 Added Version and Docs
Blake Mizerany authored
86 get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
87 "You're using Songbird version #{params[:agent][0]}"
88 end
89
90 get '/foo' do
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
91 # Matches non-songbird browsers
1776a80 Added Version and Docs
Blake Mizerany authored
92 end
df800b5 Docs are started
Blake Mizerany authored
93
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
94 == Static Files
047edc6 update README with Static help
Blake Mizerany authored
95
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
96 Static files are served from the <tt>./public</tt> directory. You can specify
97 a different location by setting the <tt>:public</tt> option:
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
98
99 set :public, File.dirname(__FILE__) + '/static'
df800b5 Docs are started
Blake Mizerany authored
100
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
101 Note that the public directory name is not included in the URL. A file
85b4462 @rtomayko README: fix formatting on static file note
rtomayko authored
102 <tt>./public/css/style.css</tt> is made available as
103 <tt>http://example.com/css/style.css</tt>.
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
104
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
105 == Views / Templates
df800b5 Docs are started
Blake Mizerany authored
106
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
107 Templates are assumed to be located directly under the <tt>./views</tt>
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
108 directory. To use a different views directory:
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
109
110 set :views, File.dirname(__FILE__) + '/templates'
111
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
112 === Haml Templates
df800b5 Docs are started
Blake Mizerany authored
113
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
114 The haml gem/library is required to render HAML templates:
115
df800b5 Docs are started
Blake Mizerany authored
116 get '/' do
117 haml :index
118 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
119
120 Renders <tt>./views/index.haml</tt>.
121
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
122 === Erb Templates
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
123
124 get '/' do
125 erb :index
126 end
127
128 Renders <tt>./views/index.erb</tt>
129
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
130 === Builder Templates
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
131
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
132 The builder gem/library is required to render builder templates:
df800b5 Docs are started
Blake Mizerany authored
133
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
134 get '/' do
135 content_type 'application/xml', :charset => 'utf-8'
136 builder :index
137 end
138
139 Renders <tt>./views/index.builder</tt>.
140
141 === Sass Templates
142
143 The sass gem/library is required to render Sass templates:
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
144
4144ac1 @nmeans Added Sass information to documentation.
nmeans authored
145 get '/stylesheet.css' do
ccc19b0 @rtomayko content_type response helper with mime type lookup and parameter supp…
rtomayko authored
146 content_type 'text/css', :charset => 'utf-8'
4144ac1 @nmeans Added Sass information to documentation.
nmeans authored
147 sass :stylesheet
148 end
149
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
150 Renders <tt>./views/stylesheet.sass</tt>.
151
152 === Inline Templates
df800b5 Docs are started
Blake Mizerany authored
153
154 get '/' do
155 haml '%div.title Hello World'
156 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
157
158 Renders the inlined template string.
df800b5 Docs are started
Blake Mizerany authored
159
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
160 === Accessing Variables in Templates
df800b5 Docs are started
Blake Mizerany authored
161
5018264 @rtomayko Tidy up README a bit; link to @sinatra on Twitter
rtomayko authored
162 Templates are evaluated within the same context as route handlers. Instance
163 variables set in route handlers are direcly accessible by templates:
df800b5 Docs are started
Blake Mizerany authored
164
165 get '/:id' do
166 @foo = Foo.find(params[:id])
95aca76 @bleything fix documentation of variable interpolation into templates
bleything authored
167 haml '%h1= @foo.name'
df800b5 Docs are started
Blake Mizerany authored
168 end
169
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
170 Or, specify an explicit Hash of local variables:
df800b5 Docs are started
Blake Mizerany authored
171
172 get '/:id' do
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
173 foo = Foo.find(params[:id])
95aca76 @bleything fix documentation of variable interpolation into templates
bleything authored
174 haml '%h1= foo.name', :locals => { :foo => foo }
df800b5 Docs are started
Blake Mizerany authored
175 end
176
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
177 This is typically used when rendering templates as partials from within
178 other templates.
179
180 === In-file Templates
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
181
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
182 Templates may be defined at the end of the source file:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
183
eec7d21 @bmizerany In-file-templates are automaticly loaded for you.
bmizerany authored
184 require 'rubygems'
185 require 'sinatra'
186
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
187 get '/' do
188 haml :index
189 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
190
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
191 __END__
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
192
f71330e @bmizerany quick doc fix
bmizerany authored
193 @@ layout
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
194 %html
195 = yield
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
196
f71330e @bmizerany quick doc fix
bmizerany authored
197 @@ index
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
198 %div.title Hello world!!!!!
199
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
200 NOTE: In-file templates defined in the source file that requires sinatra
201 are automatically loaded. Call the <tt>use_in_file_templates!</tt>
5018264 @rtomayko Tidy up README a bit; link to @sinatra on Twitter
rtomayko authored
202 method explicitly if you have in-file templates in other source files.
eec7d21 @bmizerany In-file-templates are automaticly loaded for you.
bmizerany authored
203
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
204 === Named Templates
205
5018264 @rtomayko Tidy up README a bit; link to @sinatra on Twitter
rtomayko authored
206 Templates may also be defined using the top-level <tt>template</tt> method:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
207
208 template :layout do
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
209 "%html\n =yield\n"
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
210 end
211
212 template :index do
213 '%div.title Hello World!'
214 end
215
216 get '/' do
217 haml :index
218 end
219
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
220 If a template named "layout" exists, it will be used each time a template
221 is rendered. You can disable layouts by passing <tt>:layout => false</tt>.
578bbab @djanowski Updating README for :layout => true.
djanowski authored
222
223 get '/' do
224 haml :index, :layout => !request.xhr?
225 end
226
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
227 == Helpers
df800b5 Docs are started
Blake Mizerany authored
228
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
229 Use the top-level <tt>helpers</tt> method to define helper methods for use in
5018264 @rtomayko Tidy up README a bit; link to @sinatra on Twitter
rtomayko authored
230 route handlers and templates:
df800b5 Docs are started
Blake Mizerany authored
231
232 helpers do
233 def bar(name)
234 "#{name}bar"
235 end
236 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
237
1776a80 Added Version and Docs
Blake Mizerany authored
238 get '/:name' do
239 bar(params[:name])
240 end
df800b5 Docs are started
Blake Mizerany authored
241
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
242 == Filters
df800b5 Docs are started
Blake Mizerany authored
243
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
244 Before filters are evaluated before each request within the context of the
245 request and can modify the request and response. Instance variables set in
5018264 @rtomayko Tidy up README a bit; link to @sinatra on Twitter
rtomayko authored
246 filters are accessible by routes and templates:
1776a80 Added Version and Docs
Blake Mizerany authored
247
df800b5 Docs are started
Blake Mizerany authored
248 before do
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
249 @note = 'Hi!'
250 request.path_info = '/foo/bar/baz'
251 end
252
253 get '/foo/*' do
254 @note #=> 'Hi!'
255 params[:splat] #=> 'bar/baz'
df800b5 Docs are started
Blake Mizerany authored
256 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
257
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
258 == Halting
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
259
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
260 To immediately stop a request during a before filter or route use:
df800b5 Docs are started
Blake Mizerany authored
261
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
262 halt
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
263
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
264 You can also specify a body when halting ...
df800b5 Docs are started
Blake Mizerany authored
265
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
266 halt 'this will be the body'
df800b5 Docs are started
Blake Mizerany authored
267
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
268 Or set the status and body ...
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
269
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
270 halt 401, 'go away!'
df800b5 Docs are started
Blake Mizerany authored
271
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
272 == Passing
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
273
5018264 @rtomayko Tidy up README a bit; link to @sinatra on Twitter
rtomayko authored
274 A route can punt processing to the next matching route using <tt>pass</tt>:
df800b5 Docs are started
Blake Mizerany authored
275
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
276 get '/guess/:who' do
277 pass unless params[:who] == 'Frank'
278 "You got me!"
279 end
280
281 get '/guess/*' do
282 "You missed!"
283 end
284
285 The route block is immediately exited and control continues with the next
286 matching route. If no matching route is found, a 404 is returned.
df800b5 Docs are started
Blake Mizerany authored
287
13fc79d @rtomayko Remove support for source file reloading [#166]
rtomayko authored
288 == Configuration
1776a80 Added Version and Docs
Blake Mizerany authored
289
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
290 Run once, at startup, in any environment:
1776a80 Added Version and Docs
Blake Mizerany authored
291
292 configure do
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
293 ...
1776a80 Added Version and Docs
Blake Mizerany authored
294 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
295
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
296 Run only when the environment (RACK_ENV environment variable) is set to
13fc79d @rtomayko Remove support for source file reloading [#166]
rtomayko authored
297 <tt>:production</tt>:
1776a80 Added Version and Docs
Blake Mizerany authored
298
299 configure :production do
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
300 ...
1776a80 Added Version and Docs
Blake Mizerany authored
301 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
302
13fc79d @rtomayko Remove support for source file reloading [#166]
rtomayko authored
303 Run when the environment is set to either <tt>:production</tt> or
304 <tt>:test</tt>:
1776a80 Added Version and Docs
Blake Mizerany authored
305
306 configure :production, :test do
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
307 ...
1776a80 Added Version and Docs
Blake Mizerany authored
308 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
309
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
310 == Error handling
1776a80 Added Version and Docs
Blake Mizerany authored
311
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
312 Error handlers run within the same context as routes and before filters, which
313 means you get all the goodies it has to offer, like <tt>haml</tt>, <tt>erb</tt>,
314 <tt>halt</tt>, etc.
1776a80 Added Version and Docs
Blake Mizerany authored
315
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
316 === Not Found
1776a80 Added Version and Docs
Blake Mizerany authored
317
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
318 When a <tt>Sinatra::NotFound</tt> exception is raised, or the response's status
319 code is 404, the <tt>not_found</tt> handler is invoked:
1776a80 Added Version and Docs
Blake Mizerany authored
320
321 not_found do
322 'This is nowhere to be found'
323 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
324
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
325 === Error
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
326
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
327 The +error+ handler is invoked any time an exception is raised from a route
328 block or before filter. The exception object can be obtained from the
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
329 <tt>sinatra.error</tt> Rack variable:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
330
1776a80 Added Version and Docs
Blake Mizerany authored
331 error do
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
332 'Sorry there was a nasty error - ' + env['sinatra.error'].name
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
333 end
334
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
335 Custom errors:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
336
337 error MyCustomError do
e7e0e55 @rtomayko Minor docfixes in README.rdoc
rtomayko authored
338 'So what happened was...' + request.env['sinatra.error'].message
1776a80 Added Version and Docs
Blake Mizerany authored
339 end
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
340
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
341 Then, if this happens:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
342
343 get '/' do
344 raise MyCustomError, 'something bad'
345 end
346
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
347 You get this:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
348
349 So what happened was... something bad
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
350
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
351 Sinatra installs special <tt>not_found</tt> and <tt>error</tt> handlers when
352 running under the development environment.
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
353
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
354 == Mime types
355
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
356 When using <tt>send_file</tt> or static files you may have mime types Sinatra
357 doesn't understand. Use +mime+ to register them by file extension:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
358
359 mime :foo, 'text/foo'
1776a80 Added Version and Docs
Blake Mizerany authored
360
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
361 == Rack Middleware
bda21f1 @rtomayko add doc on using Rack middleware to README
rtomayko authored
362
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
363 Sinatra rides on Rack[http://rack.rubyforge.org/], a minimal standard
364 interface for Ruby web frameworks. One of Rack's most interesting capabilities
365 for application developers is support for "middleware" -- components that sit
366 between the server and your application monitoring and/or manipulating the
367 HTTP request/response to provide various types of common functionality.
bda21f1 @rtomayko add doc on using Rack middleware to README
rtomayko authored
368
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
369 Sinatra makes building Rack middleware pipelines a cinch via a top-level
370 +use+ method:
bda21f1 @rtomayko add doc on using Rack middleware to README
rtomayko authored
371
372 require 'sinatra'
373 require 'my_custom_middleware'
374
375 use Rack::Lint
376 use MyCustomMiddleware
377
378 get '/hello' do
379 'Hello World'
380 end
381
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
382 The semantics of +use+ are identical to those defined for the
383 Rack::Builder[http://rack.rubyforge.org/doc/classes/Rack/Builder.html] DSL
384 (most frequently used from rackup files). For example, the +use+ method
385 accepts multiple/variable args as well as blocks:
bda21f1 @rtomayko add doc on using Rack middleware to README
rtomayko authored
386
387 use Rack::Auth::Basic do |username, password|
388 username == 'admin' && password == 'secret'
389 end
390
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
391 Rack is distributed with a variety of standard middleware for logging,
392 debugging, URL routing, authentication, and session handling. Sinatra uses
393 many of of these components automatically based on configuration so you
394 typically don't have to +use+ them explicitly.
bda21f1 @rtomayko add doc on using Rack middleware to README
rtomayko authored
395
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
396 == Testing
1776a80 Added Version and Docs
Blake Mizerany authored
397
2f377e2 @rtomayko Trim down Testing section in the README; link to doc site instead
rtomayko authored
398 The Sinatra::Test mixin and Sinatra::TestHarness class include a variety of
399 helper methods for testing your Sinatra app:
c00a25e @rtomayko Test framework refactoring
rtomayko authored
400
7cfe04a @jcrosby Fix for test/unit and test/spec docs
jcrosby authored
401 require 'my_sinatra_app'
2f377e2 @rtomayko Trim down Testing section in the README; link to doc site instead
rtomayko authored
402 require 'test/unit'
403 require 'sinatra/test'
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
404
1776a80 Added Version and Docs
Blake Mizerany authored
405 class MyAppTest < Test::Unit::TestCase
2f377e2 @rtomayko Trim down Testing section in the README; link to doc site instead
rtomayko authored
406 include Sinatra::Test
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
407
2f377e2 @rtomayko Trim down Testing section in the README; link to doc site instead
rtomayko authored
408 def test_my_default
c00a25e @rtomayko Test framework refactoring
rtomayko authored
409 get '/'
2f377e2 @rtomayko Trim down Testing section in the README; link to doc site instead
rtomayko authored
410 assert_equal 'Hello World!', @response.body
1776a80 Added Version and Docs
Blake Mizerany authored
411 end
10c90d5 @sr document testing with rspec
sr authored
412
2f377e2 @rtomayko Trim down Testing section in the README; link to doc site instead
rtomayko authored
413 def test_with_params
414 get '/meet', {:name => 'Frank'}
415 assert_equal 'Hello Frank!', @response.body
10c90d5 @sr document testing with rspec
sr authored
416 end
417
2f377e2 @rtomayko Trim down Testing section in the README; link to doc site instead
rtomayko authored
418 def test_with_rack_env
c2940f6 @cypher Fix test_with_rack_env example in README
cypher authored
419 get '/', {}, :agent => 'Songbird'
2f377e2 @rtomayko Trim down Testing section in the README; link to doc site instead
rtomayko authored
420 assert_equal "You're using Songbird!", @response.body
1fb5b99 @dylanegan Bacon support
dylanegan authored
421 end
422 end
423
8446c5a @rtomayko Fix broken link to testing doc in README
rtomayko authored
424 See http://www.sinatrarb.com/testing.html for more on Sinatra::Test and using it
2f377e2 @rtomayko Trim down Testing section in the README; link to doc site instead
rtomayko authored
425 with other test frameworks such as RSpec, Bacon, and test/spec.
1776a80 Added Version and Docs
Blake Mizerany authored
426
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
427 == Command line
1776a80 Added Version and Docs
Blake Mizerany authored
428
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
429 Sinatra applications can be run directly:
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
430
f29486b @karmi Note "-s" (server) command line option in README
karmi authored
431 ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-s HANDLER]
1776a80 Added Version and Docs
Blake Mizerany authored
432
433 Options are:
434
435 -h # help
436 -p # set the port (default is 4567)
437 -e # set the environment (default is development)
f29486b @karmi Note "-s" (server) command line option in README
karmi authored
438 -s # specify rack server/handler (default is thin)
e7e0e55 @rtomayko Minor docfixes in README.rdoc
rtomayko authored
439 -x # turn on the mutex lock (default is off)
1776a80 Added Version and Docs
Blake Mizerany authored
440
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
441 == The Bleeding Edge
6326809 @cypher Add a subsection about tools needed for Sinatra to the Contributing s…
cypher authored
442
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
443 If you would like to use Sinatra's latest bleeding code, create a local
444 clone and run your app with the <tt>sinatra/lib</tt> directory on the
445 <tt>LOAD_PATH</tt>:
6326809 @cypher Add a subsection about tools needed for Sinatra to the Contributing s…
cypher authored
446
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
447 cd myapp
ba92616 @bmizerany Updated README to point to github.com/sinatra/sinatra.git
bmizerany authored
448 git clone git://github.com/sinatra/sinatra.git
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
449 ruby -Isinatra/lib myapp.rb
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
450
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
451 Alternatively, you can add the <tt>sinatra/lib<tt> directory to the
452 <tt>LOAD_PATH</tt> in your application:
1776a80 Added Version and Docs
Blake Mizerany authored
453
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
454 $LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
455 require 'rubygems'
1776a80 Added Version and Docs
Blake Mizerany authored
456 require 'sinatra'
457
458 get '/about' do
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
459 "I'm running version " + Sinatra::VERSION
1776a80 Added Version and Docs
Blake Mizerany authored
460 end
e6c5471 @cypher Add a community section with info about the mailing list and irc channel
cypher authored
461
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
462 To update the Sinatra sources in the future:
d8fec16 @cypher Add subsection about contributing a patch as well as a link to the is…
cypher authored
463
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
464 cd myproject/sinatra
465 git pull
e6c5471 @cypher Add a community section with info about the mailing list and irc channel
cypher authored
466
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
467 == More
e6c5471 @cypher Add a community section with info about the mailing list and irc channel
cypher authored
468
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
469 * {Project Website}[http://sinatra.github.com/] - Additional documentation,
470 news, and links to other resources.
0044683 @Aupajo Fixed broken link in README.
Aupajo authored
471 * {Contributing}[http://sinatra.github.com/contributing.html] - Find a bug? Need
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
472 help? Have a patch?
473 * {Lighthouse}[http://sinatra.lighthouseapp.com] - Issue tracking and release
474 planning.
5018264 @rtomayko Tidy up README a bit; link to @sinatra on Twitter
rtomayko authored
475 * {Twitter}[http://twitter.com/sinatra]
4298a77 @rtomayko Tweak README formatting; move community/contributing to website
rtomayko authored
476 * {Mailing List}[http://groups.google.com/group/sinatrarb]
477 * {IRC: #sinatra}[irc://chat.freenode.net/#sinatra] on http://freenode.net
Something went wrong with that request. Please try again.