renee 0.3.11 → 0.4.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +17 -0
- data/Gemfile-renee +8 -0
- data/Gemfile-renee-core +8 -0
- data/Gemfile-renee-render +9 -0
- data/Gemfile-renee-session +9 -0
- data/Gemfile-renee-url-generation +8 -0
- data/MIT-LICENSE.txt +7 -0
- data/README-renee-core.md +242 -0
- data/README-renee-render.md +38 -0
- data/README-renee-session.md +3 -0
- data/README-renee-url-generation.md +3 -0
- data/README.md +131 -6
- data/Rakefile +109 -9
- data/TODO.txt +45 -0
- data/config.ru +26 -0
- data/examples/blog/blog.rb +3 -1
- data/examples/blog/config.ru +24 -19
- data/examples/blog/views/edit.erb +10 -1
- data/examples/blog/views/show.erb +5 -0
- data/lib/renee.rb +11 -4
- data/lib/renee/core.rb +98 -0
- data/lib/renee/core/chaining.rb +66 -0
- data/lib/renee/core/env_accessors.rb +72 -0
- data/lib/renee/core/exceptions.rb +15 -0
- data/lib/renee/core/matcher.rb +61 -0
- data/lib/renee/core/plugins.rb +31 -0
- data/lib/renee/core/rack_interaction.rb +50 -0
- data/lib/renee/core/request_context.rb +56 -0
- data/lib/renee/core/responding.rb +112 -0
- data/lib/renee/core/response.rb +78 -0
- data/lib/renee/core/routing.rb +319 -0
- data/lib/renee/core/transform.rb +18 -0
- data/lib/renee/render.rb +221 -0
- data/lib/renee/session.rb +50 -0
- data/lib/renee/url_generation.rb +117 -0
- data/lib/renee/util.rb +7 -0
- data/lib/renee/version.rb +2 -4
- data/plan.txt +19 -0
- data/renee-core.gemspec +26 -0
- data/renee-render.gemspec +30 -0
- data/renee-session.gemspec +28 -0
- data/renee-url-generation.gemspec +24 -0
- data/renee.gemspec +5 -6
- data/site/MIT-LICENSE.txt +7 -0
- data/site/public/css/app.css +75 -0
- data/site/public/docs/renee-core/Renee.html +208 -0
- data/site/public/docs/renee-core/Renee/Core.html +366 -0
- data/site/public/docs/renee-core/Renee/Core/Chaining.html +192 -0
- data/site/public/docs/renee-core/Renee/Core/ClassMethods.html +725 -0
- data/site/public/docs/renee-core/Renee/Core/ClientError.html +317 -0
- data/site/public/docs/renee-core/Renee/Core/EnvAccessors.html +152 -0
- data/site/public/docs/renee-core/Renee/Core/EnvAccessors/ClassMethods.html +354 -0
- data/site/public/docs/renee-core/Renee/Core/Matcher.html +675 -0
- data/site/public/docs/renee-core/Renee/Core/Plugins.html +475 -0
- data/site/public/docs/renee-core/Renee/Core/RackInteraction.html +488 -0
- data/site/public/docs/renee-core/Renee/Core/RequestContext.html +511 -0
- data/site/public/docs/renee-core/Renee/Core/Responding.html +877 -0
- data/site/public/docs/renee-core/Renee/Core/Response.html +691 -0
- data/site/public/docs/renee-core/Renee/Core/Routing.html +1589 -0
- data/site/public/docs/renee-core/Renee/Core/Transform.html +249 -0
- data/site/public/docs/renee-core/Renee/Core/URLGeneration.html +597 -0
- data/site/public/docs/renee-core/_index.html +244 -0
- data/site/public/docs/renee-core/class_list.html +47 -0
- data/site/public/docs/renee-core/css/common.css +1 -0
- data/site/public/docs/renee-core/css/full_list.css +55 -0
- data/site/public/docs/renee-core/css/style.css +322 -0
- data/site/public/docs/renee-core/file.README-renee-core.html +341 -0
- data/site/public/docs/renee-core/file.README.html +212 -0
- data/site/public/docs/renee-core/file_list.html +49 -0
- data/site/public/docs/renee-core/frames.html +13 -0
- data/site/public/docs/renee-core/index.html +341 -0
- data/site/public/docs/renee-core/js/app.js +205 -0
- data/site/public/docs/renee-core/js/full_list.js +167 -0
- data/site/public/docs/renee-core/js/jquery.js +16 -0
- data/site/public/docs/renee-core/method_list.html +590 -0
- data/site/public/docs/renee-core/top-level-namespace.html +103 -0
- data/site/public/docs/renee-render/Renee.html +116 -0
- data/site/public/docs/renee-render/Renee/Core.html +346 -0
- data/site/public/docs/renee-render/Renee/Core/Chaining.html +125 -0
- data/site/public/docs/renee-render/Renee/Core/ClassMethods.html +620 -0
- data/site/public/docs/renee-render/Renee/Core/ClientError.html +317 -0
- data/site/public/docs/renee-render/Renee/Core/EnvAccessors.html +152 -0
- data/site/public/docs/renee-render/Renee/Core/EnvAccessors/ClassMethods.html +354 -0
- data/site/public/docs/renee-render/Renee/Core/Matcher.html +675 -0
- data/site/public/docs/renee-render/Renee/Core/RackInteraction.html +488 -0
- data/site/public/docs/renee-render/Renee/Core/RequestContext.html +421 -0
- data/site/public/docs/renee-render/Renee/Core/Responding.html +873 -0
- data/site/public/docs/renee-render/Renee/Core/Response.html +691 -0
- data/site/public/docs/renee-render/Renee/Core/Routing.html +1682 -0
- data/site/public/docs/renee-render/Renee/Core/Transform.html +249 -0
- data/site/public/docs/renee-render/Renee/Core/URLGeneration.html +597 -0
- data/site/public/docs/renee-render/Renee/Render.html +873 -0
- data/site/public/docs/renee-render/Renee/Render/ClassMethods.html +382 -0
- data/site/public/docs/renee-render/Renee/Render/TemplateNotFound.html +126 -0
- data/site/public/docs/renee-render/_index.html +143 -0
- data/site/public/docs/renee-render/class_list.html +47 -0
- data/site/public/docs/renee-render/css/common.css +1 -0
- data/site/public/docs/renee-render/css/full_list.css +55 -0
- data/site/public/docs/renee-render/css/style.css +322 -0
- data/site/public/docs/renee-render/file.README-renee-render.html +104 -0
- data/site/public/docs/renee-render/file.README.html +212 -0
- data/site/public/docs/renee-render/file_list.html +49 -0
- data/site/public/docs/renee-render/frames.html +13 -0
- data/site/public/docs/renee-render/index.html +104 -0
- data/site/public/docs/renee-render/js/app.js +205 -0
- data/site/public/docs/renee-render/js/full_list.js +167 -0
- data/site/public/docs/renee-render/js/jquery.js +16 -0
- data/site/public/docs/renee-render/method_list.html +110 -0
- data/site/public/docs/renee-render/top-level-namespace.html +103 -0
- data/site/public/docs/renee-session/Renee.html +106 -0
- data/site/public/docs/renee-session/Renee/Session.html +173 -0
- data/site/public/docs/renee-session/Renee/Session/ClassMethods.html +470 -0
- data/site/public/docs/renee-session/_index.html +136 -0
- data/site/public/docs/renee-session/class_list.html +47 -0
- data/site/public/docs/renee-session/css/common.css +1 -0
- data/site/public/docs/renee-session/css/full_list.css +55 -0
- data/site/public/docs/renee-session/css/style.css +322 -0
- data/site/public/docs/renee-session/file.README-renee-core.html +341 -0
- data/site/public/docs/renee-session/file.README-renee-session.html +69 -0
- data/site/public/docs/renee-session/file_list.html +49 -0
- data/site/public/docs/renee-session/frames.html +13 -0
- data/site/public/docs/renee-session/index.html +69 -0
- data/site/public/docs/renee-session/js/app.js +205 -0
- data/site/public/docs/renee-session/js/full_list.js +167 -0
- data/site/public/docs/renee-session/js/jquery.js +16 -0
- data/site/public/docs/renee-session/method_list.html +102 -0
- data/site/public/docs/renee-session/top-level-namespace.html +103 -0
- data/site/public/docs/renee-url-generation/Renee.html +208 -0
- data/site/public/docs/renee-url-generation/Renee/Core.html +366 -0
- data/site/public/docs/renee-url-generation/Renee/Core/Chaining.html +192 -0
- data/site/public/docs/renee-url-generation/Renee/Core/ClassMethods.html +725 -0
- data/site/public/docs/renee-url-generation/Renee/Core/ClientError.html +317 -0
- data/site/public/docs/renee-url-generation/Renee/Core/EnvAccessors.html +152 -0
- data/site/public/docs/renee-url-generation/Renee/Core/EnvAccessors/ClassMethods.html +354 -0
- data/site/public/docs/renee-url-generation/Renee/Core/Matcher.html +675 -0
- data/site/public/docs/renee-url-generation/Renee/Core/Plugins.html +475 -0
- data/site/public/docs/renee-url-generation/Renee/Core/RackInteraction.html +488 -0
- data/site/public/docs/renee-url-generation/Renee/Core/RequestContext.html +511 -0
- data/site/public/docs/renee-url-generation/Renee/Core/Responding.html +877 -0
- data/site/public/docs/renee-url-generation/Renee/Core/Response.html +691 -0
- data/site/public/docs/renee-url-generation/Renee/Core/Routing.html +1589 -0
- data/site/public/docs/renee-url-generation/Renee/Core/Transform.html +249 -0
- data/site/public/docs/renee-url-generation/_index.html +244 -0
- data/site/public/docs/renee-url-generation/class_list.html +47 -0
- data/site/public/docs/renee-url-generation/css/common.css +1 -0
- data/site/public/docs/renee-url-generation/css/full_list.css +55 -0
- data/site/public/docs/renee-url-generation/css/style.css +322 -0
- data/site/public/docs/renee-url-generation/file.README-renee-url-generation.html +69 -0
- data/site/public/docs/renee-url-generation/file_list.html +49 -0
- data/site/public/docs/renee-url-generation/frames.html +13 -0
- data/site/public/docs/renee-url-generation/index.html +69 -0
- data/site/public/docs/renee-url-generation/js/app.js +205 -0
- data/site/public/docs/renee-url-generation/js/full_list.js +167 -0
- data/site/public/docs/renee-url-generation/js/jquery.js +16 -0
- data/site/public/docs/renee-url-generation/method_list.html +590 -0
- data/site/public/docs/renee-url-generation/top-level-namespace.html +103 -0
- data/site/public/docs/renee/Renee.html +232 -0
- data/site/public/docs/renee/Renee/Application.html +367 -0
- data/site/public/docs/renee/Renee/Core.html +370 -0
- data/site/public/docs/renee/Renee/Core/Chaining.html +192 -0
- data/site/public/docs/renee/Renee/Core/ClassMethods.html +725 -0
- data/site/public/docs/renee/Renee/Core/ClientError.html +317 -0
- data/site/public/docs/renee/Renee/Core/EnvAccessors.html +152 -0
- data/site/public/docs/renee/Renee/Core/EnvAccessors/ClassMethods.html +354 -0
- data/site/public/docs/renee/Renee/Core/Matcher.html +675 -0
- data/site/public/docs/renee/Renee/Core/Plugins.html +475 -0
- data/site/public/docs/renee/Renee/Core/RackInteraction.html +488 -0
- data/site/public/docs/renee/Renee/Core/RequestContext.html +511 -0
- data/site/public/docs/renee/Renee/Core/Responding.html +877 -0
- data/site/public/docs/renee/Renee/Core/Response.html +691 -0
- data/site/public/docs/renee/Renee/Core/Routing.html +1589 -0
- data/site/public/docs/renee/Renee/Core/Transform.html +249 -0
- data/site/public/docs/renee/Renee/Core/URLGeneration.html +597 -0
- data/site/public/docs/renee/Renee/Render.html +877 -0
- data/site/public/docs/renee/Renee/Render/ClassMethods.html +382 -0
- data/site/public/docs/renee/Renee/Render/TemplateNotFound.html +126 -0
- data/site/public/docs/renee/Renee/Session.html +177 -0
- data/site/public/docs/renee/Renee/Session/ClassMethods.html +470 -0
- data/site/public/docs/renee/Renee/URLGeneration.html +142 -0
- data/site/public/docs/renee/Renee/URLGeneration/ClassMethods.html +593 -0
- data/site/public/docs/renee/Renee/Util.html +163 -0
- data/site/public/docs/renee/_index.html +336 -0
- data/site/public/docs/renee/class_list.html +47 -0
- data/site/public/docs/renee/css/common.css +1 -0
- data/site/public/docs/renee/css/full_list.css +55 -0
- data/site/public/docs/renee/css/style.css +322 -0
- data/site/public/docs/renee/file.README.html +212 -0
- data/site/public/docs/renee/file_list.html +49 -0
- data/site/public/docs/renee/frames.html +13 -0
- data/site/public/docs/renee/index.html +212 -0
- data/site/public/docs/renee/js/app.js +205 -0
- data/site/public/docs/renee/js/full_list.js +167 -0
- data/site/public/docs/renee/js/jquery.js +16 -0
- data/site/public/docs/renee/method_list.html +758 -0
- data/site/public/docs/renee/top-level-namespace.html +202 -0
- data/site/public/img/favicon.ico +0 -0
- data/site/public/img/reneeclean.png +0 -0
- data/site/public/img/russiangithub.png +0 -0
- data/site/public/img/stoneposter.png +0 -0
- data/site/public/img/vospit.jpeg +0 -0
- data/site/views/chaining.md +32 -0
- data/site/views/index.md +219 -0
- data/site/views/layouts/app.haml +16 -0
- data/site/views/rack-integration.md +51 -0
- data/site/views/responding.md +103 -0
- data/site/views/route-generation.md +82 -0
- data/site/views/routing.md +261 -0
- data/site/views/settings.md +19 -0
- data/site/views/team-renee.md +13 -0
- data/site/views/tutorial.md +57 -0
- data/site/views/variable-types.md +57 -0
- data/test.watchr +61 -0
- data/test/renee-core/chaining_test.rb +33 -0
- data/test/renee-core/env_accessors_test.rb +43 -0
- data/test/renee-core/include_test.rb +14 -0
- data/test/renee-core/request_context_test.rb +70 -0
- data/test/renee-core/responding_test.rb +128 -0
- data/test/renee-core/routing_test.rb +443 -0
- data/test/renee-core/test_helper.rb +4 -0
- data/test/renee-core/variable_type_test.rb +57 -0
- data/test/renee-render/render_test.rb +162 -0
- data/test/renee-render/test_helper.rb +9 -0
- data/test/renee-session/session_test.rb +31 -0
- data/test/renee-session/test_helper.rb +9 -0
- data/test/renee-url-generation/test_helper.rb +10 -0
- data/test/renee-url-generation/url_generation_test.rb +63 -0
- data/test/{blog_test.rb → renee/blog_test.rb} +10 -5
- data/test/renee/test_helper.rb +56 -0
- data/test/test_helper.rb +23 -10
- metadata +333 -156
- data/.yardopts +0 -6
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gemspec :name => 'renee'
|
4
|
+
gemspec :name => 'renee-core'
|
5
|
+
gemspec :name => 'renee-render'
|
6
|
+
|
7
|
+
gem 'rdiscount'
|
8
|
+
gem 'haml'
|
9
|
+
gem 'rack-codehighlighter'
|
10
|
+
gem 'rack-google-analytics'
|
11
|
+
gem 'coderay'
|
12
|
+
|
13
|
+
group :development do
|
14
|
+
gem 'shotgun'
|
15
|
+
gem 'rake'
|
16
|
+
gem 'yard', '~> 0.7.4'
|
17
|
+
end
|
data/Gemfile-renee
ADDED
data/Gemfile-renee-core
ADDED
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2011 Joshua Hull, Nathan Esquenazi, Arthur Chiu
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,242 @@
|
|
1
|
+
# Renee Core
|
2
|
+
|
3
|
+
## Routing
|
4
|
+
|
5
|
+
Routing in `Renee` is different from any web framework you are likely to have used in the past. The syntax is most familiar to Sinatra but allows
|
6
|
+
for far more flexibility and freedom in the way that routes and actions are defined. In a Renee, routes are defined using the `path`, `var`, `query_string`, `extension`, `remainder` and request methods.
|
7
|
+
|
8
|
+
**Request Methods**
|
9
|
+
|
10
|
+
The bread and butter of Renee are the request verbs reminiscent of Sinatra:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
run Renee.core {
|
14
|
+
get { halt "a get!" }
|
15
|
+
post { halt "a post!" }
|
16
|
+
put { halt "a put!" }
|
17
|
+
delete { halt "a delete!" }
|
18
|
+
}
|
19
|
+
```
|
20
|
+
|
21
|
+
These will declare the response to "/" for each of the common request types. Notice the use of the request method to
|
22
|
+
specify the http verb and the use of `halt` inside the block to send back the body of the response.
|
23
|
+
|
24
|
+
**Path**
|
25
|
+
|
26
|
+
Path is how Renee describes the basic uri path for a route:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
run Renee.core {
|
30
|
+
path('blog') { ... }
|
31
|
+
}
|
32
|
+
```
|
33
|
+
|
34
|
+
All declarations inside that block will start with `/blog`. Paths can also be nested within one another:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
run Renee.core {
|
38
|
+
path('blog') {
|
39
|
+
path('foo') { get { halt "path is /blog/foo" } }
|
40
|
+
}
|
41
|
+
}
|
42
|
+
```
|
43
|
+
|
44
|
+
You can also use `exact_path` for more precise path matching and/or `part` which doesn't look for leading slashes.
|
45
|
+
|
46
|
+
**Query String**
|
47
|
+
|
48
|
+
In addition to defining paths, you may find yourself wanting to describe the state of the query string for a request within the path:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
path 'foo' do
|
52
|
+
query_string 'bar' do
|
53
|
+
get { halt 'BAR!' }
|
54
|
+
end
|
55
|
+
|
56
|
+
query_string 'baz' do
|
57
|
+
get { halt 'BAZ!' }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
This will respond to `/foo?bar` with "BAR!" and `/foo?baz` with "BAZ!". You can also specify query_string in a variety of other ways:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# Check key and value of query param
|
66
|
+
query_string 'foo=bar' do
|
67
|
+
post { halt [200,{},'foo'] }
|
68
|
+
end
|
69
|
+
|
70
|
+
# Declare query params as a hash
|
71
|
+
query :foo => "bar" do
|
72
|
+
halt 200
|
73
|
+
end
|
74
|
+
|
75
|
+
# Switch based on a query parameter
|
76
|
+
query :foo do |var|
|
77
|
+
case var
|
78
|
+
when 'bar' then halt 200
|
79
|
+
when 'bar2' then halt 500
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
**Variables**
|
85
|
+
|
86
|
+
In Renee, you specify parameters for your request as explicit variables. Variables are declared like this:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
path('blog') {
|
90
|
+
var { |id| get { halt "path is /blog/#{id}" } }
|
91
|
+
}
|
92
|
+
```
|
93
|
+
|
94
|
+
You can access the variables (passed into the request) using the local variables yielded to the block. Variables are a powerful
|
95
|
+
way to express expected parameters for a given set of requests. You can specify variables that match a regex:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
path('blog') {
|
99
|
+
var(/\d+/) { |id| get { halt "path is /blog/#{id}" } }
|
100
|
+
}
|
101
|
+
```
|
102
|
+
|
103
|
+
and even explicitly cast your variable types:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
path('blog') {
|
107
|
+
var :integer do |id|
|
108
|
+
get { halt "path is /blog/#{id} and id is an integer" }
|
109
|
+
end
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
**Extensions**
|
114
|
+
|
115
|
+
You can also use `extension` as a way to define formats:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
path '/test' do
|
119
|
+
extension 'html' do
|
120
|
+
halt 'html'
|
121
|
+
end
|
122
|
+
extension 'json' do
|
123
|
+
halt 'json'
|
124
|
+
end
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
This will have `test.html` respond with 'html' and `test.json` respond with 'json'.
|
129
|
+
|
130
|
+
**Remainder**
|
131
|
+
|
132
|
+
In the event that no route has been matched, the `remainder` keyword makes defining the else case rather easy:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
path 'foo' do
|
136
|
+
path 'bar' do
|
137
|
+
halt "BAR!"
|
138
|
+
end
|
139
|
+
|
140
|
+
remainder do |rest|
|
141
|
+
halt "Rest was #{rest}"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
```
|
145
|
+
|
146
|
+
Notice this allows you to handle the cases within a particular route scope and manage them based on the "rest" of the uri yielded in the `remainder` block. You
|
147
|
+
can handle different remainders in all the different path blocks.
|
148
|
+
|
149
|
+
**Named Routes**
|
150
|
+
|
151
|
+
Once you have defined your routes, you can then "register" a particular path mapping that to a symbol. This is useful for referencing routes without
|
152
|
+
having to specify the entire path:
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
run Renee.core {
|
156
|
+
register(:test, '/test/time')
|
157
|
+
register(:test_var, '/test/:id')
|
158
|
+
}
|
159
|
+
```
|
160
|
+
|
161
|
+
You can then access these using the `path` method in a route or template:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
path(:test) # => '/test/time'
|
165
|
+
path(:test_var, :id => 123) # => '/test/123'
|
166
|
+
```
|
167
|
+
|
168
|
+
Using named routes makes referencing and modifying routes within an application much simpler to manage.
|
169
|
+
|
170
|
+
## Responding
|
171
|
+
|
172
|
+
Responding to a request within a route can be managed with the `respond`, `halt`, `redirect` commands:
|
173
|
+
|
174
|
+
**Respond**
|
175
|
+
|
176
|
+
The `respond` command makes returning a rack response very explicit, you can respond as if you were constructing a Rack::Response
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
run Renee {
|
180
|
+
get { respond!("hello!", 403, "foo" => "bar") }
|
181
|
+
}
|
182
|
+
```
|
183
|
+
|
184
|
+
or use the block DSL for convenience:
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
run Renee {
|
188
|
+
get { respond! { status 403; headers :foo => "bar"; body "hello!" } }
|
189
|
+
}
|
190
|
+
```
|
191
|
+
|
192
|
+
**Halt**
|
193
|
+
|
194
|
+
Halting is the easiest way to render data within a route:
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
run Renee.core {
|
198
|
+
get { halt 'easy' }
|
199
|
+
}
|
200
|
+
```
|
201
|
+
|
202
|
+
This will return a 200 status code and 'easy' as the body. You can also specify status code and header explicitly in the halt response:
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
get { halt [200, {}, 'body'] }
|
206
|
+
```
|
207
|
+
|
208
|
+
This will set the status code to 200, pass no headers and return 'body'. You can also use several variations of halt:
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
# Return just status code
|
212
|
+
halt 200
|
213
|
+
|
214
|
+
# Return status with symbol
|
215
|
+
halt :not_found
|
216
|
+
|
217
|
+
# Return 200 with body
|
218
|
+
halt "hello!"
|
219
|
+
|
220
|
+
# Return 500 with body
|
221
|
+
halt 500, "hello!"
|
222
|
+
```
|
223
|
+
|
224
|
+
Halt is the most straightforward way to control the response for a request.
|
225
|
+
|
226
|
+
**Redirect**
|
227
|
+
|
228
|
+
A redirect is a common action within a web route and can be achieved with the convenience method `redirect` command:
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
get {
|
232
|
+
halt redirect('/hello')
|
233
|
+
}
|
234
|
+
```
|
235
|
+
|
236
|
+
You can also specify the status code for the redirect:
|
237
|
+
|
238
|
+
```ruby
|
239
|
+
get {
|
240
|
+
halt redirect('/hello', 303)
|
241
|
+
}
|
242
|
+
```
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Renee Render
|
2
|
+
|
3
|
+
Rendering templates in Renee should be familiar and intuitive using the `render` command:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
run Renee.core {
|
7
|
+
path('blog') do
|
8
|
+
get { render! "blogs/index", :haml }
|
9
|
+
end
|
10
|
+
}
|
11
|
+
```
|
12
|
+
|
13
|
+
This above is the standard render syntax, specifying the engine followed by the template. You can also render without specifying an engine:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
path('blog') do
|
17
|
+
get { render! "blogs/index" }
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
This will do a lookup in the views path to find the appropriately named template. You can also pass locals and layout options as you would expect:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
path('blog') do
|
25
|
+
get { render! "blogs/index", :locals => { :foo => "bar" }, :layout => :bar }
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
This will render the "blogs/index.erb" file if it exists, passing the 'foo' local variable
|
30
|
+
and wrapping the result in the 'bar.erb' layout file. You can also render without returning the response by using:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
path('blog') do
|
34
|
+
get { render "blogs/index" }
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
This allows you to render the content as a string without immediately responding.
|
data/README.md
CHANGED
@@ -1,10 +1,135 @@
|
|
1
1
|
# Renee
|
2
2
|
|
3
|
-
|
3
|
+
The super friendly rack-based web framework.
|
4
4
|
|
5
|
-
##
|
5
|
+
## Introduction
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
Renee is a new Rack-based library for describing web applications. Sinatra delivered a new simple way to think about building web applications. The popularity of Sinatra both as a library and as a concept shows now enduring the concept really was. Sinatra was different from Rails because the entire DSL was lightweight, easy to read and combined routing and actions into a single file. However, let's consider an example from Sinatra to see where we can improve upon this.
|
8
|
+
|
9
|
+
Consider:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
get '/blog/:id' do
|
13
|
+
Blog.get(params[:id])
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
This is not too bad so far. The repetition of `:id` is a bit un-DRY, but not bad. Let's keep expanding upon this.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
get '/blog/:id' do
|
21
|
+
Blog.get(params[:id])
|
22
|
+
end
|
23
|
+
|
24
|
+
put '/blog/:id' do
|
25
|
+
Blog.get(params[:id]).update_attributes(params)
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
Now, we've retrieved blog in two places. Time to refactor. We'd normally create a before filter, with the same path.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
before '/blog/:id' do
|
33
|
+
@blog = Blog.get(params[:id])
|
34
|
+
end
|
35
|
+
|
36
|
+
get '/blog/:id' do
|
37
|
+
@blog
|
38
|
+
end
|
39
|
+
|
40
|
+
put '/blog/:id' do
|
41
|
+
@blog.update_attributes(params)
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Now we've repeated the same path three times. With Renee, we can describe these kind of ideas in a simple, easy-to-read way. Here is the equivalent in Renee.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
path 'blog' do
|
49
|
+
var do |id|
|
50
|
+
@blog = Blog.get(id)
|
51
|
+
get { halt @blog }
|
52
|
+
put { @blog.update(request.params); halt :ok}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
This web library is inspired by Sinatra, but offers an approach more inline with Rack itself, and lets you maximize code-reuse within your application.
|
58
|
+
|
59
|
+
## Installation
|
60
|
+
|
61
|
+
Setup Renee by running:
|
62
|
+
|
63
|
+
```
|
64
|
+
gem install renee
|
65
|
+
```
|
66
|
+
|
67
|
+
or by adding `Renee` to your `Gemfile`:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
# Gemfile
|
71
|
+
gem "renee", "~> 0.0.1"
|
72
|
+
```
|
73
|
+
|
74
|
+
Now you can start using Renee for your application!
|
75
|
+
|
76
|
+
## Usage
|
77
|
+
|
78
|
+
In your rackup file, give this a go!
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
require 'renee'
|
82
|
+
|
83
|
+
run Renee.core {
|
84
|
+
path('test') do
|
85
|
+
get { halt "Hey, this is a get!" }
|
86
|
+
post { halt "and .. a post" }
|
87
|
+
|
88
|
+
var do |id|
|
89
|
+
halt "Getting the blog with id #{id}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
}
|
93
|
+
```
|
94
|
+
|
95
|
+
This rack-app will respond to GET /test with "Hey, this is a get!", POST /test with "and .. a post" and /test/:id with `"Getting the blog with id #{id}"`.
|
96
|
+
|
97
|
+
A more complete example might be sample RESTful routing definitions for a blog post resource:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
require 'renee'
|
101
|
+
|
102
|
+
run Renee.core {
|
103
|
+
path('posts') do
|
104
|
+
@posts = Post.all
|
105
|
+
# GET /posts
|
106
|
+
get { render! "index" }
|
107
|
+
# POST /posts
|
108
|
+
post { redirect! "/posts/index" }
|
109
|
+
|
110
|
+
path('new') do
|
111
|
+
# GET /posts/new
|
112
|
+
get { render! "new" }
|
113
|
+
end
|
114
|
+
|
115
|
+
var :integer do |id|
|
116
|
+
@post = Post.find(id)
|
117
|
+
|
118
|
+
# GET /posts/5
|
119
|
+
get { render! 'show' }
|
120
|
+
# PUT /posts/5
|
121
|
+
put { halt "update" }
|
122
|
+
# DELETE /posts/5
|
123
|
+
delete { halt "delete" }
|
124
|
+
|
125
|
+
path('edit') do
|
126
|
+
# GET /posts/5/edit
|
127
|
+
get { render! "edit" }
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
}
|
132
|
+
```
|
133
|
+
|
134
|
+
This implements the standard 7 REST actions in a very concise and simple way. The routes and the actions blended together utilizing the various
|
135
|
+
Renee routing methods and block syntax. The routing methods are defined below, followed by an explanation of how to respond to a route.
|