darkhelmet-sinatra 0.9.1.1 → 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +2 -0
- data/CHANGES +180 -0
- data/LICENSE +1 -1
- data/README.jp.rdoc +552 -0
- data/README.rdoc +177 -38
- data/Rakefile +18 -25
- data/lib/sinatra.rb +1 -2
- data/lib/sinatra/base.rb +405 -305
- data/lib/sinatra/main.rb +5 -24
- data/lib/sinatra/showexceptions.rb +303 -0
- data/lib/sinatra/tilt.rb +509 -0
- data/sinatra.gemspec +21 -51
- data/test/base_test.rb +123 -93
- data/test/builder_test.rb +2 -1
- data/test/contest.rb +64 -0
- data/test/erb_test.rb +1 -1
- data/test/erubis_test.rb +82 -0
- data/test/extensions_test.rb +24 -8
- data/test/filter_test.rb +99 -3
- data/test/haml_test.rb +25 -3
- data/test/helper.rb +43 -48
- data/test/helpers_test.rb +500 -424
- data/test/mapped_error_test.rb +163 -137
- data/test/middleware_test.rb +3 -3
- data/test/request_test.rb +16 -1
- data/test/response_test.rb +2 -2
- data/test/result_test.rb +1 -1
- data/test/route_added_hook_test.rb +59 -0
- data/test/routing_test.rb +170 -22
- data/test/sass_test.rb +44 -1
- data/test/server_test.rb +19 -13
- data/test/sinatra_test.rb +1 -1
- data/test/static_test.rb +9 -2
- data/test/templates_test.rb +78 -11
- data/test/views/error.builder +3 -0
- data/test/views/error.erb +3 -0
- data/test/views/error.erubis +3 -0
- data/test/views/error.haml +3 -0
- data/test/views/error.sass +2 -0
- data/test/views/foo/hello.test +1 -0
- data/test/views/hello.erubis +1 -0
- data/test/views/layout2.erubis +2 -0
- metadata +37 -55
- data/compat/app_test.rb +0 -282
- data/compat/application_test.rb +0 -262
- data/compat/builder_test.rb +0 -101
- data/compat/compat_test.rb +0 -12
- data/compat/custom_error_test.rb +0 -62
- data/compat/erb_test.rb +0 -136
- data/compat/events_test.rb +0 -78
- data/compat/filter_test.rb +0 -30
- data/compat/haml_test.rb +0 -233
- data/compat/helper.rb +0 -30
- data/compat/mapped_error_test.rb +0 -72
- data/compat/pipeline_test.rb +0 -45
- data/compat/public/foo.xml +0 -1
- data/compat/sass_test.rb +0 -57
- data/compat/sessions_test.rb +0 -42
- data/compat/streaming_test.rb +0 -133
- data/compat/sym_params_test.rb +0 -19
- data/compat/template_test.rb +0 -30
- data/compat/use_in_file_templates_test.rb +0 -47
- data/compat/views/foo.builder +0 -1
- data/compat/views/foo.erb +0 -1
- data/compat/views/foo.haml +0 -1
- data/compat/views/foo.sass +0 -2
- data/compat/views/foo_layout.erb +0 -2
- data/compat/views/foo_layout.haml +0 -2
- data/compat/views/layout_test/foo.builder +0 -1
- data/compat/views/layout_test/foo.erb +0 -1
- data/compat/views/layout_test/foo.haml +0 -1
- data/compat/views/layout_test/foo.sass +0 -2
- data/compat/views/layout_test/layout.builder +0 -3
- data/compat/views/layout_test/layout.erb +0 -1
- data/compat/views/layout_test/layout.haml +0 -1
- data/compat/views/layout_test/layout.sass +0 -2
- data/compat/views/no_layout/no_layout.builder +0 -1
- data/compat/views/no_layout/no_layout.haml +0 -1
- data/lib/sinatra/compat.rb +0 -250
- data/lib/sinatra/test.rb +0 -126
- data/lib/sinatra/test/bacon.rb +0 -19
- data/lib/sinatra/test/rspec.rb +0 -13
- data/lib/sinatra/test/spec.rb +0 -11
- data/lib/sinatra/test/unit.rb +0 -13
- data/test/data/reload_app_file.rb +0 -3
- data/test/options_test.rb +0 -374
- data/test/reload_test.rb +0 -68
- data/test/test_test.rb +0 -144
data/AUTHORS
CHANGED
@@ -10,6 +10,8 @@ Sinatra would not be possible:
|
|
10
10
|
* Ryan Tomayko (rtomayko) for constantly fixing whitespace errors 60d5006
|
11
11
|
* Ezra Zygmuntowicz (ezmobius) for initial help and letting Blake steal
|
12
12
|
some of merbs internal code.
|
13
|
+
* Ari Lerner (http://xnot.org/) for his evangelism, spirit, and gumption
|
14
|
+
that got Sinatra recognized from Day 1.
|
13
15
|
* Christopher Schneid (cschneid) for The Book, the blog (gittr.com),
|
14
16
|
irclogger.com, and a bunch of useful patches.
|
15
17
|
* Markus Prinz (cypher) for patches over the years, caring about
|
data/CHANGES
CHANGED
@@ -1,3 +1,183 @@
|
|
1
|
+
= 1.0 / unreleased
|
2
|
+
|
3
|
+
* It's now possible to register blocks to run after each request using
|
4
|
+
after filters. After filters run at the end of each request, after
|
5
|
+
routes and error handlers.
|
6
|
+
|
7
|
+
* Sinatra now uses Tilt <http://github.com/rtomayko/tilt> for rendering
|
8
|
+
templates. This adds support for template caching, consistent
|
9
|
+
template backtraces, and support for new template engines, like
|
10
|
+
mustache and liquid.
|
11
|
+
|
12
|
+
* New 'settings' method gives access to options in both class and request
|
13
|
+
scopes. This replaces the 'options' method.
|
14
|
+
|
15
|
+
* New 'erubis' helper method for rendering Erubis templates.
|
16
|
+
|
17
|
+
* New 'expires' helper method is like cache_control but takes an
|
18
|
+
integer number of seconds or Time object:
|
19
|
+
expires 300, :public, :must_revalidate
|
20
|
+
|
21
|
+
* New 'cache_control' helper method provides a convenient way of
|
22
|
+
setting the Cache-Control response header. Takes a variable number
|
23
|
+
of boolean directives followed by a hash of value directives, like
|
24
|
+
this:
|
25
|
+
cache_control :public, :must_revalidate, :max_age => 60
|
26
|
+
|
27
|
+
* Sinatra apps can now be run with a `-h <addr>` argument to specify
|
28
|
+
the address to bind to.
|
29
|
+
|
30
|
+
* Rack::Session::Cookie is now added to the middleware pipeline when
|
31
|
+
running in test environments if the :sessions option is set.
|
32
|
+
|
33
|
+
* Route handlers, before filters, templates, error mappings, and
|
34
|
+
middleware are now resolved dynamically up the inheritance hierarchy
|
35
|
+
when needed instead of duplicating the superclass's version when
|
36
|
+
a new Sinatra::Base subclass is created. This should fix a variety
|
37
|
+
of issues with extensions that need to add any of these things
|
38
|
+
to the base class.
|
39
|
+
|
40
|
+
The following Sinatra features have been obsoleted in the 1.0 release:
|
41
|
+
|
42
|
+
* The `sinatra/test` library is obsolete. This includes the
|
43
|
+
`Sinatra::Test` module, the `Sinatra::TestHarness` class,
|
44
|
+
and the `get_it`, `post_it`, `put_it`, `delete_it`, and `head_it`
|
45
|
+
helper methods. The
|
46
|
+
[`Rack::Test` library](http://gitrdoc.com/brynary/rack-test) should
|
47
|
+
be used instead.
|
48
|
+
|
49
|
+
* Test framework specific libraries (`sinatra/test/spec`,
|
50
|
+
`sinatra/test/bacon`,`sinatra/test/rspec`, etc.) are obsolete.
|
51
|
+
See http://www.sinatrarb.com/testing.html for instructions on
|
52
|
+
setting up a testing environment under each of these frameworks.
|
53
|
+
|
54
|
+
* Auto-requiring template libraries in the `erb`, `builder`, `haml`,
|
55
|
+
and `sass` methods is obsolete due to thread-safety issues. You must
|
56
|
+
require the template libraries explicitly in your app.
|
57
|
+
|
58
|
+
* The `:views_directory` option to rendering methods is obsolete; use
|
59
|
+
`:views` instead.
|
60
|
+
|
61
|
+
* The `:haml` and `:sass` options to rendering methods are obsolete.
|
62
|
+
Template engine options should be passed in the second Hash argument
|
63
|
+
instead.
|
64
|
+
|
65
|
+
* The 'media_type' helper method is obsolete. Use 'mime_type' instead.
|
66
|
+
|
67
|
+
* The request-level `send_data` method is no longer supported.
|
68
|
+
|
69
|
+
* The `Sinatra::Event` and `Sinatra::EventContext` classes are no longer
|
70
|
+
supported. This may effect extensions written for versions prior to 0.9.2.
|
71
|
+
See [Writing Sinatra Extensions](http://www.sinatrarb.com/extensions.html)
|
72
|
+
for the officially supported extensions API.
|
73
|
+
|
74
|
+
* The `set_option` and `set_options` methods are obsolete; use `set`
|
75
|
+
instead.
|
76
|
+
|
77
|
+
* The `:env` setting (`settings.env`) is obsolete; use `:environment`
|
78
|
+
instead.
|
79
|
+
|
80
|
+
* The request level `stop` method is obsolete; use `halt` instead.
|
81
|
+
|
82
|
+
* The request level `entity_tag` method is obsolete; use `etag`
|
83
|
+
instead.
|
84
|
+
|
85
|
+
* The request level `headers` method (HTTP response headers) is obsolete;
|
86
|
+
use `response['Header-Name']` instead.
|
87
|
+
|
88
|
+
* `Sinatra.application` is obsolete; use `Sinatra::Application` instead.
|
89
|
+
|
90
|
+
* Using `Sinatra.application = nil` to reset an application is obsolete.
|
91
|
+
This should no longer be necessary.
|
92
|
+
|
93
|
+
* Using `Sinatra.default_options` to set base configuration items is
|
94
|
+
obsolete; use `Sinatra::Base.set(key, value)` instead.
|
95
|
+
|
96
|
+
* The `Sinatra::ServerError` exception is obsolete. All exceptions raised
|
97
|
+
within a request are now treated as internal server errors and result in
|
98
|
+
a 500 response status.
|
99
|
+
|
100
|
+
= 0.9.2 / 2009-05-18
|
101
|
+
|
102
|
+
* This version is compatible with Rack 1.0. [Rein Henrichs]
|
103
|
+
|
104
|
+
* The development-mode unhandled exception / error page has been
|
105
|
+
greatly enhanced, functionally and aesthetically. The error
|
106
|
+
page is used when the :show_exceptions option is enabled and an
|
107
|
+
exception propagates outside of a route handler or before filter.
|
108
|
+
[Simon Rozet / Matte Noble / Ryan Tomayko]
|
109
|
+
|
110
|
+
* Backtraces that move through templates now include filenames and
|
111
|
+
line numbers where possible. [#51 / S. Brent Faulkner]
|
112
|
+
|
113
|
+
* All templates now have an app-level option for setting default
|
114
|
+
template options (:haml, :sass, :erb, :builder). The app-level
|
115
|
+
option value must be a Hash if set and is merged with the
|
116
|
+
template options specified to the render method (Base#haml,
|
117
|
+
Base#erb, Base#builder). [S. Brent Faulkner, Ryan Tomayko]
|
118
|
+
|
119
|
+
* The method signature for all template rendering methods has
|
120
|
+
been unified: "def engine(template, options={}, locals={})".
|
121
|
+
The options Hash now takes the generic :views, :layout, and
|
122
|
+
:locals options but also any template-specific options. The
|
123
|
+
generic options are removed before calling the template specific
|
124
|
+
render method. Locals may be specified using either the
|
125
|
+
:locals key in the options hash or a second Hash option to the
|
126
|
+
rendering method. [#191 / Ryan Tomayko]
|
127
|
+
|
128
|
+
* The receiver is now passed to "configure" blocks. This
|
129
|
+
allows for the following idiom in top-level apps:
|
130
|
+
configure { |app| set :foo, app.root + '/foo' }
|
131
|
+
[TJ Holowaychuck / Ryan Tomayko]
|
132
|
+
|
133
|
+
* The "sinatra/test" lib is deprecated and will be removed in
|
134
|
+
Sinatra 1.0. This includes the Sinatra::Test module and
|
135
|
+
Sinatra::TestHarness class in addition to all the framework
|
136
|
+
test helpers that were deprecated in 0.9.1. The Rack::Test
|
137
|
+
lib should be used instead: http://gitrdoc.com/brynary/rack-test
|
138
|
+
[#176 / Simon Rozet]
|
139
|
+
|
140
|
+
* Development mode source file reloading has been removed. The
|
141
|
+
"shotgun" (http://rtomayko.github.com/shotgun/) program can be
|
142
|
+
used to achieve the same basic functionality in most situations.
|
143
|
+
Passenger users should use the "tmp/always_restart.txt"
|
144
|
+
file (http://tinyurl.com/c67o4h). [#166 / Ryan Tomayko]
|
145
|
+
|
146
|
+
* Auto-requiring template libs in the erb, builder, haml, and
|
147
|
+
sass methods is deprecated due to thread-safety issues. You must
|
148
|
+
require the template libs explicitly in your app file. [Simon Rozet]
|
149
|
+
|
150
|
+
* A new Sinatra::Base#route_missing method was added. route_missing
|
151
|
+
is sent when no route matches the request or all route handlers
|
152
|
+
pass. The default implementation forwards the request to the
|
153
|
+
downstream app when running as middleware (i.e., "@app" is
|
154
|
+
non-nil), or raises a NotFound exception when no downstream app
|
155
|
+
is defined. Subclasses can override this method to perform custom
|
156
|
+
route miss logic. [Jon Crosby]
|
157
|
+
|
158
|
+
* A new Sinatra::Base#route_eval method was added. The method
|
159
|
+
yields to the block and throws :halt with the result. Subclasses
|
160
|
+
can override this method to tap into the route execution logic.
|
161
|
+
[TJ Holowaychuck]
|
162
|
+
|
163
|
+
* Fix the "-x" (enable request mutex / locking) command line
|
164
|
+
argument. Passing -x now properly sets the :lock option.
|
165
|
+
[S. Brent Faulkner, Ryan Tomayko]
|
166
|
+
|
167
|
+
* Fix writer ("foo=") and predicate ("foo?") methods in extension
|
168
|
+
modules not being added to the registering class.
|
169
|
+
[#172 / Pat Nakajima]
|
170
|
+
|
171
|
+
* Fix in-file templates when running alongside activesupport and
|
172
|
+
fatal errors when requiring activesupport before sinatra
|
173
|
+
[#178 / Brian Candler]
|
174
|
+
|
175
|
+
* Fix various issues running on Google AppEngine.
|
176
|
+
[Samuel Goebert, Simon Rozet]
|
177
|
+
|
178
|
+
* Fix in-file templates __END__ detection when __END__ exists with
|
179
|
+
other stuff on a line [Yoji Shidara]
|
180
|
+
|
1
181
|
= 0.9.1.1 / 2009-03-09
|
2
182
|
|
3
183
|
* Fix directory traversal vulnerability in default static files
|
data/LICENSE
CHANGED
data/README.jp.rdoc
ADDED
@@ -0,0 +1,552 @@
|
|
1
|
+
= Sinatra
|
2
|
+
|
3
|
+
SinatraはRubyで下記のような最小労力で手早くウェブアプリケーションを作成するためのDSLです。
|
4
|
+
|
5
|
+
# myapp.rb
|
6
|
+
require 'rubygems'
|
7
|
+
require 'sinatra'
|
8
|
+
get '/' do
|
9
|
+
'Hello world!'
|
10
|
+
end
|
11
|
+
|
12
|
+
gemをインストールして動かしてみる。
|
13
|
+
|
14
|
+
sudo gem install sinatra
|
15
|
+
ruby myapp.rb
|
16
|
+
|
17
|
+
http://localhost:4567 を見る。
|
18
|
+
|
19
|
+
== ルート
|
20
|
+
|
21
|
+
Sinatraでは、ルートはHTTPメソッドとURLマッチングパターンがペアになっています。
|
22
|
+
ルートはブロックに結び付けられています。
|
23
|
+
|
24
|
+
get '/' do
|
25
|
+
.. 何か見せる ..
|
26
|
+
end
|
27
|
+
|
28
|
+
post '/' do
|
29
|
+
.. 何か生成する ..
|
30
|
+
end
|
31
|
+
|
32
|
+
put '/' do
|
33
|
+
.. 何か更新する ..
|
34
|
+
end
|
35
|
+
|
36
|
+
delete '/' do
|
37
|
+
.. 何か削除する ..
|
38
|
+
end
|
39
|
+
|
40
|
+
ルートは定義された順番にマッチします。 リクエストに最初にマッチしたルートが呼び出されます。
|
41
|
+
|
42
|
+
ルートのパターンは名前付きパラメータを含むことができ、
|
43
|
+
<tt>params</tt>ハッシュで取得できます。
|
44
|
+
|
45
|
+
get '/hello/:name' do
|
46
|
+
# matches "GET /hello/foo" and "GET /hello/bar"
|
47
|
+
# params[:name] is 'foo' or 'bar'
|
48
|
+
"Hello #{params[:name]}!"
|
49
|
+
end
|
50
|
+
|
51
|
+
また、ブロックパラメータで名前付きパラメータにアクセスすることもできます。
|
52
|
+
|
53
|
+
get '/hello/:name' do |n|
|
54
|
+
"Hello #{n}!"
|
55
|
+
end
|
56
|
+
|
57
|
+
ルートパターンはsplat(またはワイルドカード)を含むこともでき、
|
58
|
+
<tt>params[:splat]</tt> で取得できます。
|
59
|
+
|
60
|
+
get '/say/*/to/*' do
|
61
|
+
# matches /say/hello/to/world
|
62
|
+
params[:splat] # => ["hello", "world"]
|
63
|
+
end
|
64
|
+
|
65
|
+
get '/download/*.*' do
|
66
|
+
# matches /download/path/to/file.xml
|
67
|
+
params[:splat] # => ["path/to/file", "xml"]
|
68
|
+
end
|
69
|
+
|
70
|
+
正規表現を使ったルート:
|
71
|
+
|
72
|
+
get %r{/hello/([\w]+)} do
|
73
|
+
"Hello, #{params[:captures].first}!"
|
74
|
+
end
|
75
|
+
|
76
|
+
ブロックパラーメータを使用した場合:
|
77
|
+
|
78
|
+
get %r{/hello/([\w]+)} do |c|
|
79
|
+
"Hello, #{c}!"
|
80
|
+
end
|
81
|
+
|
82
|
+
ルートにはユーザエージェントのようなさまざまな条件を含めることができます。
|
83
|
+
|
84
|
+
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
|
85
|
+
"You're using Songbird version #{params[:agent][0]}"
|
86
|
+
end
|
87
|
+
|
88
|
+
get '/foo' do
|
89
|
+
# Matches non-songbird browsers
|
90
|
+
end
|
91
|
+
|
92
|
+
== 静的ファイル
|
93
|
+
|
94
|
+
静的ファイルは<tt>./public</tt>ディレクトリから配信されます。
|
95
|
+
<tt>:public</tt>オプションを指定することで別の場所を指定することができます。
|
96
|
+
|
97
|
+
set :public, File.dirname(__FILE__) + '/static'
|
98
|
+
|
99
|
+
注意: この静的ファイル用のディレクトリ名はURL中に含まれません。
|
100
|
+
例えば、<tt>./public/css/style.css</tt>は<tt>http://example.com/css/style.css</tt>でアクセスできます。
|
101
|
+
|
102
|
+
== ビュー / テンプレート
|
103
|
+
|
104
|
+
テンプレートは<tt>./views</tt>ディレクトリ下に配置されています。
|
105
|
+
他のディレクトリを使用する場合の例:
|
106
|
+
|
107
|
+
set :views, File.dirname(__FILE__) + '/templates'
|
108
|
+
|
109
|
+
テンプレートはシンボルを使用して参照させることを覚えておいて下さい。
|
110
|
+
サブデレクトリでもこの場合は<tt>:'subdir/template'</tt>のようにします。
|
111
|
+
レンダリングメソッドは文字列が渡されると、そのまま文字列を出力します。
|
112
|
+
|
113
|
+
=== Haml テンプレート
|
114
|
+
|
115
|
+
hamlを使うにはhamlライブラリが必要です:
|
116
|
+
|
117
|
+
## hamlを読み込みます
|
118
|
+
require 'haml'
|
119
|
+
|
120
|
+
get '/' do
|
121
|
+
haml :index
|
122
|
+
end
|
123
|
+
|
124
|
+
<tt>./views/index.haml</tt>を表示します。
|
125
|
+
|
126
|
+
{Haml's options}[http://haml.hamptoncatlin.com/docs/rdoc/classes/Haml.html]
|
127
|
+
はSinatraの設定でグローバルに設定することができます。
|
128
|
+
{Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
129
|
+
を参照してそれぞれ設定を上書きして下さい。
|
130
|
+
|
131
|
+
set :haml, {:format => :html5 } # デフォルトのフォーマットは:xhtml
|
132
|
+
|
133
|
+
get '/' do
|
134
|
+
haml :index, :haml_options => {:format => :html4 } # 上書き
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
=== Erb テンプレート
|
139
|
+
|
140
|
+
## erbを読み込みます
|
141
|
+
require 'erb'
|
142
|
+
|
143
|
+
get '/' do
|
144
|
+
erb :index
|
145
|
+
end
|
146
|
+
|
147
|
+
<tt>./views/index.erb</tt>を表示します。
|
148
|
+
|
149
|
+
=== Builder テンプレート
|
150
|
+
|
151
|
+
builderを使うにはbuilderライブラリが必要です:
|
152
|
+
|
153
|
+
## builderを読み込みます
|
154
|
+
require 'builder'
|
155
|
+
|
156
|
+
get '/' do
|
157
|
+
content_type 'application/xml', :charset => 'utf-8'
|
158
|
+
builder :index
|
159
|
+
end
|
160
|
+
|
161
|
+
<tt>./views/index.builder</tt>を表示します。
|
162
|
+
|
163
|
+
=== Sass テンプレート
|
164
|
+
|
165
|
+
Sassテンプレートを使うにはsassライブラリが必要です:
|
166
|
+
|
167
|
+
## hamlかsassを読み込みます
|
168
|
+
require 'sass'
|
169
|
+
|
170
|
+
get '/stylesheet.css' do
|
171
|
+
content_type 'text/css', :charset => 'utf-8'
|
172
|
+
sass :stylesheet
|
173
|
+
end
|
174
|
+
|
175
|
+
<tt>./views/stylesheet.sass</tt>を表示します。
|
176
|
+
|
177
|
+
{Sass' options}[http://haml.hamptoncatlin.com/docs/rdoc/classes/Sass.html]
|
178
|
+
はSinatraの設定でグローバルに設定することができます。
|
179
|
+
see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
180
|
+
を参照してそれぞれ設定を上書きして下さい。
|
181
|
+
|
182
|
+
set :sass, {:style => :compact } # デフォルトのSass styleは :nested
|
183
|
+
|
184
|
+
get '/stylesheet.css' do
|
185
|
+
content_type 'text/css', :charset => 'utf-8'
|
186
|
+
sass :stylesheet, :sass_options => {:style => :expanded } # 上書き
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
=== インラインテンプレート
|
191
|
+
|
192
|
+
get '/' do
|
193
|
+
haml '%div.title Hello World'
|
194
|
+
end
|
195
|
+
|
196
|
+
文字列をテンプレートとして表示します。
|
197
|
+
|
198
|
+
=== テンプレート内で変数にアクセスする
|
199
|
+
|
200
|
+
テンプレートはルートハンドラと同じコンテキストの中で評価されます。. ルートハンドラでセットされたインスタンス変数は
|
201
|
+
テンプレート内で直接使うことができます。
|
202
|
+
|
203
|
+
get '/:id' do
|
204
|
+
@foo = Foo.find(params[:id])
|
205
|
+
haml '%h1= @foo.name'
|
206
|
+
end
|
207
|
+
|
208
|
+
ローカル変数を明示的に定義することもできます。
|
209
|
+
|
210
|
+
get '/:id' do
|
211
|
+
foo = Foo.find(params[:id])
|
212
|
+
haml '%h1= foo.name', :locals => { :foo => foo }
|
213
|
+
end
|
214
|
+
|
215
|
+
このやり方は他のテンプレート内で部分テンプレートとして表示する時に典型的に使用されます。
|
216
|
+
|
217
|
+
=== ファイル内テンプレート
|
218
|
+
|
219
|
+
テンプレートはソースファイルの最後で定義することもできます。
|
220
|
+
|
221
|
+
require 'rubygems'
|
222
|
+
require 'sinatra'
|
223
|
+
|
224
|
+
get '/' do
|
225
|
+
haml :index
|
226
|
+
end
|
227
|
+
|
228
|
+
__END__
|
229
|
+
|
230
|
+
@@ layout
|
231
|
+
%html
|
232
|
+
= yield
|
233
|
+
|
234
|
+
@@ index
|
235
|
+
%div.title Hello world!!!!!
|
236
|
+
|
237
|
+
注意: sinatraをrequireするファイル内で定義されたファイル内テンプレートは自動的に読み込まれます。
|
238
|
+
他のファイルで定義されているテンプレートを使うには <tt>use_in_file_templates!</tt>メソッドで指定します。
|
239
|
+
|
240
|
+
=== 名前付きテンプレート
|
241
|
+
|
242
|
+
テンプレートはトップレベルの<tt>template</tt>メソッドで定義することができます。
|
243
|
+
|
244
|
+
template :layout do
|
245
|
+
"%html\n =yield\n"
|
246
|
+
end
|
247
|
+
|
248
|
+
template :index do
|
249
|
+
'%div.title Hello World!'
|
250
|
+
end
|
251
|
+
|
252
|
+
get '/' do
|
253
|
+
haml :index
|
254
|
+
end
|
255
|
+
|
256
|
+
「layout」というテンプレートが存在する場合、そのテンプレートファイルは他のテンプレートが
|
257
|
+
表示される度に使用されます。<tt>:layout => false</tt>.することでlayoutsを無効にできます。
|
258
|
+
|
259
|
+
get '/' do
|
260
|
+
haml :index, :layout => !request.xhr?
|
261
|
+
end
|
262
|
+
|
263
|
+
== ヘルパー
|
264
|
+
|
265
|
+
トップレベルの<tt>helpers</tt>を使用してルートハンドラやテンプレートで使うヘルパメソッドを
|
266
|
+
定義できます。
|
267
|
+
|
268
|
+
helpers do
|
269
|
+
def bar(name)
|
270
|
+
"#{name}bar"
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
get '/:name' do
|
275
|
+
bar(params[:name])
|
276
|
+
end
|
277
|
+
|
278
|
+
== フィルタ
|
279
|
+
|
280
|
+
beforeフィルタはリクエストされたコンテキストを実行する前に評価され、
|
281
|
+
リクエストとレスポンスを変更することができます。フィルタ内でセットされた
|
282
|
+
インスタンス変数はルーティングとテンプレートで使用できます。
|
283
|
+
|
284
|
+
before do
|
285
|
+
@note = 'Hi!'
|
286
|
+
request.path_info = '/foo/bar/baz'
|
287
|
+
end
|
288
|
+
|
289
|
+
get '/foo/*' do
|
290
|
+
@note #=> 'Hi!'
|
291
|
+
params[:splat] #=> 'bar/baz'
|
292
|
+
end
|
293
|
+
|
294
|
+
== 強制終了
|
295
|
+
|
296
|
+
ルートかbeforeフィルタ内で直ちに実行を終了する方法:
|
297
|
+
|
298
|
+
halt
|
299
|
+
|
300
|
+
body部を指定することもできます ...
|
301
|
+
|
302
|
+
halt 'ここにbodyを書く'
|
303
|
+
|
304
|
+
ステータスとbody部を指定する ...
|
305
|
+
|
306
|
+
halt 401, '立ち去れ!'
|
307
|
+
|
308
|
+
== パッシング(Passing)
|
309
|
+
|
310
|
+
ルートは<tt>pass</tt>を使って次のルートに飛ばすことができます:
|
311
|
+
|
312
|
+
get '/guess/:who' do
|
313
|
+
pass unless params[:who] == 'Frank'
|
314
|
+
"見つかっちゃった!"
|
315
|
+
end
|
316
|
+
|
317
|
+
get '/guess/*' do
|
318
|
+
"はずれです!"
|
319
|
+
end
|
320
|
+
|
321
|
+
ルートブロックからすぐに抜け出し、次にマッチするルートを実行します。
|
322
|
+
マッチするルートが見当たらない場合は404が返されます。
|
323
|
+
|
324
|
+
== 設定
|
325
|
+
|
326
|
+
どの環境でも起動時に1回だけ実行されます。
|
327
|
+
|
328
|
+
configure do
|
329
|
+
...
|
330
|
+
end
|
331
|
+
|
332
|
+
環境変数<tt>:production</tt>(RACK_ENV環境変数) がセットされている時だけ実行する方法:
|
333
|
+
|
334
|
+
configure :production do
|
335
|
+
...
|
336
|
+
end
|
337
|
+
|
338
|
+
環境変数<tt>:production</tt> か<tt>:test</tt>の場合に設定する方法:
|
339
|
+
|
340
|
+
configure :production, :test do
|
341
|
+
...
|
342
|
+
end
|
343
|
+
|
344
|
+
== エラーハンドリング
|
345
|
+
|
346
|
+
エラーハンドラーはルートコンテキストとbeforeフィルタ内で実行します。
|
347
|
+
<tt>haml</tt>、<tt>erb</tt>、<tt>halt</tt>などを使うこともできます。
|
348
|
+
|
349
|
+
=== Not Found
|
350
|
+
|
351
|
+
<tt>Sinatra::NotFound</tt>が起きた時か レスポンスのステータスコードが
|
352
|
+
404の時に<tt>not_found</tt>ハンドラーが発動します。
|
353
|
+
|
354
|
+
not_found do
|
355
|
+
'ファイルが存在しません'
|
356
|
+
end
|
357
|
+
|
358
|
+
=== エラー
|
359
|
+
|
360
|
+
+error+ ハンドラーはルートブロックかbeforeフィルタ内で例外が発生した時はいつでも発動します。
|
361
|
+
block or before filter. 例外オブジェクトはRack変数<tt>sinatra.error</tt>から取得されます。
|
362
|
+
|
363
|
+
error do
|
364
|
+
'エラーが発生しました。 - ' + env['sinatra.error'].name
|
365
|
+
end
|
366
|
+
|
367
|
+
エラーをカスタマイズする場合は、
|
368
|
+
|
369
|
+
error MyCustomError do
|
370
|
+
'エラーメッセージ...' + request.env['sinatra.error'].message
|
371
|
+
end
|
372
|
+
|
373
|
+
と書いておいて,下記のように呼び出します。
|
374
|
+
|
375
|
+
get '/' do
|
376
|
+
raise MyCustomError, '何かがまずかったようです'
|
377
|
+
end
|
378
|
+
|
379
|
+
そうするとこうなります:
|
380
|
+
|
381
|
+
エラーメッセージ... 何かがまずかったようです
|
382
|
+
|
383
|
+
開発環境として実行している場合、Sinatraは特別な<tt>not_found</tt>と<tt>error</tt>ハンドラーを
|
384
|
+
インストールしています。
|
385
|
+
|
386
|
+
== MIMEタイプ
|
387
|
+
|
388
|
+
<tt>send_file</tt>か静的ファイルを使う時、Sinatraが理解でいないMIMEタイプがある場合があります。
|
389
|
+
その時は +mime_type+ を使ってファイル拡張子毎に登録して下さい。
|
390
|
+
|
391
|
+
mime_type :foo, 'text/foo'
|
392
|
+
|
393
|
+
== Rackミドルウェア
|
394
|
+
|
395
|
+
SinatraはRack[http://rack.rubyforge.org/]というRubyのWEBフレームワーク用の
|
396
|
+
最小限の標準インターフェース 上で動作しています。Rack中でもアプリケーションデベロッパー
|
397
|
+
向けに一番興味深い機能はミドルウェア(サーバとアプリケーション間に介在し、モニタリング、HTTPリクエストとレスポンス
|
398
|
+
の手動操作ができるなど、一般的な機能のいろいろなことを提供するもの)をサポートすることです。
|
399
|
+
|
400
|
+
Sinatraではトップレベルの+user+ メソッドを使ってRackにパイプラインを構築します。
|
401
|
+
|
402
|
+
require 'sinatra'
|
403
|
+
require 'my_custom_middleware'
|
404
|
+
|
405
|
+
use Rack::Lint
|
406
|
+
use MyCustomMiddleware
|
407
|
+
|
408
|
+
get '/hello' do
|
409
|
+
'Hello World'
|
410
|
+
end
|
411
|
+
|
412
|
+
<tt>use</tt> の意味は{Rack::Builder}[http://rack.rubyforge.org/doc/classes/Rack/Builder.html] DSLで定義されていることと全て一致します。
|
413
|
+
例えば +use+ メソッドはブロック構文のように複数の引数を受け取ることができます。
|
414
|
+
|
415
|
+
use Rack::Auth::Basic do |username, password|
|
416
|
+
username == 'admin' && password == 'secret'
|
417
|
+
end
|
418
|
+
|
419
|
+
Rackはログ、デバッギング、URLルーティング、認証、セッションなどいろいろな機能を備えた標準的ミドルウェアです。
|
420
|
+
Sinatraはその多くのコンポーネントを自動で使うよう基本設定されているため、+use+で明示的に指定する必要はありません。
|
421
|
+
|
422
|
+
== テスト
|
423
|
+
|
424
|
+
SinatraでのテストはRack-basedのテストライブラリかフレームワークを使って書くことができます。
|
425
|
+
{Rack::Test}[http://gitrdoc.com/brynary/rack-test] をおすすめします。やり方:
|
426
|
+
|
427
|
+
require 'my_sinatra_app'
|
428
|
+
require 'rack/test'
|
429
|
+
|
430
|
+
class MyAppTest < Test::Unit::TestCase
|
431
|
+
include Rack::Test::Methods
|
432
|
+
|
433
|
+
def app
|
434
|
+
Sinatra::Application
|
435
|
+
end
|
436
|
+
|
437
|
+
def test_my_default
|
438
|
+
get '/'
|
439
|
+
assert_equal 'Hello World!', last_response.body
|
440
|
+
end
|
441
|
+
|
442
|
+
def test_with_params
|
443
|
+
get '/meet', :name => 'Frank'
|
444
|
+
assert_equal 'Hello Frank!', last_response.body
|
445
|
+
end
|
446
|
+
|
447
|
+
def test_with_rack_env
|
448
|
+
get '/', {}, 'HTTP_USER_AGENT' => 'Songbird'
|
449
|
+
assert_equal "あなたはSongbirdを使ってますね!", last_response.body
|
450
|
+
end
|
451
|
+
end
|
452
|
+
|
453
|
+
注意: ビルトインのSinatra::TestモジュールとSinatra::TestHarnessクラスは
|
454
|
+
0.9.2リリース以降、廃止予定になっています。
|
455
|
+
|
456
|
+
== Sinatra::Base - ミドルウェア、ライブラリ、 モジュラーアプリ
|
457
|
+
|
458
|
+
トップレベル(グローバル領域)上でいろいろ定義していくのは軽量アプリならうまくいきますが、
|
459
|
+
RackミドルウェアやRails metal、サーバのコンポーネントを含んだシンプルな
|
460
|
+
ライブラリやSinatraの拡張プログラムを考慮するような場合はそうとは限りません。
|
461
|
+
トップレベルのDSLがネームスペースを汚染したり、設定を変えてしまうこと(例:./publicや./view)がありえます。
|
462
|
+
そこでSinatra::Baseの出番です。
|
463
|
+
|
464
|
+
require 'sinatra/base'
|
465
|
+
|
466
|
+
class MyApp < Sinatra::Base
|
467
|
+
set :sessions, true
|
468
|
+
set :foo, 'bar'
|
469
|
+
|
470
|
+
get '/' do
|
471
|
+
'Hello world!'
|
472
|
+
end
|
473
|
+
end
|
474
|
+
|
475
|
+
このMyAppは独立したRackコンポーネントで、RackミドルウェアやRackアプリケーション
|
476
|
+
Rails metalとして使用することができます。<tt>config.ru</tt>ファイル内で +use+ か、または
|
477
|
+
+run+ でこのクラスを指定するか、ライブラリとしてサーバコンポーネントをコントロールします。
|
478
|
+
|
479
|
+
MyApp.run! :host => 'localhost', :port => 9090
|
480
|
+
|
481
|
+
Sinatra::Baseのサブクラスで使えるメソッドはトップレベルのDSLを経由して確実に使うことができます。
|
482
|
+
ほとんどのトップレベルで記述されたアプリは、以下の2点を修正することでSinatra::Baseコンポーネントに変えることができます。
|
483
|
+
|
484
|
+
* +sinatra+の代わりに<tt>sinatra/base</tt>を読み込む
|
485
|
+
(そうしない場合、SinatraのDSLメソッドの全てがメインネームスペースにインポートされます)
|
486
|
+
* ルート、エラーハンドラー、フィルター、オプションをSinatra::Baseのサブクラスに書く
|
487
|
+
|
488
|
+
<tt>Sinatra::Base</tt> はまっさらです。ビルトインサーバを含む、ほとんどのオプションがデフォルト
|
489
|
+
で無効になっています。オプション詳細については{Options and Configuration}[http://sinatra.github.com/configuration.html]
|
490
|
+
をご覧下さい。
|
491
|
+
|
492
|
+
補足: SinatraのトップレベルDSLはシンプルな委譲(delgation)システムで実装されています。
|
493
|
+
<tt>Sinatra::Application</tt>クラス(Sinatra::Baseの特別なサブクラス)は、トップレベルに送られる
|
494
|
+
:get、 :put、 :post、:delete、 :before、:error、:not_found、 :configure、:set messagesのこれら
|
495
|
+
全てを受け取ります。 詳細を閲覧されたい方はこちら(英語):
|
496
|
+
{Sinatra::Delegator mixin}[http://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1064]
|
497
|
+
{included into the main namespace}[http://github.com/sinatra/sinatra/blob/master/lib/sinatra/main.rb#L25].
|
498
|
+
|
499
|
+
== コマンドライン
|
500
|
+
|
501
|
+
Sinatraアプリケーションは直接実行できます。
|
502
|
+
|
503
|
+
ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-s HANDLER]
|
504
|
+
|
505
|
+
オプション:
|
506
|
+
|
507
|
+
-h # ヘルプ
|
508
|
+
-p # ポート指定(デフォルトは4567)
|
509
|
+
-e # 環境を指定 (デフォルトはdevelopment)
|
510
|
+
-s # rackserver/handlerを指定 (デフォルトはthin)
|
511
|
+
-x # mutex lockを付ける (デフォルトはoff)
|
512
|
+
|
513
|
+
== 最新開発版について
|
514
|
+
|
515
|
+
Sinatraの開発版を使いたい場合は、ローカルに開発版を落として、
|
516
|
+
<tt>LOAD_PATH</tt>の<tt>sinatra/lib</tt>ディレクトリを指定して実行して下さい。
|
517
|
+
|
518
|
+
cd myapp
|
519
|
+
git clone git://github.com/sinatra/sinatra.git
|
520
|
+
ruby -Isinatra/lib myapp.rb
|
521
|
+
|
522
|
+
<tt>sinatra/lib</tt>ディレクトリをto the<tt>LOAD_PATH</tt>に追加する方法もあります。
|
523
|
+
|
524
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
|
525
|
+
require 'rubygems'
|
526
|
+
require 'sinatra'
|
527
|
+
|
528
|
+
get '/about' do
|
529
|
+
"今使ってるバージョンは" + Sinatra::VERSION
|
530
|
+
end
|
531
|
+
|
532
|
+
Sinatraのソースを更新する方法:
|
533
|
+
|
534
|
+
cd myproject/sinatra
|
535
|
+
git pull
|
536
|
+
|
537
|
+
== その他
|
538
|
+
|
539
|
+
日本語サイト
|
540
|
+
|
541
|
+
* {Greenbear Laboratory Rack日本語マニュアル}[http://mono.kmc.gr.jp/~yhara/w/?RackReferenceJa] - Rackの日本語マニュアル
|
542
|
+
|
543
|
+
英語サイト
|
544
|
+
|
545
|
+
* {プロジェクトサイト}[http://sinatra.github.com/] - ドキュメント、
|
546
|
+
ニュース、他のリソースへのリンクがあります。
|
547
|
+
* {プロジェクトに参加(貢献)する}[http://sinatra.github.com/contributing.html] - バグレポート
|
548
|
+
パッチの送信、サポートなど
|
549
|
+
* {Lighthouse}[http://sinatra.lighthouseapp.com] - チケット管理とリリース計画
|
550
|
+
* {Twitter}[http://twitter.com/sinatra]
|
551
|
+
* {メーリングリスト}[http://groups.google.com/group/sinatrarb]
|
552
|
+
* {IRC: #sinatra}[irc://chat.freenode.net/#sinatra] on http://freenode.net
|