Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 547 lines (375 sloc) 12.368 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
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
13 Run with <tt>ruby myapp.rb</tt> and view at <tt>http://localhost:4567</tt>
df800b5 Docs are started
Blake Mizerany authored
14
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
15 == HTTP Methods
df800b5 Docs are started
Blake Mizerany authored
16
17 get '/' do
18 .. show things ..
19 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
20
df800b5 Docs are started
Blake Mizerany authored
21 post '/' do
22 .. create something ..
23 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
24
df800b5 Docs are started
Blake Mizerany authored
25 put '/' do
26 .. update something ..
27 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
28
df800b5 Docs are started
Blake Mizerany authored
29 delete '/' do
30 .. annihilate something ..
31 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
32
33 == Routes
1776a80 Added Version and Docs
Blake Mizerany authored
34
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
35 Routes are matched based on the order of declaration. The first route that
36 matches the request is invoked.
1776a80 Added Version and Docs
Blake Mizerany authored
37
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
38 Basic routes:
1776a80 Added Version and Docs
Blake Mizerany authored
39
40 get '/hi' do
41 ...
42 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko 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
47 get '/: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
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
53 Route patterns may also include splat (or wildcard) parameters, accessible
54 via the <tt>params[:splat]</tt> array.
1776a80 Added Version and Docs
Blake Mizerany authored
55
9c85e99 @vic Specs, documentation and fixes for splat'n routes
vic authored
56 get '/say/*/to/*' do
57 # matches /say/hello/to/world
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
58 params[:splat] # => ["hello", "world"]
9c85e99 @vic Specs, documentation and fixes for splat'n routes
vic authored
59 end
60
61 get '/download/*.*' do
62 # matches /download/path/to/file.xml
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
63 params[:splat] # => ["path/to/file", "xml"]
1776a80 Added Version and Docs
Blake Mizerany authored
64 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
65
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
66 Route matching with Regular Expressions:
67
68 get %r{/hello/([\w]+)} do
69 "Hello, #{params[:captures].first}!"
70 end
71
72 Routes may include a variety of matching conditions, such as the user agent:
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
73
1776a80 Added Version and Docs
Blake Mizerany authored
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
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
79 # Matches non-songbird browsers
1776a80 Added Version and Docs
Blake Mizerany authored
80 end
df800b5 Docs are started
Blake Mizerany authored
81
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
82 == Static Files
047edc6 update README with Static help
Blake Mizerany authored
83
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
84 Static files are served from the <tt>./public</tt> directory. You can specify
85 a different location by setting the <tt>:public</tt> option:
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
86
87 set :public, File.dirname(__FILE__) + '/static'
df800b5 Docs are started
Blake Mizerany authored
88
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
89 == Views / Templates
df800b5 Docs are started
Blake Mizerany authored
90
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
91 Templates are assumed to be located directly under a <tt>./views</tt>
92 directory. To use a different views directory:
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
93
94 set :views, File.dirname(__FILE__) + '/templates'
95
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
96 === Haml Templates
df800b5 Docs are started
Blake Mizerany authored
97
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
98 The haml gem/library is required to render HAML templates:
99
df800b5 Docs are started
Blake Mizerany authored
100 get '/' do
101 haml :index
102 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
103
104 Renders <tt>./views/index.haml</tt>.
105
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
106 === Erb Templates
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
107
108 get '/' do
109 erb :index
110 end
111
112 Renders <tt>./views/index.erb</tt>
113
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
114 === Builder Templates
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
115
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
116 The builder gem/library is required to render builder templates:
df800b5 Docs are started
Blake Mizerany authored
117
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
118 get '/' do
119 content_type 'application/xml', :charset => 'utf-8'
120 builder :index
121 end
122
123 Renders <tt>./views/index.builder</tt>.
124
125 === Sass Templates
126
127 The sass gem/library is required to render Sass templates:
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
128
4144ac1 @nmeans Added Sass information to documentation.
nmeans authored
129 get '/stylesheet.css' do
ccc19b0 @rtomayko content_type response helper with mime type lookup and parameter supp…
rtomayko authored
130 content_type 'text/css', :charset => 'utf-8'
4144ac1 @nmeans Added Sass information to documentation.
nmeans authored
131 sass :stylesheet
132 end
133
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
134 Renders <tt>./views/stylesheet.sass</tt>.
135
136 === Inline Templates
df800b5 Docs are started
Blake Mizerany authored
137
138 get '/' do
139 haml '%div.title Hello World'
140 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
141
142 Renders the inlined template string.
df800b5 Docs are started
Blake Mizerany authored
143
144 === Accessing Variables
145
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
146 Templates are evaluated within the same context as the route blocks. Instance
147 variables set in route blocks are available in templates:
df800b5 Docs are started
Blake Mizerany authored
148
149 get '/:id' do
150 @foo = Foo.find(params[:id])
95aca76 @bleything fix documentation of variable interpolation into templates
bleything authored
151 haml '%h1= @foo.name'
df800b5 Docs are started
Blake Mizerany authored
152 end
153
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
154 Or, specify an explicit Hash of local variables:
df800b5 Docs are started
Blake Mizerany authored
155
156 get '/:id' do
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
157 foo = Foo.find(params[:id])
95aca76 @bleything fix documentation of variable interpolation into templates
bleything authored
158 haml '%h1= foo.name', :locals => { :foo => foo }
df800b5 Docs are started
Blake Mizerany authored
159 end
160
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
161 This is typically used when rendering templates as partials from within
162 other templates.
163
164 === In-file Templates
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
165
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
166 Templates may be defined at the end of the source file:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
167
168 get '/' do
169 haml :index
170 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
171
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
172 use_in_file_templates!
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
173
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
174 __END__
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
175
f71330e @bmizerany quick doc fix
bmizerany authored
176 @@ layout
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
177 %html
178 = yield
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
179
f71330e @bmizerany quick doc fix
bmizerany authored
180 @@ index
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
181 %div.title Hello world!!!!!
182
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
183 It's also possible to define named templates using the top-level template
184 method:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
185
186 template :layout do
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
187 "%html\n =yield\n"
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
188 end
189
190 template :index do
191 '%div.title Hello World!'
192 end
193
194 get '/' do
195 haml :index
196 end
197
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
198 If a template named "layout" exists, it will be used each time a template
199 is rendered. You can disable layouts by passing <tt>:layout => false</tt>.
578bbab @djanowski Updating README for :layout => true.
djanowski authored
200
201 get '/' do
202 haml :index, :layout => !request.xhr?
203 end
204
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
205 == Helpers
df800b5 Docs are started
Blake Mizerany authored
206
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
207 Use the top-level <tt>helpers</tt> method to define helper methods for use in
208 route blocks and templates:
df800b5 Docs are started
Blake Mizerany authored
209
210 helpers do
211 def bar(name)
212 "#{name}bar"
213 end
214 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
215
1776a80 Added Version and Docs
Blake Mizerany authored
216 get '/:name' do
217 bar(params[:name])
218 end
df800b5 Docs are started
Blake Mizerany authored
219
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
220 == Filters
df800b5 Docs are started
Blake Mizerany authored
221
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
222 Before filters are evaluated before each request within the context of the
223 request and can modify the request and response. Instance variables set in
224 filters are accessible by routes and templates.
1776a80 Added Version and Docs
Blake Mizerany authored
225
df800b5 Docs are started
Blake Mizerany authored
226 before do
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
227 @note = 'Hi!'
228 request.path_info = '/foo/bar/baz'
229 end
230
231 get '/foo/*' do
232 @note #=> 'Hi!'
233 params[:splat] #=> 'bar/baz'
df800b5 Docs are started
Blake Mizerany authored
234 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
235
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
236 == Halting
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
237
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
238 To immediately stop a request during a before filter or route use:
df800b5 Docs are started
Blake Mizerany authored
239
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
240 halt
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
241
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
242 You can also specify a body when halting ...
df800b5 Docs are started
Blake Mizerany authored
243
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
244 halt 'this will be the body'
df800b5 Docs are started
Blake Mizerany authored
245
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
246 Set the status and body ...
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
247
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
248 halt 401, 'go away!'
df800b5 Docs are started
Blake Mizerany authored
249
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
250 == Passing
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
251
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
252 A route can punt processing to the next matching route using the <tt>pass</tt>
253 statement:
df800b5 Docs are started
Blake Mizerany authored
254
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
255 get '/guess/:who' do
256 pass unless params[:who] == 'Frank'
257 "You got me!"
258 end
259
260 get '/guess/*' do
261 "You missed!"
262 end
263
264 The route block is immediately exited and control continues with the next
265 matching route. If no matching route is found, a 404 is returned.
df800b5 Docs are started
Blake Mizerany authored
266
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
267 == Configuration and Reloading
1776a80 Added Version and Docs
Blake Mizerany authored
268
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
269 Sinatra supports multiple environments and reloading. Reloading happens
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
270 before each request when running under the <tt>:development</tt>
271 environment. Wrap your configurations (e.g., database connections, constants,
272 etc.) in <tt>configure</tt> blocks to protect them from reloading or to
273 target specific environments.
1776a80 Added Version and Docs
Blake Mizerany authored
274
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
275 Run once, at startup, in any environment:
1776a80 Added Version and Docs
Blake Mizerany authored
276
277 configure do
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
278 ...
1776a80 Added Version and Docs
Blake Mizerany authored
279 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
280
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
281 Run only when the environment (RACK_ENV environment variable) is set to
282 <tt>:production</tt>.
1776a80 Added Version and Docs
Blake Mizerany authored
283
284 configure :production do
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
285 ...
1776a80 Added Version and Docs
Blake Mizerany authored
286 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
287
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
288 Run when the environment (RACK_ENV environment variable) is set to
289 either <tt>:production</tt> or <tt>:test</tt>.
1776a80 Added Version and Docs
Blake Mizerany authored
290
291 configure :production, :test do
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
292 ...
1776a80 Added Version and Docs
Blake Mizerany authored
293 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
294
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
295 == Error handling
1776a80 Added Version and Docs
Blake Mizerany authored
296
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
297 Error handlers run within the same context as routes and before filters, which
298 means you get all the goodies it has to offer, like <tt>haml</tt>, <tt>erb</tt>,
299 <tt>halt</tt>, etc.
1776a80 Added Version and Docs
Blake Mizerany authored
300
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
301 === Not Found
1776a80 Added Version and Docs
Blake Mizerany authored
302
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
303 When a <tt>Sinatra::NotFound</tt> exception is raised, or the response's status
304 code is 404, the <tt>not_found</tt> handler is invoked:
1776a80 Added Version and Docs
Blake Mizerany authored
305
306 not_found do
307 'This is nowhere to be found'
308 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
309
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
310 === Error
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
311
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
312 The +error+ handler is invoked any time an exception is raised from a route
313 block or before filter. The exception object can be obtained from the
314 'sinatra.error' Rack variable:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
315
1776a80 Added Version and Docs
Blake Mizerany authored
316 error do
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
317 'Sorry there was a nasty error - ' + env['sinatra.error'].name
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
318 end
319
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
320 Custom errors:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
321
322 error MyCustomError do
e7e0e55 @rtomayko Minor docfixes in README.rdoc
rtomayko authored
323 'So what happened was...' + request.env['sinatra.error'].message
1776a80 Added Version and Docs
Blake Mizerany authored
324 end
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
325
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
326 Then, if this happens:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
327
328 get '/' do
329 raise MyCustomError, 'something bad'
330 end
331
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
332 You get this:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
333
334 So what happened was... something bad
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
335
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
336 Sinatra installs special not_found and error handlers when running under
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
337 the development environment.
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
338
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
339 == Mime types
340
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
341 When using <tt>send_file</tt> or static files you may have mime types Sinatra
342 doesn't understand. Use +mime+ to register them by file extension:
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
343
344 mime :foo, 'text/foo'
1776a80 Added Version and Docs
Blake Mizerany authored
345
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
346 == Rack Middleware
bda21f1 @rtomayko add doc on using Rack middleware to README
rtomayko authored
347
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
348 Sinatra rides on Rack[http://rack.rubyforge.org/], a minimal standard
349 interface for Ruby web frameworks. One of Rack's most interesting capabilities
350 for application developers is support for "middleware" -- components that sit
351 between the server and your application monitoring and/or manipulating the
352 HTTP request/response to provide various types of common functionality.
bda21f1 @rtomayko add doc on using Rack middleware to README
rtomayko authored
353
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
354 Sinatra makes building Rack middleware pipelines a cinch via a top-level
355 +use+ method:
bda21f1 @rtomayko add doc on using Rack middleware to README
rtomayko authored
356
357 require 'sinatra'
358 require 'my_custom_middleware'
359
360 use Rack::Lint
361 use MyCustomMiddleware
362
363 get '/hello' do
364 'Hello World'
365 end
366
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
367 The semantics of +use+ are identical to those defined for the
368 Rack::Builder[http://rack.rubyforge.org/doc/classes/Rack/Builder.html] DSL
369 (most frequently used from rackup files). For example, the +use+ method
370 accepts multiple/variable args as well as blocks:
bda21f1 @rtomayko add doc on using Rack middleware to README
rtomayko authored
371
372 use Rack::Auth::Basic do |username, password|
373 username == 'admin' && password == 'secret'
374 end
375
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
376 Rack is distributed with a variety of standard middleware for logging,
377 debugging, URL routing, authentication, and session handling. Sinatra uses
378 many of of these components automatically based on configuration so you
379 typically don't have to +use+ them explicitly.
bda21f1 @rtomayko add doc on using Rack middleware to README
rtomayko authored
380
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
381 == Testing
1776a80 Added Version and Docs
Blake Mizerany authored
382
c00a25e @rtomayko Test framework refactoring
rtomayko authored
383 The Sinatra::Test module includes a variety of helper methods for testing
384 your Sinatra app. Sinatra includes support for Test::Unit, test-spec, RSpec,
385 and Bacon through separate source files.
386
387 === Test::Unit
1776a80 Added Version and Docs
Blake Mizerany authored
388
7cfe04a @jcrosby Fix for test/unit and test/spec docs
jcrosby authored
389 require 'sinatra'
1776a80 Added Version and Docs
Blake Mizerany authored
390 require 'sinatra/test/unit'
7cfe04a @jcrosby Fix for test/unit and test/spec docs
jcrosby authored
391 require 'my_sinatra_app'
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
392
1776a80 Added Version and Docs
Blake Mizerany authored
393 class MyAppTest < Test::Unit::TestCase
394 def test_my_default
c00a25e @rtomayko Test framework refactoring
rtomayko authored
395 get '/'
1776a80 Added Version and Docs
Blake Mizerany authored
396 assert_equal 'My Default Page!', @response.body
397 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
398
1776a80 Added Version and Docs
Blake Mizerany authored
399 def test_with_agent
c00a25e @rtomayko Test framework refactoring
rtomayko authored
400 get '/', :agent => 'Songbird'
1776a80 Added Version and Docs
Blake Mizerany authored
401 assert_equal 'You're in Songbird!', @response.body
402 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
403
1776a80 Added Version and Docs
Blake Mizerany authored
404 ...
405 end
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
406
c00a25e @rtomayko Test framework refactoring
rtomayko authored
407 === Test::Spec
408
409 Install the test-spec gem and require <tt>'sinatra/test/spec'</tt> before
410 your app:
1776a80 Added Version and Docs
Blake Mizerany authored
411
7cfe04a @jcrosby 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/spec'
7cfe04a @jcrosby Fix for test/unit and test/spec docs
jcrosby authored
414 require 'my_sinatra_app'
1776a80 Added Version and Docs
Blake Mizerany authored
415
7cfe04a @jcrosby Fix for test/unit and test/spec docs
jcrosby authored
416 describe 'My app' do
417 it "should show a default page" do
c00a25e @rtomayko Test framework refactoring
rtomayko authored
418 get '/'
1776a80 Added Version and Docs
Blake Mizerany authored
419 should.be.ok
83cba9c @bmizerany updated README with helpful tidbits
bmizerany authored
420 body.should.equal 'My Default Page!'
1776a80 Added Version and Docs
Blake Mizerany authored
421 end
10c90d5 @sr document testing with rspec
sr authored
422
423 ...
424 end
425
426 === RSpec
427
c00a25e @rtomayko Test framework refactoring
rtomayko authored
428 Install the rspec gem and require <tt>'sinatra/test/rspec'</tt> before
429 your app:
430
10c90d5 @sr document testing with rspec
sr authored
431 require 'sinatra'
432 require 'sinatra/test/rspec'
433 require 'my_sinatra_app'
434
435 describe 'My app' do
436 it 'should show a default page' do
c00a25e @rtomayko Test framework refactoring
rtomayko authored
437 get '/'
10c90d5 @sr document testing with rspec
sr authored
438 @response.should be_ok
439 @response.body.should == 'My Default Page!'
440 end
441
1776a80 Added Version and Docs
Blake Mizerany authored
442 ...
443
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
444 end
1776a80 Added Version and Docs
Blake Mizerany authored
445
1fb5b99 @dylanegan Bacon support
dylanegan authored
446 === Bacon
447
448 require 'sinatra'
449 require 'sinatra/test/bacon'
450 require 'my_sinatra_app'
451
452 describe 'My app' do
453 it 'should be ok' do
454 get '/'
455 should.be.ok
456 body.should == 'Im OK'
457 end
458 end
459
460 See Sinatra::Test for more information on +get+, +post+, +put+, and
461 friends.
1776a80 Added Version and Docs
Blake Mizerany authored
462
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
463 == Command line
1776a80 Added Version and Docs
Blake Mizerany authored
464
e75f4b3 @rtomayko misc README formatting tweaks
rtomayko authored
465 Sinatra applications can be run directly:
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
466
f29486b @karmi Note "-s" (server) command line option in README
karmi authored
467 ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-s HANDLER]
1776a80 Added Version and Docs
Blake Mizerany authored
468
469 Options are:
470
471 -h # help
472 -p # set the port (default is 4567)
473 -e # set the environment (default is development)
f29486b @karmi Note "-s" (server) command line option in README
karmi authored
474 -s # specify rack server/handler (default is thin)
e7e0e55 @rtomayko Minor docfixes in README.rdoc
rtomayko authored
475 -x # turn on the mutex lock (default is off)
1776a80 Added Version and Docs
Blake Mizerany authored
476
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
477 == Contributing
1776a80 Added Version and Docs
Blake Mizerany authored
478
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
479 === Tools
6326809 @cypher Add a subsection about tools needed for Sinatra to the Contributing s…
cypher authored
480
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
481 Besides Ruby itself, you only need a text editor, preferably one that supports
482 Ruby syntax hilighting. VIM and Emacs are a fine choice on any platform, but
483 feel free to use whatever you're familiar with.
6326809 @cypher Add a subsection about tools needed for Sinatra to the Contributing s…
cypher authored
484
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
485 Sinatra uses the Git source code management system. If you're unfamiliar with
486 Git, you can find more information and tutorials on http://git.or.cz/ as well
487 as http://git-scm.com/. Scott Chacon created a great series of introductory
488 screencasts about Git, which you can find here: http://www.gitcasts.com/
6326809 @cypher Add a subsection about tools needed for Sinatra to the Contributing s…
cypher authored
489
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
490 === First Time: Cloning The Sinatra Repo
4c02878 @cypher Update Contributing section to include initialization and updating th…
cypher authored
491
1776a80 Added Version and Docs
Blake Mizerany authored
492 cd where/you/keep/your/projects
493 git clone git://github.com/bmizerany/sinatra.git
4c02878 @cypher Update Contributing section to include initialization and updating th…
cypher authored
494 cd sinatra
495 cd path/to/your_project
1776a80 Added Version and Docs
Blake Mizerany authored
496 ln -s ../sinatra/
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
497
498 === Updating Your Existing Sinatra Clone
499
4c02878 @cypher Update Contributing section to include initialization and updating th…
cypher authored
500 cd where/you/keep/sinatra
501 git pull
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
502
503 === Using Edge Sinatra in Your App
1776a80 Added Version and Docs
Blake Mizerany authored
504
4c02878 @cypher Update Contributing section to include initialization and updating th…
cypher authored
505 at the top of your sinatra_app.rb file:
1776a80 Added Version and Docs
Blake Mizerany authored
506
17cb177 @cypher README and CHANGES tweaks for 0.9.0 release (#63)
cypher authored
507 $LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
1776a80 Added Version and Docs
Blake Mizerany authored
508 require 'sinatra'
509
510 get '/about' do
f44fb6a @rtomayko grammar/formatting pass over README
rtomayko authored
511 "I'm running on Version " + Sinatra::VERSION
1776a80 Added Version and Docs
Blake Mizerany authored
512 end
e6c5471 @cypher Add a community section with info about the mailing list and irc channel
cypher authored
513
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
514 === Contributing a Patch
d8fec16 @cypher Add subsection about contributing a patch as well as a link to the is…
cypher authored
515
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
516 There are several ways to do this. Probably the easiest (and preferred) way is
517 to fork Sinatra on GitHub (http://github.com/bmizerany/sinatra), push your
518 changes to your Sinatra repo, and then send Blake Mizerany (bmizerany on
519 GitHub) a pull request.
d8fec16 @cypher Add subsection about contributing a patch as well as a link to the is…
cypher authored
520
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
521 You can also create a patch file and attach it to a feature request or bug fix
522 on the issue tracker (see below) or send it to the mailing list (see Community
523 section).
d8fec16 @cypher Add subsection about contributing a patch as well as a link to the is…
cypher authored
524
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
525 === Issue Tracking and Feature Requests
d8fec16 @cypher Add subsection about contributing a patch as well as a link to the is…
cypher authored
526
527 http://sinatra.lighthouseapp.com/
528
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
529 == Community
e6c5471 @cypher Add a community section with info about the mailing list and irc channel
cypher authored
530
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
531 === Mailing List
e6c5471 @cypher Add a community section with info about the mailing list and irc channel
cypher authored
532
533 http://groups.google.com/group/sinatrarb
534
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
535 If you have a problem or question, please make sure to include all the
536 relevant information in your mail, like the Sinatra version you're using, what
537 version of Ruby you have, and so on.
e6c5471 @cypher Add a community section with info about the mailing list and irc channel
cypher authored
538
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
539 === IRC Channel
e6c5471 @cypher Add a community section with info about the mailing list and irc channel
cypher authored
540
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
541 You can find us on the Freenode network in the channel #sinatra
542 (irc://chat.freenode.net/#sinatra)
e6c5471 @cypher Add a community section with info about the mailing list and irc channel
cypher authored
543
480fbfa @rtomayko minor formatting tweaks to cypher's README updates
rtomayko authored
544 There's usually someone online at any given time, but we cannot pay attention
545 to the channel all the time, so please stick around for a while after asking a
546 question.
Something went wrong with that request. Please try again.