sinatra 1.1.b → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sinatra might be problematic. Click here for more details.

data/CHANGES CHANGED
@@ -1,4 +1,4 @@
1
- = 1.1 / Not Yet Released
1
+ = 1.1.0 / 2010-10-24
2
2
 
3
3
  * Before and after filters now support pattern matching, including the
4
4
  ability to use captures: "before('/user/:name') { |name| ... }". This
@@ -35,10 +35,10 @@
35
35
  content type corresponding to the rendering engine rather than just using
36
36
  "text/html". (Konstantin Haase)
37
37
 
38
- * README is now available in French (Mickael Riga), German (Bernhard Essl,
39
- Konstantin Haase, burningTyger), Hungarian (Janos Hardi) and Spanish
40
- (Gabriel Andretta). The extremely outdated Japanese README has been updated
41
- (Kouhei Yanagita).
38
+ * README is now available in Chinese (Wu Jiang), French (Mickael Riga),
39
+ German (Bernhard Essl, Konstantin Haase, burningTyger), Hungarian (Janos
40
+ Hardi) and Spanish (Gabriel Andretta). The extremely outdated Japanese
41
+ README has been updated (Kouhei Yanagita).
42
42
 
43
43
  * It is now possible to access Sinatra's template_cache from the outside.
44
44
  (Nick Sutterer)
@@ -66,8 +66,7 @@
66
66
  application under a subpath in Rails 3, the PATH_INFO is not prefixed with
67
67
  a slash and no routes did match. (José Valim)
68
68
 
69
- * Better handling of encodings in 1.9, defaults params encoding to UTF-8 and
70
- respects Encoding.default_internal and Encoding.default_external.
69
+ * Better handling of encodings in 1.9, defaults params encoding to UTF-8.
71
70
  (Konstantin Haase)
72
71
 
73
72
  * `show_exeptions` handling is now triggered after custom error handlers, if
