Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 536 lines (371 sloc) 12.098 kb
df800b5 Docs are started
Blake Mizerany authored
1 = Sinatra
2
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
3 Sinatra is a DSL for quickly creating web-applications in Ruby with minimal
17cb177 Markus Prinz 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 Ryan Tomayko 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 Ryan Tomayko 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 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
26 .. show something ..
df800b5 Docs are started
Blake Mizerany authored
27 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
28
df800b5 Docs are started
Blake Mizerany authored
29 post '/' do
30 .. create something ..
31 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
32
df800b5 Docs are started
Blake Mizerany authored
33 put '/' do
34 .. update something ..
35 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
36
df800b5 Docs are started
Blake Mizerany authored
37 delete '/' do
38 .. annihilate something ..
39 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
40
4298a77 Ryan Tomayko 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 Ryan Tomayko grammar/formatting pass over README
rtomayko authored
42 matches the request is invoked.
1776a80 Added Version and Docs
Blake Mizerany authored
43
17cb177 Markus Prinz 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 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
47 get '/hello/:name' do
a734cf3 Ryan Tomayko 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 Markus Prinz 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 Ryan Tomayko grammar/formatting pass over README
rtomayko authored
52
6569d1b Brandon Dimcheff 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 Markus Prinz 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 Victor Hugo Borja Specs, documentation and fixes for splat'n routes
vic authored
62 get '/say/*/to/*' do
63 # matches /say/hello/to/world
a734cf3 Ryan Tomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
64 params[:splat] # => ["hello", "world"]
9c85e99 Victor Hugo Borja 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 Ryan Tomayko 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 Ryan Tomayko grammar/formatting pass over README
rtomayko authored
71
17cb177 Markus Prinz 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 Brandon Dimcheff 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 Markus Prinz 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 Ryan Tomayko 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 Ryan Tomayko 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 Ryan Tomayko 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 Markus Prinz 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 Ryan Tomayko 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 Ryan Tomayko 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 Ryan Tomayko 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 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
104
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
105 == Views / Templates
df800b5 Docs are started
Blake Mizerany authored
106
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
107 Templates are assumed to be located directly under the <tt>./views</tt>
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
108 directory. To use a different views directory:
a734cf3 Ryan Tomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
109
110 set :views, File.dirname(__FILE__) + '/templates'
111
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
112 === Haml Templates
df800b5 Docs are started
Blake Mizerany authored
113
17cb177 Markus Prinz 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 Ryan Tomayko grammar/formatting pass over README
rtomayko authored
119
120 Renders <tt>./views/index.haml</tt>.
121
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
122 === Erb Templates
f44fb6a Ryan Tomayko 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 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
130 === Builder Templates
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
131
17cb177 Markus Prinz 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 Markus Prinz 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 Ryan Tomayko grammar/formatting pass over README
rtomayko authored
144
4144ac1 Nickolas Means Added Sass information to documentation.
nmeans authored
145 get '/stylesheet.css' do
ccc19b0 Ryan Tomayko content_type response helper with mime type lookup and parameter support...
rtomayko authored
146 content_type 'text/css', :charset => 'utf-8'
4144ac1 Nickolas Means Added Sass information to documentation.
nmeans authored
147 sass :stylesheet
148 end
149
f44fb6a Ryan Tomayko 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 Ryan Tomayko grammar/formatting pass over README
rtomayko authored
157
158 Renders the inlined template string.
df800b5 Docs are started
Blake Mizerany authored
159
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
160 === Accessing Variables in Templates
df800b5 Docs are started
Blake Mizerany authored
161
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
162 Templates are evaluated within the same context as the route blocks. Instance
163 variables set in route blocks are available in templates:
df800b5 Docs are started
Blake Mizerany authored
164
165 get '/:id' do
166 @foo = Foo.find(params[:id])
95aca76 Ben 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 Ryan Tomayko 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 Ryan Tomayko grammar/formatting pass over README
rtomayko authored
173 foo = Foo.find(params[:id])
95aca76 Ben 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 Ryan Tomayko 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 Blake Mizerany updated README with helpful tidbits
bmizerany authored
181
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
182 Templates may be defined at the end of the source file:
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
183
eec7d21 Blake Mizerany In-file-templates are automaticly loaded for you.
bmizerany authored
184 require 'rubygems'
185 require 'sinatra'
186
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
187 get '/' do
188 haml :index
189 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
190
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
191 __END__
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
192
f71330e Blake Mizerany quick doc fix
bmizerany authored
193 @@ layout
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
194 %html
195 = yield
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
196
f71330e Blake Mizerany quick doc fix
bmizerany authored
197 @@ index
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
198 %div.title Hello world!!!!!
199
4298a77 Ryan Tomayko 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>
202 method explicitly if you have in-file templates in another source file.
eec7d21 Blake Mizerany In-file-templates are automaticly loaded for you.
bmizerany authored
203
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
204 === Named Templates
205
206 It's possible to define named templates using the top-level <tt>template</tt>
480fbfa Ryan Tomayko minor formatting tweaks to cypher's README updates
rtomayko authored
207 method:
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
208
209 template :layout do
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
210 "%html\n =yield\n"
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
211 end
212
213 template :index do
214 '%div.title Hello World!'
215 end
216
217 get '/' do
218 haml :index
219 end
220
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
221 If a template named "layout" exists, it will be used each time a template
222 is rendered. You can disable layouts by passing <tt>:layout => false</tt>.
578bbab Damian Janowski Updating README for :layout => true.
djanowski authored
223
224 get '/' do
225 haml :index, :layout => !request.xhr?
226 end
227
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
228 == Helpers
df800b5 Docs are started
Blake Mizerany authored
229
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
230 Use the top-level <tt>helpers</tt> method to define helper methods for use in
231 route blocks and templates:
df800b5 Docs are started
Blake Mizerany authored
232
233 helpers do
234 def bar(name)
235 "#{name}bar"
236 end
237 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
238
1776a80 Added Version and Docs
Blake Mizerany authored
239 get '/:name' do
240 bar(params[:name])
241 end
df800b5 Docs are started
Blake Mizerany authored
242
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
243 == Filters
df800b5 Docs are started
Blake Mizerany authored
244
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
245 Before filters are evaluated before each request within the context of the
246 request and can modify the request and response. Instance variables set in
247 filters are accessible by routes and templates.
1776a80 Added Version and Docs
Blake Mizerany authored
248
df800b5 Docs are started
Blake Mizerany authored
249 before do
a734cf3 Ryan Tomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
250 @note = 'Hi!'
251 request.path_info = '/foo/bar/baz'
252 end
253
254 get '/foo/*' do
255 @note #=> 'Hi!'
256 params[:splat] #=> 'bar/baz'
df800b5 Docs are started
Blake Mizerany authored
257 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
258
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
259 == Halting
a734cf3 Ryan Tomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
260
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
261 To immediately stop a request during a before filter or route use:
df800b5 Docs are started
Blake Mizerany authored
262
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
263 halt
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
264
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
265 You can also specify a body when halting ...
df800b5 Docs are started
Blake Mizerany authored
266
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
267 halt 'this will be the body'
df800b5 Docs are started
Blake Mizerany authored
268
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
269 Or set the status and body ...
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
270
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
271 halt 401, 'go away!'
df800b5 Docs are started
Blake Mizerany authored
272
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
273 == Passing
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
274
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
275 A route can punt processing to the next matching route using the <tt>pass</tt>
276 statement:
df800b5 Docs are started
Blake Mizerany authored
277
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
278 get '/guess/:who' do
279 pass unless params[:who] == 'Frank'
280 "You got me!"
281 end
282
283 get '/guess/*' do
284 "You missed!"
285 end
286
287 The route block is immediately exited and control continues with the next
288 matching route. If no matching route is found, a 404 is returned.
df800b5 Docs are started
Blake Mizerany authored
289
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
290 == Configuration and Reloading
1776a80 Added Version and Docs
Blake Mizerany authored
291
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
292 Sinatra supports multiple environments and reloading. Reloading happens
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
293 before each request when running under the <tt>:development</tt>
294 environment. Wrap your configurations (e.g., database connections, constants,
295 etc.) in <tt>configure</tt> blocks to protect them from reloading or to
296 target specific environments.
1776a80 Added Version and Docs
Blake Mizerany authored
297
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
298 Run once, at startup, in any environment:
1776a80 Added Version and Docs
Blake Mizerany authored
299
300 configure do
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
301 ...
1776a80 Added Version and Docs
Blake Mizerany authored
302 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
303
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
304 Run only when the environment (RACK_ENV environment variable) is set to
305 <tt>:production</tt>.
1776a80 Added Version and Docs
Blake Mizerany authored
306
307 configure :production do
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
308 ...
1776a80 Added Version and Docs
Blake Mizerany authored
309 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
310
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
311 Run when the environment (RACK_ENV environment variable) is set to
312 either <tt>:production</tt> or <tt>:test</tt>.
1776a80 Added Version and Docs
Blake Mizerany authored
313
314 configure :production, :test do
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
315 ...
1776a80 Added Version and Docs
Blake Mizerany authored
316 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
317
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
318 == Error handling
1776a80 Added Version and Docs
Blake Mizerany authored
319
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
320 Error handlers run within the same context as routes and before filters, which
321 means you get all the goodies it has to offer, like <tt>haml</tt>, <tt>erb</tt>,
322 <tt>halt</tt>, etc.
1776a80 Added Version and Docs
Blake Mizerany authored
323
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
324 === Not Found
1776a80 Added Version and Docs
Blake Mizerany authored
325
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
326 When a <tt>Sinatra::NotFound</tt> exception is raised, or the response's status
327 code is 404, the <tt>not_found</tt> handler is invoked:
1776a80 Added Version and Docs
Blake Mizerany authored
328
329 not_found do
330 'This is nowhere to be found'
331 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
332
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
333 === Error
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
334
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
335 The +error+ handler is invoked any time an exception is raised from a route
336 block or before filter. The exception object can be obtained from the
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
337 <tt>sinatra.error</tt> Rack variable:
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
338
1776a80 Added Version and Docs
Blake Mizerany authored
339 error do
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
340 'Sorry there was a nasty error - ' + env['sinatra.error'].name
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
341 end
342
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
343 Custom errors:
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
344
345 error MyCustomError do
e7e0e55 Ryan Tomayko Minor docfixes in README.rdoc
rtomayko authored
346 'So what happened was...' + request.env['sinatra.error'].message
1776a80 Added Version and Docs
Blake Mizerany authored
347 end
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
348
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
349 Then, if this happens:
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
350
351 get '/' do
352 raise MyCustomError, 'something bad'
353 end
354
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
355 You get this:
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
356
357 So what happened was... something bad
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
358
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
359 Sinatra installs special <tt>not_found</tt> and <tt>error</tt> handlers when
360 running under the development environment.
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
361
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
362 == Mime types
363
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
364 When using <tt>send_file</tt> or static files you may have mime types Sinatra
365 doesn't understand. Use +mime+ to register them by file extension:
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
366
367 mime :foo, 'text/foo'
1776a80 Added Version and Docs
Blake Mizerany authored
368
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
369 == Rack Middleware
bda21f1 Ryan Tomayko add doc on using Rack middleware to README
rtomayko authored
370
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
371 Sinatra rides on Rack[http://rack.rubyforge.org/], a minimal standard
372 interface for Ruby web frameworks. One of Rack's most interesting capabilities
373 for application developers is support for "middleware" -- components that sit
374 between the server and your application monitoring and/or manipulating the
375 HTTP request/response to provide various types of common functionality.
bda21f1 Ryan Tomayko add doc on using Rack middleware to README
rtomayko authored
376
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
377 Sinatra makes building Rack middleware pipelines a cinch via a top-level
378 +use+ method:
bda21f1 Ryan Tomayko add doc on using Rack middleware to README
rtomayko authored
379
380 require 'sinatra'
381 require 'my_custom_middleware'
382
383 use Rack::Lint
384 use MyCustomMiddleware
385
386 get '/hello' do
387 'Hello World'
388 end
389
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
390 The semantics of +use+ are identical to those defined for the
391 Rack::Builder[http://rack.rubyforge.org/doc/classes/Rack/Builder.html] DSL
392 (most frequently used from rackup files). For example, the +use+ method
393 accepts multiple/variable args as well as blocks:
bda21f1 Ryan Tomayko add doc on using Rack middleware to README
rtomayko authored
394
395 use Rack::Auth::Basic do |username, password|
396 username == 'admin' && password == 'secret'
397 end
398
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
399 Rack is distributed with a variety of standard middleware for logging,
400 debugging, URL routing, authentication, and session handling. Sinatra uses
401 many of of these components automatically based on configuration so you
402 typically don't have to +use+ them explicitly.
bda21f1 Ryan Tomayko add doc on using Rack middleware to README
rtomayko authored
403
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
404 == Testing
1776a80 Added Version and Docs
Blake Mizerany authored
405
c00a25e Ryan Tomayko Test framework refactoring
rtomayko authored
406 The Sinatra::Test module includes a variety of helper methods for testing
407 your Sinatra app. Sinatra includes support for Test::Unit, test-spec, RSpec,
408 and Bacon through separate source files.
409
410 === Test::Unit
1776a80 Added Version and Docs
Blake Mizerany authored
411
7cfe04a Jon Crosby Fix for test/unit and test/spec docs
jcrosby authored
412 require 'sinatra'
1776a80 Added Version and Docs
Blake Mizerany authored
413 require 'sinatra/test/unit'
7cfe04a Jon Crosby Fix for test/unit and test/spec docs
jcrosby authored
414 require 'my_sinatra_app'
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
415
1776a80 Added Version and Docs
Blake Mizerany authored
416 class MyAppTest < Test::Unit::TestCase
417 def test_my_default
c00a25e Ryan Tomayko Test framework refactoring
rtomayko authored
418 get '/'
1776a80 Added Version and Docs
Blake Mizerany authored
419 assert_equal 'My Default Page!', @response.body
420 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
421
1776a80 Added Version and Docs
Blake Mizerany authored
422 def test_with_agent
c00a25e Ryan Tomayko Test framework refactoring
rtomayko authored
423 get '/', :agent => 'Songbird'
1776a80 Added Version and Docs
Blake Mizerany authored
424 assert_equal 'You're in Songbird!', @response.body
425 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
426
1776a80 Added Version and Docs
Blake Mizerany authored
427 ...
428 end
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
429
c00a25e Ryan Tomayko Test framework refactoring
rtomayko authored
430 === Test::Spec
431
432 Install the test-spec gem and require <tt>'sinatra/test/spec'</tt> before
433 your app:
1776a80 Added Version and Docs
Blake Mizerany authored
434
7cfe04a Jon Crosby Fix for test/unit and test/spec docs
jcrosby authored
435 require 'sinatra'
1776a80 Added Version and Docs
Blake Mizerany authored
436 require 'sinatra/test/spec'
7cfe04a Jon Crosby Fix for test/unit and test/spec docs
jcrosby authored
437 require 'my_sinatra_app'
1776a80 Added Version and Docs
Blake Mizerany authored
438
7cfe04a Jon Crosby Fix for test/unit and test/spec docs
jcrosby authored
439 describe 'My app' do
440 it "should show a default page" do
c00a25e Ryan Tomayko Test framework refactoring
rtomayko authored
441 get '/'
1776a80 Added Version and Docs
Blake Mizerany authored
442 should.be.ok
83cba9c Blake Mizerany updated README with helpful tidbits
bmizerany authored
443 body.should.equal 'My Default Page!'
1776a80 Added Version and Docs
Blake Mizerany authored
444 end
10c90d5 Simon Rozet document testing with rspec
sr authored
445
446 ...
447 end
448
449 === RSpec
450
c00a25e Ryan Tomayko Test framework refactoring
rtomayko authored
451 Install the rspec gem and require <tt>'sinatra/test/rspec'</tt> before
452 your app:
453
10c90d5 Simon Rozet document testing with rspec
sr authored
454 require 'sinatra'
455 require 'sinatra/test/rspec'
456 require 'my_sinatra_app'
457
458 describe 'My app' do
459 it 'should show a default page' do
c00a25e Ryan Tomayko Test framework refactoring
rtomayko authored
460 get '/'
10c90d5 Simon Rozet document testing with rspec
sr authored
461 @response.should be_ok
462 @response.body.should == 'My Default Page!'
463 end
464
1776a80 Added Version and Docs
Blake Mizerany authored
465 ...
466
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
467 end
1776a80 Added Version and Docs
Blake Mizerany authored
468
1fb5b99 Dylan Egan Bacon support
dylanegan authored
469 === Bacon
470
471 require 'sinatra'
472 require 'sinatra/test/bacon'
473 require 'my_sinatra_app'
474
475 describe 'My app' do
476 it 'should be ok' do
477 get '/'
478 should.be.ok
479 body.should == 'Im OK'
480 end
481 end
482
483 See Sinatra::Test for more information on +get+, +post+, +put+, and
484 friends.
1776a80 Added Version and Docs
Blake Mizerany authored
485
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
486 == Command line
1776a80 Added Version and Docs
Blake Mizerany authored
487
e75f4b3 Ryan Tomayko misc README formatting tweaks
rtomayko authored
488 Sinatra applications can be run directly:
f44fb6a Ryan Tomayko grammar/formatting pass over README
rtomayko authored
489
f29486b Karel Minarik Note "-s" (server) command line option in README
karmi authored
490 ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-s HANDLER]
1776a80 Added Version and Docs
Blake Mizerany authored
491
492 Options are:
493
494 -h # help
495 -p # set the port (default is 4567)
496 -e # set the environment (default is development)
f29486b Karel Minarik Note "-s" (server) command line option in README
karmi authored
497 -s # specify rack server/handler (default is thin)
e7e0e55 Ryan Tomayko Minor docfixes in README.rdoc
rtomayko authored
498 -x # turn on the mutex lock (default is off)
1776a80 Added Version and Docs
Blake Mizerany authored
499
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
500 == The Bleeding Edge
6326809 Markus Prinz Add a subsection about tools needed for Sinatra to the Contributing sect...
cypher authored
501
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
502 If you would like to use Sinatra's latest bleeding code, create a local
503 clone and run your app with the <tt>sinatra/lib</tt> directory on the
504 <tt>LOAD_PATH</tt>:
6326809 Markus Prinz Add a subsection about tools needed for Sinatra to the Contributing sect...
cypher authored
505
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
506 cd myapp
ba92616 Blake Mizerany Updated README to point to github.com/sinatra/sinatra.git
bmizerany authored
507 git clone git://github.com/sinatra/sinatra.git
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
508 ruby -Isinatra/lib myapp.rb
480fbfa Ryan Tomayko minor formatting tweaks to cypher's README updates
rtomayko authored
509
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
510 Alternatively, you can add the <tt>sinatra/lib<tt> directory to the
511 <tt>LOAD_PATH</tt> in your application:
1776a80 Added Version and Docs
Blake Mizerany authored
512
17cb177 Markus Prinz README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
513 $LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
514 require 'rubygems'
1776a80 Added Version and Docs
Blake Mizerany authored
515 require 'sinatra'
516
517 get '/about' do
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
518 "I'm running version " + Sinatra::VERSION
1776a80 Added Version and Docs
Blake Mizerany authored
519 end
e6c5471 Markus Prinz Add a community section with info about the mailing list and irc channel
cypher authored
520
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
521 To update the Sinatra sources in the future:
d8fec16 Markus Prinz Add subsection about contributing a patch as well as a link to the issue...
cypher authored
522
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
523 cd myproject/sinatra
524 git pull
e6c5471 Markus Prinz Add a community section with info about the mailing list and irc channel
cypher authored
525
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
526 == More
e6c5471 Markus Prinz Add a community section with info about the mailing list and irc channel
cypher authored
527
4298a77 Ryan Tomayko Tweak README formatting; move community/contributing to website
rtomayko authored
528 * {Project Website}[http://sinatra.github.com/] - Additional documentation,
529 news, and links to other resources.
530 * {Contributing}[http://sinatra.github.com/contribute.html] - Find a bug? Need
531 help? Have a patch?
532 * {Lighthouse}[http://sinatra.lighthouseapp.com] - Issue tracking and release
533 planning.
534 * {Mailing List}[http://groups.google.com/group/sinatrarb]
535 * {IRC: #sinatra}[irc://chat.freenode.net/#sinatra] on http://freenode.net
Something went wrong with that request. Please try again.