renee 0.4.0.pre1 → 0.4.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +11 -8
- data/README.md +50 -1
- data/Rakefile +14 -104
- data/config.ru +5 -0
- data/examples/blog/config.ru +1 -0
- data/lib/renee.rb +3 -2
- data/lib/renee/render.rb +1 -1
- data/lib/renee/url_generation.rb +66 -45
- data/lib/renee/version.rb +1 -1
- data/plan.txt +1 -5
- data/renee.gemspec +3 -3
- data/site/public/css/app.css +47 -26
- data/site/views/extending.md +66 -0
- data/site/views/index.md +14 -4
- data/site/views/routing.md +0 -38
- data/site/views/variable-types.md +2 -2
- data/test/{renee/blog_test.rb → blog_test.rb} +0 -0
- data/test/{renee-render/render_test.rb → render_test.rb} +0 -0
- data/test/renee-url-generation/test_helper.rb +1 -1
- data/test/{renee-session/session_test.rb → session_test.rb} +2 -2
- data/test/renee/test_helper.rb b/data/test/test_helper → copy.rb +0 -0
- data/test/test_helper.rb +8 -1
- data/test/{renee-url-generation/url_generation_test.rb → url_generation_test.rb} +2 -2
- metadata +47 -77
- data/Gemfile-renee +0 -8
- data/Gemfile-renee-core +0 -8
- data/Gemfile-renee-render +0 -9
- data/Gemfile-renee-session +0 -9
- data/Gemfile-renee-url-generation +0 -8
- data/README-renee-core.md +0 -242
- data/README-renee-render.md +0 -38
- data/README-renee-session.md +0 -3
- data/README-renee-url-generation.md +0 -3
- data/lib/renee/core.rb +0 -98
- data/lib/renee/core/chaining.rb +0 -66
- data/lib/renee/core/env_accessors.rb +0 -72
- data/lib/renee/core/exceptions.rb +0 -15
- data/lib/renee/core/matcher.rb +0 -61
- data/lib/renee/core/plugins.rb +0 -31
- data/lib/renee/core/rack_interaction.rb +0 -50
- data/lib/renee/core/request_context.rb +0 -56
- data/lib/renee/core/responding.rb +0 -112
- data/lib/renee/core/response.rb +0 -78
- data/lib/renee/core/routing.rb +0 -319
- data/lib/renee/core/transform.rb +0 -18
- data/renee-core.gemspec +0 -26
- data/renee-render.gemspec +0 -30
- data/renee-session.gemspec +0 -28
- data/renee-url-generation.gemspec +0 -24
- data/test.watchr +0 -61
- data/test/renee-core/chaining_test.rb +0 -33
- data/test/renee-core/env_accessors_test.rb +0 -43
- data/test/renee-core/include_test.rb +0 -14
- data/test/renee-core/request_context_test.rb +0 -70
- data/test/renee-core/responding_test.rb +0 -128
- data/test/renee-core/routing_test.rb +0 -443
- data/test/renee-core/test_helper.rb +0 -4
- data/test/renee-core/variable_type_test.rb +0 -57
data/Gemfile-renee
DELETED
data/Gemfile-renee-core
DELETED
data/Gemfile-renee-render
DELETED
data/Gemfile-renee-session
DELETED
data/README-renee-core.md
DELETED
@@ -1,242 +0,0 @@
|
|
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
|
-
```
|
data/README-renee-render.md
DELETED
@@ -1,38 +0,0 @@
|
|
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-renee-session.md
DELETED
data/lib/renee/core.rb
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
require 'rack'
|
2
|
-
|
3
|
-
require 'renee/version'
|
4
|
-
require 'renee/core/matcher'
|
5
|
-
require 'renee/core/chaining'
|
6
|
-
require 'renee/core/response'
|
7
|
-
require 'renee/core/exceptions'
|
8
|
-
require 'renee/core/rack_interaction'
|
9
|
-
require 'renee/core/request_context'
|
10
|
-
require 'renee/core/transform'
|
11
|
-
require 'renee/core/routing'
|
12
|
-
require 'renee/core/responding'
|
13
|
-
require 'renee/core/env_accessors'
|
14
|
-
require 'renee/core/plugins'
|
15
|
-
|
16
|
-
# Top-level Renee constant
|
17
|
-
module Renee
|
18
|
-
# @example
|
19
|
-
# Renee.core { path('/hello') { halt :ok } }
|
20
|
-
def self.core(&blk)
|
21
|
-
cls = Class.new(Renee::Core)
|
22
|
-
cls.app(&blk) if blk
|
23
|
-
cls
|
24
|
-
end
|
25
|
-
|
26
|
-
# The top-level class for creating core application.
|
27
|
-
# For convience you can also used a method named #Renee
|
28
|
-
# for decalaring new instances.
|
29
|
-
class Core
|
30
|
-
# Current version of Renee::Core
|
31
|
-
VERSION = Renee::VERSION
|
32
|
-
|
33
|
-
# Error raised if routing fails. Use #continue_routing to continue routing.
|
34
|
-
NotMatchedError = Class.new(RuntimeError)
|
35
|
-
|
36
|
-
# Class methods that are included in new instances of {Core}
|
37
|
-
module ClassMethods
|
38
|
-
include Plugins
|
39
|
-
|
40
|
-
# The application block used to create your application.
|
41
|
-
attr_reader :application_block
|
42
|
-
|
43
|
-
# Provides a rack interface compliant call method. This method creates a new instance of your class and calls
|
44
|
-
# #call on it.
|
45
|
-
def call(env)
|
46
|
-
new.call(env)
|
47
|
-
end
|
48
|
-
|
49
|
-
# Allows you to set the #application_block on your class.
|
50
|
-
# @yield The application block
|
51
|
-
def app(&app)
|
52
|
-
@application_block = app
|
53
|
-
setup do
|
54
|
-
register_variable_type :integer, IntegerMatcher
|
55
|
-
register_variable_type :int, :integer
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
# Runs class methods on your application.
|
60
|
-
def setup(&blk)
|
61
|
-
instance_eval(&blk)
|
62
|
-
self
|
63
|
-
end
|
64
|
-
|
65
|
-
# The currently available variable types you've defined.
|
66
|
-
def variable_types
|
67
|
-
@variable_types ||= {}
|
68
|
-
end
|
69
|
-
|
70
|
-
# Registers a new variable type for use within {Renee::Core::Routing#variable} and others.
|
71
|
-
# @param [Symbol] name The name of the variable.
|
72
|
-
# @param [Regexp] matcher A regexp describing what part of an arbitrary string to capture.
|
73
|
-
# @return [Renee::Core::Matcher] A matcher
|
74
|
-
def register_variable_type(name, matcher)
|
75
|
-
matcher = case matcher
|
76
|
-
when Matcher then matcher
|
77
|
-
when Array then Matcher.new(matcher.map{|m| variable_types[m]})
|
78
|
-
when Symbol then variable_types[matcher]
|
79
|
-
else Matcher.new(matcher)
|
80
|
-
end
|
81
|
-
matcher.name = name
|
82
|
-
variable_types[name] = matcher
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
include Chaining
|
87
|
-
include RequestContext
|
88
|
-
include Routing
|
89
|
-
include Responding
|
90
|
-
include RackInteraction
|
91
|
-
include Transform
|
92
|
-
include EnvAccessors
|
93
|
-
|
94
|
-
class << self
|
95
|
-
include ClassMethods
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
data/lib/renee/core/chaining.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
module Renee
|
2
|
-
class Core
|
3
|
-
# Module for creating chainable methods. To use this within your own modules, first `include Chaining`, then,
|
4
|
-
# mark methods you want to be available with `chain_method :method_name`.
|
5
|
-
# @example
|
6
|
-
# module MoreRoutingMethods
|
7
|
-
# include Chaining
|
8
|
-
# def other_routing_method
|
9
|
-
# # ..
|
10
|
-
# end
|
11
|
-
# chain_method :other_routing_method
|
12
|
-
#
|
13
|
-
module Chaining
|
14
|
-
# @private
|
15
|
-
class ChainingProxy
|
16
|
-
def initialize(target, m, args = nil)
|
17
|
-
@target, @calls = target, []
|
18
|
-
@calls << [m, args]
|
19
|
-
end
|
20
|
-
|
21
|
-
def method_missing(m, *args, &blk)
|
22
|
-
@calls << [m, args]
|
23
|
-
if blk.nil? && @target.class.respond_to?(:chainable?) && @target.class.chainable?(m)
|
24
|
-
self
|
25
|
-
else
|
26
|
-
inner_args = []
|
27
|
-
ret = nil
|
28
|
-
callback = proc do |*callback_args|
|
29
|
-
inner_args.concat(callback_args)
|
30
|
-
if @calls.size == 0
|
31
|
-
ret = blk.call(*inner_args) if blk
|
32
|
-
else
|
33
|
-
call = @calls.shift
|
34
|
-
ret = call.at(1) ? @target.send(call.at(0), *call.at(1), &callback) : @target.send(call.at(0), &callback)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
ret = callback.call
|
38
|
-
ret
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# @private
|
44
|
-
module ClassMethods
|
45
|
-
def chainable?(m)
|
46
|
-
method_defined?(:"#{m}_chainable")
|
47
|
-
end
|
48
|
-
|
49
|
-
def chainable(*methods)
|
50
|
-
methods.each do |m|
|
51
|
-
define_method(:"#{m}_chainable") { }
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
def create_chain_proxy(method_name, *args)
|
58
|
-
ChainingProxy.new(self, method_name, args)
|
59
|
-
end
|
60
|
-
|
61
|
-
def self.included(o)
|
62
|
-
o.extend(ClassMethods)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|