@@ -623,7 +623,7 @@ The incoming request object can be accessed from request level (filter, routes,
623
623
  request.path # "/example/foo"
624
624
  request.ip # client IP address
625
625
  request.secure? # false
626
- requuest.env # raw env hash handed in by Rack
626
+ request.env # raw env hash handed in by Rack
627
627
  end
628
628
 
629
629
  Some options, like <tt>script_name</tt> or <tt>path_info</tt> can also be
@@ -0,0 +1,1024 @@
1
+ = Sinatra
2
+ <i>注:本文档仅仅是英文版的翻译,会出现内容没有及时更新的情况发生。如有不一致的地方,请以英文版为准。</i>
3
+
4
+ Sinatra是一个基于Ruby语言,以最小精力为代价快速创建web应用为目的的DSL(领域专属语言):
5
+
6
+ # myapp.rb
7
+ require 'sinatra'
8
+
9
+ get '/' do
10
+ 'Hello world!'
11
+ end
12
+
13
+ 安装gem然后运行:
14
+
15
+ gem install sinatra
16
+ ruby rubygems myapp.rb
17
+
18
+ 在该地址查看: http://localhost:4567
19
+
20
+ == 路由
21
+
22
+ 在Sinatra中,一个路由是一个HTTP方法与URL匹配范式的配对。
23
+ 每个路由都与一个代码块关联:
24
+
25
+ get '/' do
26
+ .. 显示一些事物 ..
27
+ end
28
+
29
+ post '/' do
30
+ .. 创建一些事物 ..
31
+ end
32
+
33
+ put '/' do
34
+ .. 更新一些事物 ..
35
+ end
36
+
37
+ delete '/' do
38
+ .. 消灭一些事物 ..
39
+ end
40
+
41
+ 路由按照它们被定义的顺序进行匹配。
42
+ 第一个与请求匹配的路由会被调用。
43
+
44
+ 路由范式可以包括具名参数,
45
+ 可通过<tt>params</tt>哈希表获得:
46
+
47
+ get '/hello/:name' do
48
+ # 匹配 "GET /hello/foo" 和 "GET /hello/bar"
49
+ # params[:name] 的值是 'foo' 或者 'bar'
50
+ "Hello #{params[:name]}!"
51
+ end
52
+
53
+ 你同样可以通过代码块参数获得具名参数:
54
+
55
+ get '/hello/:name' do |n|
56
+ "Hello #{n}!"
57
+ end
58
+
59
+ 路由范式也可以包含通配符参数,
60
+ 可以通过<tt>params[:splat]</tt> 数组获得。
61
+
62
+ get '/say/*/to/*' do
63
+ # 匹配 /say/hello/to/world
64
+ params[:splat] # => ["hello", "world"]
65
+ end
66
+
67
+ get '/download/*.*' do
68
+ # 匹配 /download/path/to/file.xml
69
+ params[:splat] # => ["path/to/file", "xml"]
70
+ end
71
+
72
+ 通过正则表达式匹配的路由:
73
+
74
+ get %r{/hello/([\w]+)} do
75
+ "Hello, #{params[:captures].first}!"
76
+ end
77
+
78
+ 或者使用代码块参数:
79
+
80
+ get %r{/hello/([\w]+)} do |c|
81
+ "Hello, #{c}!"
82
+ end
83
+
84
+ === 条件
85
+
86
+ 路由也可以包含多样的匹配条件,比如user agent:
87
+
88
+ get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
89
+ "你正在使用Songbird,版本是 #{params[:agent][0]}"
90
+ end
91
+
92
+ get '/foo' do
93
+ # 匹配除Songbird以外的浏览器
94
+ end
95
+
96
+ 其他可选的条件是 +host_name+ 和 +provides+:
97
+
98
+ get '/', :host_name => /^admin\./ do
99
+ "管理员区域,无权进入!"
100
+ end
101
+
102
+ get '/', :provides => 'html' do
103
+ haml :index
104
+ end
105
+
106
+ get '/', :provides => ['rss', 'atom', 'xml'] do
107
+ builder :feed
108
+ end
109
+
110
+ 你也可以很轻松地定义自己的条件:
111
+
112
+ set(:probability) { |value| condition { rand <= value } }
113
+
114
+ get '/win_a_car', :probability => 0.1 do
115
+ "You won!"
116
+ end
117
+
118
+ get '/win_a_car' do
119
+ "Sorry, you lost."
120
+ end
121
+
122
+ === 返回值
123
+
124
+ 一个路由代码块的返回值最少决定了返回给HTTP客户端的响应体,
125
+ 或者至少决定了在Rack堆栈中的下一个中间件。
126
+ 大多数情况下,将是一个字符串,就像上面的例子中的一样。
127
+ 但是其他值也是可以接受的。
128
+
129
+ 你可以返回任何对象,或者是一个合理的Rack响应,
130
+ Rack body对象或者HTTP状态码:
131
+
132
+ * 一个包含三个元素的数组: <tt>[状态 (Fixnum), 头 (Hash), 响应体 (回应 #each)]</tt>
133
+ * 一个包含两个元素的数组: <tt>[状态 (Fixnum), 响应体 (回应 #each)]</tt>
134
+ * 一个能够回应 <tt>#each</tt> ,只传回字符串的对象
135
+ * 一个代表状态码的数字
136
+
137
+ 那样,我们可以轻松的实现例如流式传输的例子:
138
+
139
+ class Stream
140
+ def each
141
+ 100.times { |i| yield "#{i}\n" }
142
+ end
143
+ end
144
+
145
+ get('/') { Stream.new }
146
+
147
+ == 静态文件
148
+
149
+ 静态文件是从 <tt>./public</tt> 目录提供服务。你可以通过设置<tt>:public</tt>
150
+ 选项设定一个不同的位置:
151
+
152
+ set :public, File.dirname(__FILE__) + '/static'
153
+
154
+ 请注意public目录名并没有被包含在URL之中。文件
155
+ <tt>./public/css/style.css</tt>是通过
156
+ <tt>http://example.com/css/style.css</tt>地址访问。
157
+
158
+ == 视图 / 模板
159
+
160
+ 模板被假定直接位于<tt>./views</tt>目录。
161
+ 要使用不同的视图目录:
162
+
163
+ set :views, File.dirname(__FILE__) + '/templates'
164
+
165
+ 请记住一件非常重要的事情,你只可以使用符号引用模板,
166
+ 即使它们在子目录下
167
+ (在这种情况下,使用 <tt>:'subdir/template'</tt>)。
168
+ 你必须使用一个符号,因为渲染方法会直接地渲染
169
+ 任何传入的字符串。
170
+
171
+ === Haml模板
172
+
173
+ 需要引入haml gem/library以渲染 HAML 模板:
174
+
175
+ ## 你需要在你的应用中引入 haml
176
+ require 'haml'
177
+
178
+ get '/' do
179
+ haml :index
180
+ end
181
+
182
+ 渲染 <tt>./views/index.haml</tt>。
183
+
184
+ {Haml的选项}[http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#options]
185
+ 可以通过Sinatra的配置全局设定,
186
+ 参见 {选项和配置}[http://www.sinatrarb.com/configuration.html],
187
+ 也可以个别的被覆盖。
188
+
189
+ set :haml, {:format => :html5 } # 默认的Haml输出格式是 :xhtml
190
+
191
+ get '/' do
192
+ haml :index, :haml_options => {:format => :html4 } # 被覆盖,变成:html4
193
+ end
194
+
195
+
196
+ === Erb模板
197
+
198
+ ## 你需要在你的应用中引入 erb
199
+ require 'erb'
200
+
201
+ get '/' do
202
+ erb :index
203
+ end
204
+
205
+ 渲染 <tt>./views/index.erb</tt>
206
+
207
+ === Erubis
208
+
209
+ 需要引入erubis gem/library以渲染 erubis 模板:
210
+
211
+ ## 你需要在你的应用中引入 erubis
212
+ require 'erubis'
213
+
214
+ get '/' do
215
+ erubis :index
216
+ end
217
+
218
+ 渲染 <tt>./views/index.erubis</tt>
219
+
220
+ === Builder 模板
221
+
222
+ 需要引入 builder gem/library 以渲染 builder templates:
223
+
224
+ ## 需要在你的应用中引入builder
225
+ require 'builder'
226
+
227
+ get '/' do
228
+ builder :index
229
+ end
230
+
231
+ 渲染 <tt>./views/index.builder</tt>。
232
+
233
+ === Nokogiri 模板
234
+
235
+ 需要引入 nokogiri gem/library 以渲染 nokogiri 模板:
236
+
237
+ ## 需要在你的应用中引入 nokogiri
238
+ require 'nokogiri'
239
+
240
+ get '/' do
241
+ nokogiri :index
242
+ end
243
+
244
+ 渲染 <tt>./views/index.nokogiri</tt>。
245
+
246
+ === Sass 模板
247
+
248
+ 需要引入 haml gem/library 以渲染 Sass 模板:
249
+
250
+ ## 需要在你的应用中引入 haml 或者 sass
251
+ require 'sass'
252
+
253
+ get '/stylesheet.css' do
254
+ sass :stylesheet
255
+ end
256
+
257
+ 渲染 <tt>./views/stylesheet.sass</tt>。
258
+
259
+ {Sass 的选项}[http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options]
260
+ 可以通过Sinatra选项全局设定,
261
+ 参考 {选项和配置(英文)}[http://www.sinatrarb.com/configuration.html],
262
+ 也可以在个体的基础上覆盖。
263
+
264
+ set :sass, {:style => :compact } # 默认的 Sass 样式是 :nested
265
+
266
+ get '/stylesheet.css' do
267
+ sass :stylesheet, :style => :expanded # 覆盖
268
+ end
269
+
270
+ === Scss 模板
271
+
272
+ 需要引入 haml gem/library 来渲染 Scss templates:
273
+
274
+ ## 需要在你的应用中引入 haml 或者 sass
275
+ require 'sass'
276
+
277
+ get '/stylesheet.css' do
278
+ scss :stylesheet
279
+ end
280
+
281
+ 渲染 <tt>./views/stylesheet.scss</tt>。
282
+
283
+ {Scss的选项}[http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options]
284
+ 可以通过Sinatra选项全局设定,
285
+ 参考 {选项和配置(英文)}[http://www.sinatrarb.com/configuration.html],
286
+ 也可以在个体的基础上覆盖。
287
+
288
+ set :scss, :style => :compact # default Scss style is :nested
289
+
290
+ get '/stylesheet.css' do
291
+ scss :stylesheet, :style => :expanded # overridden
292
+ end
293
+
294
+ === Less 模板
295
+
296
+ 需要引入 less gem/library 以渲染 Less 模板:
297
+
298
+ ## 需要在你的应用中引入 less
299
+ require 'less'
300
+
301
+ get '/stylesheet.css' do
302
+ less :stylesheet
303
+ end
304
+
305
+ 渲染 <tt>./views/stylesheet.less</tt>。
306
+
307
+ === Liquid 模板
308
+
309
+ 需要引入 liquid gem/library 来渲染 Liquid 模板:
310
+
311
+ ## 需要在你的应用中引入 liquid
312
+ require 'liquid'
313
+
314
+ get '/' do
315
+ liquid :index
316
+ end
317
+
318
+ 渲染 <tt>./views/index.liquid</tt>。
319
+
320
+ 因为你不能在Liquid 模板中调用 Ruby 方法 (除了 +yield+) ,
321
+ 你几乎总是需要传递locals给它:
322
+
323
+ liquid :index, :locals => { :key => 'value' }
324
+
325
+ === Markdown 模板
326
+
327
+ 需要引入 rdiscount gem/library 以渲染 Markdown 模板:
328
+
329
+ ## 需要在你的应用中引入rdiscount
330
+ require "rdiscount"
331
+
332
+ get '/' do
333
+ markdown :index
334
+ end
335
+
336
+ 渲染 <tt>./views/index.markdown</tt>
337
+ (+md+ 和 +mkd+ 也是合理的文件扩展名)。
338
+
339
+ 在markdown中是不可以调用方法的,也不可以传递 locals给它。你因此一般会结合其他的渲染引擎来使用它:
340
+
341
+ erb :overview, :locals => { :text => markdown(:introduction) }
342
+
343
+ 请注意你也可以从其他模板中调用 markdown 方法:
344
+
345
+ %h1 Hello From Haml!
346
+ %p= markdown(:greetings)
347
+
348
+ === Textile 模板
349
+
350
+ 需要引入 RedCloth gem/library 以渲染 Textile 模板:
351
+
352
+ ## 在你的应用中引入redcloth
353
+ require "redcloth"
354
+
355
+ get '/' do
356
+ textile :index
357
+ end
358
+
359
+ 渲染 <tt>./views/index.textile</tt>。
360
+
361
+ 在textile中是不可以调用方法的,也不可以传递 locals给它。你因此一般会结合其他的渲染引擎来使用它:
362
+
363
+ erb :overview, :locals => { :text => textile(:introduction) }
364
+
365
+ 请注意你也可以从其他模板中调用textile方法:
366
+
367
+ %h1 Hello From Haml!
368
+ %p= textile(:greetings)
369
+
370
+ === RDoc 模板
371
+
372
+ 需要引入 RDoc gem/library 以渲染RDoc模板:
373
+
374
+ ## 需要在你的应用中引入rdoc
375
+ require "rdoc"
376
+
377
+ get '/' do
378
+ rdoc :index
379
+ end
380
+
381
+ 渲染 <tt>./views/index.rdoc</tt>。
382
+
383
+ 在rdoc中是不可以调用方法的,也不可以传递 locals给它。你因此一般会结合其他的渲染引擎来使用它:
384
+
385
+ erb :overview, :locals => { :text => rdoc(:introduction) }
386
+
387
+ 请注意你也可以从其他模板中调用rdoc方法:
388
+
389
+ %h1 Hello From Haml!
390
+ %p= rdoc(:greetings)
391
+
392
+ === Radius 模板
393
+
394
+ 需要引入 radius gem/library 以渲染 Radius 模板:
395
+
396
+ ## You'll need to require radius in your app
397
+ require 'radius'
398
+
399
+ get '/' do
400
+ radius :index
401
+ end
402
+
403
+ 渲染 <tt>./views/index.radius</tt>。
404
+
405
+ 因为你不能在Liquid 模板中调用 Ruby 方法 (除了 +yield+) ,
406
+ 你几乎总是需要传递locals给它:
407
+
408
+ radius :index, :locals => { :key => 'value' }
409
+
410
+ === Markaby 模板
411
+
412
+ 需要引入markaby gem/library以渲染Markaby模板:
413
+
414
+ ##需要在你的应用中引入 markaby
415
+ require 'markaby'
416
+
417
+ get '/' do
418
+ markaby :index
419
+ end
420
+
421
+ 渲染 <tt>./views/index.mab</tt>。
422
+
423
+ === CoffeeScript 模板
424
+
425
+ 需要引入 coffee-script gem/library 并在路径中存在 `coffee` 二进制文件以渲染
426
+ CoffeeScript 模板:
427
+
428
+ ## 需要在你的应用中引入coffee-script
429
+ require 'coffee-script'
430
+
431
+ get '/application.js' do
432
+ coffee :application
433
+ end
434
+
435
+ 渲染 <tt>./views/application.coffee</tt>。
436
+
437
+ === 内联模板字符串
438
+
439
+ get '/' do
440
+ haml '%div.title Hello World'
441
+ end
442
+
443
+ 渲染内联模板字符串。
444
+
445
+ === 在模板中访问变量
446
+
447
+ 模板和路由执行器在同样的上下文求值。
448
+ 在路由执行器中赋值的实例变量可以直接被模板访问。
449
+
450
+ get '/:id' do
451
+ @foo = Foo.find(params[:id])
452
+ haml '%h1= @foo.name'
453
+ end
454
+
455
+ 或者,显式地指定一个本地变量的哈希:
456
+
457
+ get '/:id' do
458
+ foo = Foo.find(params[:id])
459
+ haml '%h1= foo.name', :locals => { :foo => foo }
460
+ end
461
+
462
+ 典型的使用情况是在别的模板中按照部分模板的方式来渲染。
463
+
464
+
465
+ === 内联模板
466
+
467
+ 模板可以在源文件的末尾定义:
468
+
469
+ require 'sinatra'
470
+
471
+ get '/' do
472
+ haml :index
473
+ end
474
+
475
+ __END__
476
+
477
+ @@ layout
478
+ %html
479
+ = yield
480
+
481
+ @@ index
482
+ %div.title Hello world!!!!!
483
+
484
+ 注意:引入sinatra的源文件中定义的内联模板才能被自动载入。
485
+ 如果你在其他源文件中有内联模板,
486
+ 需要显式执行调用<tt>enable :inline_templates</tt>。
487
+
488
+ === 具名模板
489
+
490
+ 模板可以通过使用顶层 <tt>template</tt> 方法定义:
491
+
492
+ template :layout do
493
+ "%html\n =yield\n"
494
+ end
495
+
496
+ template :index do
497
+ '%div.title Hello World!'
498
+ end
499
+
500
+ get '/' do
501
+ haml :index
502
+ end
503
+
504
+ 如果存在名为"layout"的模板,该模板会在每个模板渲染的时候被使用。
505
+ 你可以通过传送 <tt>:layout => false</tt>来禁用。
506
+
507
+ get '/' do
508
+ haml :index, :layout => !request.xhr?
509
+ end
510
+
511
+ == 辅助方法
512
+
513
+ 使用顶层的 <tt>helpers</tt> 方法来定义辅助方法,
514
+ 以便在路由处理器和模板中使用:
515
+
516
+ helpers do
517
+ def bar(name)
518
+ "#{name}bar"
519
+ end
520
+ end
521
+
522
+ get '/:name' do
523
+ bar(params[:name])
524
+ end
525
+
526
+ == 过滤器
527
+
528
+ 前置过滤器在每个请求前,在请求的上下文环境中被执行,
529
+ 而且可以修改请求和响应。
530
+ 在过滤器中设定的实例变量可以被路由和模板访问:
531
+
532
+ before do
533
+ @note = 'Hi!'
534
+ request.path_info = '/foo/bar/baz'
535
+ end
536
+
537
+ get '/foo/*' do
538
+ @note #=> 'Hi!'
539
+ params[:splat] #=> 'bar/baz'
540
+ end
541
+
542
+ 后置过滤器在每个请求之后,在请求的上下文环境中执行,
543
+ 而且可以修改请求和响应。
544
+ 在前置过滤器和路由中设定的实例变量可以被后置过滤器访问:
545
+
546
+ after do
547
+ puts response.status
548
+ end
549
+
550
+ 过滤器可以可选地带有范式,
551
+ 只有请求路径满足该范式时才会执行:
552
+
553
+ before '/protected/*' do
554
+ authenticate!
555
+ end
556
+
557
+ after '/create/:slug' do |slug|
558
+ session[:last_slug] = slug
559
+ end
560
+
561
+ == 挂起
562
+
563
+ 要想直接地停止请求,在过滤器或者路由中使用:
564
+
565
+ halt
566
+
567
+ 你也可以指定挂起时的状态码:
568
+
569
+ halt 410
570
+
571
+ 或者消息体:
572
+
573
+ halt 'this will be the body'
574
+
575
+ 或者两者;
576
+
577
+ halt 401, 'go away!'
578
+
579
+ 也可以带消息头:
580
+
581
+ halt 402, {'Content-Type' => 'text/plain'}, 'revenge'
582
+
583
+ == 让路
584
+
585
+ 一个路由可以放弃处理,将处理让给下一个匹配的路由,使用 <tt>pass</tt>:
586
+
587
+ get '/guess/:who' do
588
+ pass unless params[:who] == 'Frank'
589
+ 'You got me!'
590
+ end
591
+
592
+ get '/guess/*' do
593
+ 'You missed!'
594
+ end
595
+
596
+ 路由代码块被直接退出,控制流继续前进到下一个匹配的路由。
597
+ 如果没有匹配的路由,将返回404。
598
+
599
+ == 访问请求对象
600
+
601
+ 传入的请求对象可以在请求层(过滤器,路由,错误处理)通过 `request` 方法被访问:
602
+
603
+ # 在 http://example.com/example 上运行的应用
604
+ get '/foo' do
605
+ request.body # 被客户端设定的请求体(见下)
606
+ request.scheme # "http"
607
+ request.script_name # "/example"
608
+ request.path_info # "/foo"
609
+ request.port # 80
610
+ request.request_method # "GET"
611
+ request.query_string # ""
612
+ request.content_length # request.body的长度
613
+ request.media_type # request.body的媒体类型
614
+ request.host # "example.com"
615
+ request.get? # true (其他动词也具有类似方法)
616
+ request.form_data? # false
617
+ request["SOME_HEADER"] # SOME_HEADER header的值
618
+ request.referer # 客户端的referer 或者 '/'
619
+ request.user_agent # user agent (被 :agent 条件使用)
620
+ request.cookies # 浏览器 cookies 哈希
621
+ request.xhr? # 这是否是ajax请求?
622
+ request.url # "http://example.com/example/foo"
623
+ request.path # "/example/foo"
624
+ request.ip # 客户端IP地址
625
+ request.secure? # false
626
+ request.env # Rack中使用的未处理的env哈希
627
+ end
628
+
629
+ 一些选项,例如 <tt>script_name</tt> 或者 <tt>path_info</tt>
630
+ 也是可写的:
631
+
632
+ before { request.path_info = "/" }
633
+
634
+ get "/" do
635
+ "all requests end up here"
636
+ end
637
+
638
+ <tt>request.body</tt> 是一个IO或者StringIO对象:
639
+
640
+ post "/api" do
641
+ request.body.rewind # 如果已经有人读了它
642
+ data = JSON.parse request.body.read
643
+ "Hello #{data['name']}!"
644
+ end
645
+
646
+ == 配置
647
+
648
+ 运行一次,在启动的时候,在任何环境下:
649
+
650
+ configure do
651
+ ...
652
+ end
653
+
654
+ 只当环境 (RACK_ENV environment 变量) 被设定为
655
+ <tt>:production</tt>的时候运行:
656
+
657
+ configure :production do
658
+ ...
659
+ end
660
+
661
+ 当环境被设定为 <tt>:production</tt> 或者
662
+ <tt>:test</tt>的时候运行:
663
+
664
+ configure :production, :test do
665
+ ...
666
+ end
667
+
668
+ == 错误处理
669
+
670
+ 错误处理在与路由和前置过滤器相同的上下文中运行,
671
+ 这意味着你可以使用许多好东西,比如 <tt>haml</tt>, <tt>erb</tt>,
672
+ <tt>halt</tt>,等等。
673
+
674
+ === 未找到
675
+
676
+ 当一个 <tt>Sinatra::NotFound</tt> 错误被抛出的时候,
677
+ 或者响应状态码是404,<tt>not_found</tt> 处理器会被调用:
678
+
679
+ not_found do
680
+ 'This is nowhere to be found'
681
+ end
682
+
683
+ === 错误
684
+
685
+ +error+ 处理器,在任何路由代码块或者过滤器抛出异常的时候会被调用。
686
+ 异常对象可以通过<tt>sinatra.error</tt> Rack 变量获得:
687
+
688
+
689
+ error do
690
+ 'Sorry there was a nasty error - ' + env['sinatra.error'].name
691
+ end
692
+
693
+ 自定义错误:
694
+
695
+ error MyCustomError do
696
+ 'So what happened was...' + request.env['sinatra.error'].message
697
+ end
698
+
699
+ 那么,当这个发生的时候:
700
+
701
+ get '/' do
702
+ raise MyCustomError, 'something bad'
703
+ end
704
+
705
+ 你会得到:
706
+
707
+ So what happened was... something bad
708
+
709
+ 另一种替代方法是,为一个状态码安装错误处理器:
710
+
711
+ error 403 do
712
+ 'Access forbidden'
713
+ end
714
+
715
+ get '/secret' do
716
+ 403
717
+ end
718
+
719
+ 或者一个范围:
720
+
721
+ error 400..510 do
722
+ 'Boom'
723
+ end
724
+
725
+ 在运行在development环境下时,Sinatra会安装特殊的
726
+ <tt>not_found</tt> 和 <tt>error</tt> 处理器。
727
+
728
+ == 媒体类型
729
+
730
+ 当使用 <tt>send_file</tt> 或者静态文件的适合,你的媒体类型可能
731
+ Sinatra并不理解。使用 +mime_type+ 通过文件扩展名来注册它们:
732
+
733
+ mime_type :foo, 'text/foo'
734
+
735
+ 你也可以通过 +content_type+ 辅助方法使用:
736
+
737
+ content_type :foo
738
+
739
+ == Rack 中间件
740
+
741
+ Sinatra 依靠 Rack[http://rack.rubyforge.org/],
742
+ 一个面向Ruby web框架的最小标准接口。
743
+ Rack的一个最有趣的面向应用开发者的能力是支持“中间件”——坐落在服务器和你的应用之间,
744
+ 监视 并/或 操作HTTP请求/响应以
745
+ 提供多样类型的常用功能。
746
+
747
+ Sinatra 让建立Rack中间件管道异常简单,
748
+ 通过顶层的 +use+ 方法:
749
+
750
+ require 'sinatra'
751
+ require 'my_custom_middleware'
752
+
753
+ use Rack::Lint
754
+ use MyCustomMiddleware
755
+
756
+ get '/hello' do
757
+ 'Hello World'
758
+ end
759
+
760
+ +use+ 的语义和在Rack::Builder[http://rack.rubyforge.org/doc/classes/Rack/Builder.html] DSL
761
+ (在rack文件中最频繁使用)
762
+ 中定义的完全一样。例如,+use+ 方法
763
+ 接受 多个/可变 参数,包括代码块:
764
+
765
+ use Rack::Auth::Basic do |username, password|
766
+ username == 'admin' && password == 'secret'
767
+ end
768
+
769
+ Rack中分布有多样的标准中间件,针对日志,
770
+ 调试,URL路由,认证和session处理。
771
+ Sinatra会自动使用这里面的大部分组件,
772
+ 所以你一般不需要显示地 +use+ 他们。
773
+
774
+ == 测试
775
+
776
+ Sinatra的测试可以使用任何基于Rack的测试程序库或者框架来编写。
777
+ {Rack::Test}[http://gitrdoc.com/brynary/rack-test]
778
+ 是推荐候选:
779
+
780
+ require 'my_sinatra_app'
781
+ require 'test/unit'
782
+ require 'rack/test'
783
+
784
+ class MyAppTest < Test::Unit::TestCase
785
+ include Rack::Test::Methods
786
+
787
+ def app
788
+ Sinatra::Application
789
+ end
790
+
791
+ def test_my_default
792
+ get '/'
793
+ assert_equal 'Hello World!', last_response.body
794
+ end
795
+
796
+ def test_with_params
797
+ get '/meet', :name => 'Frank'
798
+ assert_equal 'Hello Frank!', last_response.body
799
+ end
800
+
801
+ def test_with_rack_env
802
+ get '/', {}, 'HTTP_USER_AGENT' => 'Songbird'
803
+ assert_equal "You're using Songbird!", last_response.body
804
+ end
805
+ end
806
+
807
+ 请注意: 内置的 Sinatra::Test 模块和 Sinatra::TestHarness 类
808
+ 在 0.9.2 版本已废弃。
809
+
810
+ == Sinatra::Base - 中间件,程序库和模块化应用
811
+
812
+ 把你的应用定义在顶层,对于微型应用这会工作得很好,但是在
813
+ 构建可复用的组件时候会带来客观的不利,
814
+ 比如构建Rack中间件,Rails metal,带有服务器组件的简单程序库,
815
+ 或者甚至是Sinatra扩展。顶层的DSL污染了Object命名空间并
816
+ 假定了一个微型应用风格的配置 (例如, 单一的应用文件,
817
+ ./public 和 ./views 目录,日志,异常细节页面,等等)。
818
+ 这时应该让 Sinatra::Base 走到台前了:
819
+
820
+ require 'sinatra/base'
821
+
822
+ class MyApp < Sinatra::Base
823
+ set :sessions, true
824
+ set :foo, 'bar'
825
+
826
+ get '/' do
827
+ 'Hello world!'
828
+ end
829
+ end
830
+
831
+ MyApp 类是一个独立的Rack组件,可以扮演
832
+ Rack中间件,一个Rack应用,或者 Rails metal。你可以从rackup +config.ru文件
833
+ +use+ 或者 +run+ 这个类;或者,
834
+ 直接控制作为程序库提供的服务器组件:
835
+
836
+ MyApp.run! :host => 'localhost', :port => 9090
837
+
838
+ Sinatra::Base子类可用的方法实际上就是
839
+ 通过顶层DSL可用的方法。大部分顶层应用可以通过两个改变
840
+ 转换成Sinatra::Base组件:
841
+
842
+ * 你的文件应当引入 +sinatra/base+ 而不是 +sinatra+;
843
+ 否则,所有的Sinatra的 DSL 方法将会被引进到
844
+ 主命名空间。
845
+ * 把你的应用的路由,错误处理,过滤器和选项放在
846
+ 一个Sinatra::Base的子类中。
847
+
848
+ <tt>+Sinatra::Base+</tt> 是一张白纸。大部分的选项默认是禁用的,
849
+ 包含内置的服务器。参见 {选项和配置}[http://sinatra.github.com/configuration.html]
850
+ 查看可用选项的具体细节和他们的行为。
851
+
852
+ === 把Sinatra当成中间件来使用
853
+
854
+ 不仅Sinatra有能力使用其他的Rack中间件,任何Sinatra
855
+ 应用程序都可以反过来自身被当作中间件,被加在任何Rack断电前面。
856
+ 这个端点可以是任何Sinatra应用,或者任何基于Rack的应用程序
857
+ (Rails/Ramaze/Camping/...)。
858
+
859
+ require 'sinatra/base'
860
+
861
+ class LoginScreen < Sinatra::Base
862
+ enable :session
863
+
864
+ get('/login') { haml :login }
865
+
866
+ post('/login') do
867
+ if params[:name] = 'admin' and params[:password] = 'admin'
868
+ session['user_name'] = params[:name]
869
+ else
870
+ redirect '/login'
871
+ end
872
+ end
873
+ end
874
+
875
+ class MyApp < Sinatra::Base
876
+ # 在前置过滤器前运行中间件
877
+ use LoginScreen
878
+
879
+ before do
880
+ unless session['user_name']
881
+ halt "Access denied, please <a href='/login'>login</a>."
882
+ end
883
+ end
884
+
885
+ get('/') { "Hello #{session['user_name']}." }
886
+ end
887
+
888
+ == 变量域和绑定
889
+
890
+ 当前所在的变量域决定了哪些方法和变量是可用的。
891
+
892
+
893
+ === 应用/类 变量域
894
+
895
+ 每个Sinatra应用相当与Sinatra::Base的一个子类。
896
+ 如果你在使用顶层DSL(<tt>require 'sinatra'</tt>),那么这个类就是
897
+ Sinatra::Application,或者这个类就是你显式创建的子类。
898
+ 在类层面,你具有的方法类似于 `get` 或者 `before`,但是你不能访问
899
+ `request` 对象或者 `session`, 因为对于所有的请求,
900
+ 只有单一的应用类。
901
+
902
+ 通过 `set` 创建的选项是类层面的方法:
903
+
904
+ class MyApp << Sinatra::Base
905
+ # 嘿,我在应用变量域!
906
+ set :foo, 42
907
+ foo # => 42
908
+
909
+ get '/foo' do
910
+ # 嘿,我不再处于应用变量域了!
911
+ end
912
+ end
913
+
914
+ 在下列情况下你将拥有应用变量域的绑定:
915
+
916
+ * 在应用类中
917
+ * 在扩展中定义的方法
918
+ * 传递给 `helpers` 的代码块
919
+ * 用作`set`值的过程/代码块
920
+
921
+ 你可以访问变量域对象(就是应用类)就像这样:
922
+
923
+ * 通过传递给代码块的对象 (<tt>configure { |c| ... }</tt>)
924
+ * 在请求变量域中使用`settings`
925
+
926
+ === 请求/实例 变量域
927
+
928
+ 对于每个进入的请求,一个新的应用类的实例会被创建
929
+ 所有的处理器代码块在该变量域被运行。在这个变量域中,
930
+ 你可以访问 `request` 和 `session` 对象,或者调用渲染方法比如
931
+ `erb` 或者 `haml`。你可以在请求变量域当中通过`settings`辅助方法
932
+ 访问应用变量域:
933
+
934
+ class MyApp << Sinatra::Base
935
+ # 嘿,我在应用变量域!
936
+ get '/define_route/:name' do
937
+ # 针对 '/define_route/:name' 的请求变量域
938
+ @value = 42
939
+
940
+ settings.get("/#{params[:name]}") do
941
+ # 针对 "/#{params[:name]}" 的请求变量域
942
+ @value # => nil (并不是相同的请求)
943
+ end
944
+
945
+ "Route defined!"
946
+ end
947
+ end
948
+
949
+ 在以下情况将获得请求变量域:
950
+
951
+ * get/head/post/put/delete 代码块
952
+ * 前置/后置 过滤器
953
+ * 辅助方法
954
+ * 模板/视图
955
+
956
+ === 代理变量域
957
+
958
+ 代理变量域只是把方法转送到类变量域。可是,
959
+ 他并非表现得100%类似于类变量域, 因为你并不能获得类的绑定:
960
+ 只有显式地标记为供代理使用的方法才是可用的,
961
+ 而且你不能和类变量域共享变量/状态。(解释:你有了一个不同的
962
+ `self`)。 你可以显式地增加方法代理,通过调用
963
+ <tt>Sinatra::Delegator.delegate :method_name</tt>。
964
+
965
+ 在以下情况将获得代理变量域:
966
+
967
+ * 顶层的绑定,如果你做过 <tt>require "sinatra"</tt>
968
+ * 在扩展了 `Sinatra::Delegator` mixin的对象
969
+
970
+ 自己在这里看一下代码:
971
+ {Sinatra::Delegator mixin}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/base.rb#L1128]
972
+ 已经 {被包含进了主命名空间}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/main.rb#L28]。
973
+
974
+ == 命令行
975
+
976
+ Sinatra 应用可以被直接运行:
977
+
978
+ ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s HANDLER]
979
+
980
+ 选项是:
981
+
982
+ -h # help
983
+ -p # 设定端口 (默认是 4567)
984
+ -o # 设定主机名 (默认是 0.0.0.0)
985
+ -e # 设定环境 (默认是 development)
986
+ -s # 限定 rack 服务器/处理器 (默认是 thin)
987
+ -x # 打开互斥标记锁 (默认是 off)
988
+
989
+ == 紧追前沿
990
+
991
+ 如果你喜欢使用 Sinatra 的最新鲜的代码,创建一个本地克隆
992
+ , 把<tt>sinatra/lib</tt> 目录添加到
993
+ <tt>LOAD_PATH</tt>后运行你的应用:
994
+
995
+ cd myapp
996
+ git clone git://github.com/sinatra/sinatra.git
997
+ ruby -Isinatra/lib myapp.rb
998
+
999
+ 另一种方法是,你也可以在你的应用中,添加 <tt>sinatra/lib</tt> 目录到
1000
+ <tt>LOAD_PATH</tt>:
1001
+
1002
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
1003
+ require 'rubygems'
1004
+ require 'sinatra'
1005
+
1006
+ get '/about' do
1007
+ "I'm running version " + Sinatra::VERSION
1008
+ end
1009
+
1010
+ 在未来,要更新Sinatra源代码:
1011
+
1012
+ cd myproject/sinatra
1013
+ git pull
1014
+
1015
+ == 更多
1016
+
1017
+ * {项目主页(英文)}[http://www.sinatrarb.com/] - 更多的文档,
1018
+ 新闻,和其他资源的链接。
1019
+ * {贡献}[http://www.sinatrarb.com/contributing] - 找到了一个bug?
1020
+ 需要帮助?有了一个 patch?
1021
+ * {问题追踪}[http://github.com/sinatra/sinatra/issues]
1022
+ * {Twitter}[http://twitter.com/sinatra]
1023
+ * {邮件列表}[http://groups.google.com/group/sinatrarb/topics]
1024
+ * {IRC: #sinatra}[irc://chat.freenode.net/#sinatra] on http://freenode.net
data/Rakefile CHANGED
@@ -130,8 +130,8 @@ if defined?(Gem)
130
130
  gem install #{package('.gem')} --local &&
131
131
  gem push #{package('.gem')} &&
132
132
  git add sinatra.gemspec &&
133
- git commit --allow-empty -m 'Release #{source_version}' &&
134
- git -s #{source_version} -m 'Release #{source_version}' &&
133
+ git commit --allow-empty -m '#{source_version} release' &&
134
+ git tag -s #{source_version} -m '#{source_version} release' &&
135
135
  git push && (git push sinatra || true) &&
136
136
  git push --tags && (git push sinatra --tags || true)
137
137
  SH
@@ -7,7 +7,7 @@ require 'sinatra/showexceptions'
7
7
  require 'tilt'
8
8
 
9
9
  module Sinatra
10
- VERSION = '1.1.b'
10
+ VERSION = '1.1.0'
11
11
 
12
12
  # The request object. See Rack::Request for more info:
13
13
  # http://rack.rubyforge.org/doc/classes/Rack/Request.html
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = 'sinatra'
6
- s.version = '1.1.b'
7
- s.date = '2010-10-23'
6
+ s.version = '1.1.0'
7
+ s.date = '2010-10-24'
8
8
 
9
9
  s.description = "Classy web-development dressed in a DSL"
10
10
  s.summary = "Classy web-development dressed in a DSL"
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
23
23
  README.hu.rdoc
24
24
  README.jp.rdoc
25
25
  README.rdoc
26
+ README.zh.rdoc
26
27
  Rakefile
27
28
  lib/sinatra.rb
28
29
  lib/sinatra/base.rb
@@ -109,7 +110,7 @@ Gem::Specification.new do |s|
109
110
 
110
111
  s.test_files = s.files.select {|path| path =~ /^test\/.*_test.rb/}
111
112
 
112
- s.extra_rdoc_files = %w[README.rdoc README.de.rdoc README.jp.rdoc README.fr.rdoc README.es.rdoc README.hu.rdoc LICENSE]
113
+ s.extra_rdoc_files = %w[README.rdoc README.de.rdoc README.jp.rdoc README.fr.rdoc README.es.rdoc README.hu.rdoc README.zh.rdoc LICENSE]
113
114
  s.add_dependency 'rack', '~> 1.1'
114
115
  s.add_dependency 'tilt', '~> 1.1'
115
116
  s.add_development_dependency 'rake'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra
3
3
  version: !ruby/object:Gem::Version
4
- hash: 119
5
- prerelease: true
4
+ hash: 19
5
+ prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - b
10
- version: 1.1.b
9
+ - 0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Blake Mizerany
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2010-10-23 00:00:00 +02:00
21
+ date: 2010-10-24 00:00:00 +02:00
22
22
  default_executable:
23
23
  dependencies:
24
24
  - !ruby/object:Gem::Dependency
@@ -278,6 +278,7 @@ extra_rdoc_files:
278
278
  - README.fr.rdoc
279
279
  - README.es.rdoc
280
280
  - README.hu.rdoc
281
+ - README.zh.rdoc
281
282
  - LICENSE
282
283
  files:
283
284
  - AUTHORS
@@ -289,6 +290,7 @@ files:
289
290
  - README.hu.rdoc
290
291
  - README.jp.rdoc
291
292
  - README.rdoc
293
+ - README.zh.rdoc
292
294
  - Rakefile
293
295
  - lib/sinatra.rb
294
296
  - lib/sinatra/base.rb
@@ -396,14 +398,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
396
398
  required_rubygems_version: !ruby/object:Gem::Requirement
397
399
  none: false
398
400
  requirements:
399
- - - ">"
401
+ - - ">="
400
402
  - !ruby/object:Gem::Version
401
- hash: 25
403
+ hash: 3
402
404
  segments:
403
- - 1
404
- - 3
405
- - 1
406
- version: 1.3.1
405
+ - 0
406
+ version: "0"
407
407
  requirements: []
408
408
 
409
409
  rubyforge_project: sinatra