renee-core 0.3.11 → 0.4.0.pre1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/{renee_core.rb → renee/core.rb} +20 -13
- data/lib/{renee_core → renee/core}/chaining.rb +17 -15
- data/lib/{renee_core → renee/core}/env_accessors.rb +3 -3
- data/lib/{renee_core → renee/core}/exceptions.rb +0 -0
- data/lib/{renee_core → renee/core}/matcher.rb +0 -0
- data/lib/renee/core/plugins.rb +31 -0
- data/lib/{renee_core → renee/core}/rack_interaction.rb +0 -0
- data/lib/renee/core/request_context.rb +56 -0
- data/lib/{renee_core → renee/core}/responding.rb +3 -1
- data/lib/{renee_core → renee/core}/response.rb +0 -0
- data/lib/{renee_core → renee/core}/routing.rb +98 -101
- data/lib/{renee_core → renee/core}/transform.rb +0 -0
- data/test/{chaining_test.rb → renee-core/chaining_test.rb} +3 -3
- data/test/{env_accessors_test.rb → renee-core/env_accessors_test.rb} +1 -1
- data/test/{include_test.rb → renee-core/include_test.rb} +0 -0
- data/test/renee-core/request_context_test.rb +70 -0
- data/test/{responding_test.rb → renee-core/responding_test.rb} +0 -0
- data/test/{routing_test.rb → renee-core/routing_test.rb} +39 -82
- data/test/renee-core/test_helper.rb +4 -0
- data/test/{variable_type_test.rb → renee-core/variable_type_test.rb} +0 -0
- data/test/test_helper.rb +70 -4
- metadata +88 -129
- data/.yardopts +0 -6
- data/README.md +0 -242
- data/Rakefile +0 -13
- data/lib/renee_core/request_context.rb +0 -25
- data/lib/renee_core/url_generation.rb +0 -108
- data/lib/renee_core/version.rb +0 -6
- data/renee-core.gemspec +0 -26
- data/test/url_generation_test.rb +0 -66
data/.yardopts
DELETED
data/README.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/Rakefile
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'rake/testtask'
|
2
|
-
require 'yard'
|
3
|
-
|
4
|
-
Rake::TestTask.new do |t|
|
5
|
-
t.libs.push "lib"
|
6
|
-
t.test_files = FileList[File.expand_path('../test/**/*_test.rb', __FILE__)]
|
7
|
-
t.verbose = true
|
8
|
-
end
|
9
|
-
|
10
|
-
desc "Generate documentation for the Padrino framework"
|
11
|
-
task :doc do
|
12
|
-
YARD::CLI::Yardoc.new.run
|
13
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
module Renee
|
2
|
-
class Core
|
3
|
-
# This module deals with the Rack#call compilance. It defines #call and also defines several critical methods
|
4
|
-
# used by interaction by other application modules.
|
5
|
-
module RequestContext
|
6
|
-
attr_reader :env, :request, :detected_extension
|
7
|
-
|
8
|
-
# Provides a rack interface compliant call method.
|
9
|
-
# @param[Hash] env The rack environment.
|
10
|
-
def call(env)
|
11
|
-
@env, @request = env, Rack::Request.new(env)
|
12
|
-
@detected_extension = env['PATH_INFO'][/\.([^\.\/]+)$/, 1]
|
13
|
-
# TODO clear template cache in development? `template_cache.clear`
|
14
|
-
catch(:halt) do
|
15
|
-
begin
|
16
|
-
instance_eval(&self.class.application_block)
|
17
|
-
rescue ClientError => e
|
18
|
-
e.response ? instance_eval(&e.response) : halt("There was an error with your request", 400)
|
19
|
-
end
|
20
|
-
Renee::Core::Response.new("Not found", 404).finish
|
21
|
-
end
|
22
|
-
end # call
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,108 +0,0 @@
|
|
1
|
-
require 'uri'
|
2
|
-
|
3
|
-
module Renee
|
4
|
-
class Core
|
5
|
-
# URL generator for creating paths and URLs within your application.
|
6
|
-
module URLGeneration
|
7
|
-
|
8
|
-
# Registers new paths for generation.
|
9
|
-
# @param [Symbol] name The name of the path
|
10
|
-
# @param [String] pattern The pattern used for generation.
|
11
|
-
# @param [Hash, nil] defaults Any default values used for generation.
|
12
|
-
#
|
13
|
-
# @example
|
14
|
-
# renee.register(:path, "/my/:var/path")
|
15
|
-
# renee.path(:path, 123) # => "/my/123/path"
|
16
|
-
# renee.path(:path, :var => 'hey you') # => "/my/hey%20you/path"
|
17
|
-
def register(name, pattern, defaults = nil)
|
18
|
-
url_generators[name] = Generator.new("#{@generation_prefix}#{pattern}", defaults_for_generation(defaults))
|
19
|
-
end
|
20
|
-
|
21
|
-
# Allows the creation of generation contexts.
|
22
|
-
# @param [String] prefix The prefix to add to subsequent calls to #register.
|
23
|
-
# @param [Hash, nil] defaults The defaults to add to subsequent calls to #register.
|
24
|
-
# @see #register
|
25
|
-
#
|
26
|
-
# @example
|
27
|
-
# renee.prefix("/prefix") {
|
28
|
-
# renee.register(:prefix_path, "/path") # would register /prefix/path
|
29
|
-
# }
|
30
|
-
def prefix(prefix, defaults = nil, &blk)
|
31
|
-
generator = self
|
32
|
-
subgenerator = Class.new {
|
33
|
-
include URLGeneration
|
34
|
-
define_method(:url_generators) { generator.send(:url_generators) }
|
35
|
-
}.new
|
36
|
-
subgenerator.instance_variable_set(:@generation_prefix, "#{@generation_prefix}#{prefix}")
|
37
|
-
subgenerator.instance_variable_set(:@generation_defaults, defaults_for_generation(defaults))
|
38
|
-
if block_given?
|
39
|
-
old_prefix, old_defaults = @generation_prefix, @generation_defaults
|
40
|
-
@generation_prefix, @generation_defaults = "#{@generation_prefix}#{prefix}", defaults_for_generation(defaults)
|
41
|
-
subgenerator.instance_eval(&blk)
|
42
|
-
@generation_prefix, @generation_defaults = old_prefix, old_defaults
|
43
|
-
end
|
44
|
-
subgenerator
|
45
|
-
end
|
46
|
-
|
47
|
-
# Generates a path for a given name.
|
48
|
-
# @param [Symbol] name The name of the path
|
49
|
-
# @param [Object] args The values used to generate the path. Can be named with using :name => "value" or supplied
|
50
|
-
# in the order for which the variables were decalared in #register.
|
51
|
-
#
|
52
|
-
# @see #register
|
53
|
-
def path(name, *args)
|
54
|
-
generator = url_generators[name]
|
55
|
-
generator ? generator.path(*args) : raise("Generator for #{name} doesn't exist")
|
56
|
-
end
|
57
|
-
|
58
|
-
# Generates a url for a given name.
|
59
|
-
# @param (see #path)
|
60
|
-
# @see #path
|
61
|
-
def url(name, *args)
|
62
|
-
generator = url_generators[name]
|
63
|
-
generator ? generator.url(*args) : raise("Generator for #{name} doesn't exist")
|
64
|
-
end
|
65
|
-
|
66
|
-
private
|
67
|
-
def url_generators
|
68
|
-
@url_generators ||= {}
|
69
|
-
end
|
70
|
-
|
71
|
-
def defaults_for_generation(defaults)
|
72
|
-
@generation_defaults && defaults ? @generation_defaults.merge(defaults) : (defaults || @generation_defaults)
|
73
|
-
end
|
74
|
-
|
75
|
-
# @private
|
76
|
-
class Generator
|
77
|
-
attr_reader :defaults
|
78
|
-
|
79
|
-
def initialize(template, defaults = nil)
|
80
|
-
@defaults = defaults
|
81
|
-
parsed_template = URI.parse(template)
|
82
|
-
@host = parsed_template.host
|
83
|
-
@template = parsed_template.path
|
84
|
-
@scheme = parsed_template.scheme
|
85
|
-
port = parsed_template.port
|
86
|
-
if !port.nil? and (@scheme.nil? or @scheme == "http" && port != '80' or @scheme == "https" && port != '443')
|
87
|
-
@port_part = ":#{port}"
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def path(*args)
|
92
|
-
opts = args.last.is_a?(Hash) ? args.pop : nil
|
93
|
-
opts = opts ? defaults.merge(opts) : defaults.dup if defaults
|
94
|
-
path = @template.gsub(/:([a-zA-Z0-9_]+)/) { |name|
|
95
|
-
name = name[1, name.size - 1].to_sym
|
96
|
-
(opts && opts.delete(name)) || (defaults && defaults[name]) || args.shift || raise("variable #{name.inspect} not found")
|
97
|
-
}
|
98
|
-
URI.encode(opts.nil? || opts.empty? ? path : "#{path}?#{Rack::Utils.build_query(opts)}")
|
99
|
-
end
|
100
|
-
|
101
|
-
def url(*args)
|
102
|
-
raise "This URL cannot be generated as no host has been defined." if @host.nil?
|
103
|
-
"#{@scheme}://#{@host}#{@port_part}#{path(*args)}"
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
data/lib/renee_core/version.rb
DELETED
data/renee-core.gemspec
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "renee_core/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |s|
|
6
|
-
s.name = "renee-core"
|
7
|
-
s.version = Renee::Core::VERSION
|
8
|
-
s.authors = ["Josh Hull", "Nathan Esquenazi", "Arthur Chiu"]
|
9
|
-
s.email = ["joshbuddy@gmail.com", "nesquena@gmail.com", "mr.arthur.chiu@gmail.com"]
|
10
|
-
s.homepage = "http://reneerb.com"
|
11
|
-
s.summary = %q{The super-friendly rack helpers}
|
12
|
-
s.description = %q{The super-friendly rack helpers.}
|
13
|
-
|
14
|
-
s.rubyforge_project = "renee"
|
15
|
-
|
16
|
-
s.files = `git ls-files`.split("\n")
|
17
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
-
s.require_paths = ["lib"]
|
20
|
-
|
21
|
-
s.add_runtime_dependency 'rack', "~> 1.3.0"
|
22
|
-
s.add_development_dependency 'minitest', "~> 2.6.1"
|
23
|
-
s.add_development_dependency 'bundler'
|
24
|
-
s.add_development_dependency "rack-test", ">= 0.5.0"
|
25
|
-
s.add_development_dependency "rake", "0.8.7"
|
26
|
-
end
|
data/test/url_generation_test.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
require File.expand_path('../test_helper', __FILE__)
|
4
|
-
|
5
|
-
describe Renee::Core::URLGeneration do
|
6
|
-
it "should allow registration and generation of paths" do
|
7
|
-
r = Renee.core()
|
8
|
-
r.register(:test, '/test/time')
|
9
|
-
r.register(:test_var, '/test/:id')
|
10
|
-
assert_equal '/test/time', r.path(:test)
|
11
|
-
assert_equal '/test/123', r.path(:test_var, :id => 123)
|
12
|
-
assert_equal '/test/123', r.path(:test_var, 123)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should allow registration and generation of urls" do
|
16
|
-
r = Renee.core()
|
17
|
-
r.register(:test, 'http://localhost:8080/test/:time')
|
18
|
-
assert_equal 'http://localhost:8080/test/123', r.url(:test, 123)
|
19
|
-
assert_equal 'http://localhost:8080/test/654', r.url(:test, :time => '654')
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should escape values when generating" do
|
23
|
-
r = Renee.core()
|
24
|
-
r.register(:test, '/:test')
|
25
|
-
assert_equal '/f%C3%B8%C3%B8', r.path(:test, "føø")
|
26
|
-
end
|
27
|
-
|
28
|
-
it "should encode extra values as query string params" do
|
29
|
-
r = Renee.core()
|
30
|
-
r.register(:test, '/:test')
|
31
|
-
assert_equal '/foo?bar=baz', r.path(:test, 'foo', :bar => :baz)
|
32
|
-
assert_equal '/foo?bar=baz', r.path(:test, :test => 'foo', :bar => :baz)
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should allow default values" do
|
36
|
-
r = Renee.core()
|
37
|
-
r.register(:test, '/:test', :test => 'foo')
|
38
|
-
assert_equal '/foo', r.path(:test)
|
39
|
-
assert_equal '/baz', r.path(:test, :test => 'baz')
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should include default vars as query string vars" do
|
43
|
-
r = Renee.core()
|
44
|
-
r.register(:test, '/:foo', :test => 'foo')
|
45
|
-
assert_equal '/foo?test=foo', r.path(:test, 'foo')
|
46
|
-
assert_equal '/foo?test=foo', r.path(:test, :foo => 'foo')
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should allow #prefix calls for nesting common path parts" do
|
50
|
-
r = Renee.core()
|
51
|
-
r.prefix('/foo') do
|
52
|
-
r.register(:foo_bar, '/bar')
|
53
|
-
end
|
54
|
-
assert_equal '/foo/bar', r.path(:foo_bar)
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should allow passing defaults and overriding them on a per-register basis" do
|
58
|
-
r = Renee.core()
|
59
|
-
r.prefix('/foo', :bar => 'baz') do
|
60
|
-
register(:foo_bar, '/bar', :bar => 'bam')
|
61
|
-
register(:foo_baz, '/baz')
|
62
|
-
end
|
63
|
-
assert_equal '/foo/bar?bar=bam', r.path(:foo_bar)
|
64
|
-
assert_equal '/foo/baz?bar=baz', r.path(:foo_baz)
|
65
|
-
end
|
66
|
-
end
|