roda-cj 0.9.4 → 0.9.5
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.
- checksums.yaml +4 -4
- data/CHANGELOG +4 -0
- data/lib/roda.rb +14 -1
- data/lib/roda/plugins/multi_route.rb +50 -8
- data/lib/roda/version.rb +1 -1
- data/spec/plugin/multi_route_spec.rb +22 -6
- data/spec/redirect_spec.rb +21 -4
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4b2651937ad6d2d3c5d87389b03fd9e0f1e3cef2
|
|
4
|
+
data.tar.gz: e231917660219dd780e2adaacd5fd8fbbd42afd3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8ebb628c78058e1e9c61acf148a1e791f3036cf01290461c9ebf01b220b997ab62b78bb2f2429b87b378d1ea0d64134d4a0101ee8dcef5a8794cfe2b6d4effb5
|
|
7
|
+
data.tar.gz: db3d4837c8af107e4f3ee6224a15ebadeb5afd0d4122a77f359aa1b9ccb9a263153e5e1628ce77456738f9b76c68fb7cc59a3e5e228dd3c3ba7dd1a84dcc90d6
|
data/CHANGELOG
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
= HEAD
|
|
2
2
|
|
|
3
|
+
* Add r.multi_route method to multi_route plugin, for dispatching to named route based on first segment in path (jeremyevans)
|
|
4
|
+
|
|
5
|
+
* Allow non-GET requests to use r.redirect with no argument, redirecting to current path (jeremyevans)
|
|
6
|
+
|
|
3
7
|
* Add head plugin, for handling HEAD requests like GET requests with an empty body (jeremyevans)
|
|
4
8
|
|
|
5
9
|
* Optimize consuming patterns by using a positive lookahead assertion (jeremyevans)
|
data/lib/roda.rb
CHANGED
|
@@ -429,7 +429,7 @@ class Roda
|
|
|
429
429
|
end
|
|
430
430
|
|
|
431
431
|
# Immediately redirect to the given path.
|
|
432
|
-
def redirect(path, status=302)
|
|
432
|
+
def redirect(path=default_redirect_path, status=302)
|
|
433
433
|
response.redirect(path, status)
|
|
434
434
|
throw :halt, response.finish
|
|
435
435
|
end
|
|
@@ -535,6 +535,19 @@ class Roda
|
|
|
535
535
|
captures.concat(vars)
|
|
536
536
|
end
|
|
537
537
|
|
|
538
|
+
# The default path to use for redirects when a path is not given.
|
|
539
|
+
# For non-GET requests, redirects to the current path, which will
|
|
540
|
+
# trigger a GET request. This is to make the common case where
|
|
541
|
+
# a POST request will redirect to a GET request at the same location
|
|
542
|
+
# will work fine.
|
|
543
|
+
#
|
|
544
|
+
# If the current request is a GET request, raise an error, as otherwise
|
|
545
|
+
# it is easy to create an infinite redirect.
|
|
546
|
+
def default_redirect_path
|
|
547
|
+
raise RodaError, "must provide path argument to redirect for get requests" if is_get?
|
|
548
|
+
full_path_info
|
|
549
|
+
end
|
|
550
|
+
|
|
538
551
|
# If all of the arguments match, yields to the match block and
|
|
539
552
|
# returns the rack response when the block returns. If any of
|
|
540
553
|
# the match arguments doesn't match, does nothing.
|
|
@@ -1,39 +1,56 @@
|
|
|
1
1
|
class Roda
|
|
2
2
|
module RodaPlugins
|
|
3
3
|
# The multi_route plugin allows for multiple named routes, which the
|
|
4
|
-
# main route block can dispatch to by name at any point
|
|
5
|
-
# route doesn't handle the request, execution will continue,
|
|
6
|
-
# named route does handle the request, the response by
|
|
7
|
-
# will be returned.
|
|
4
|
+
# main route block can dispatch to by name at any point by calling +route+.
|
|
5
|
+
# If the named route doesn't handle the request, execution will continue,
|
|
6
|
+
# and if the named route does handle the request, the response returned by
|
|
7
|
+
# the named route will be returned.
|
|
8
|
+
#
|
|
9
|
+
# In addition, this also adds the +r.multi_route+ method, which will assume
|
|
10
|
+
# check if the first segment in the path matches a named route, and dispatch
|
|
11
|
+
# to that named route.
|
|
8
12
|
#
|
|
9
13
|
# Example:
|
|
10
14
|
#
|
|
11
15
|
# plugin :multi_route
|
|
12
16
|
#
|
|
13
|
-
# route(
|
|
17
|
+
# route('foo') do |r|
|
|
14
18
|
# r.is 'bar' do
|
|
15
19
|
# '/foo/bar'
|
|
16
20
|
# end
|
|
17
21
|
# end
|
|
18
22
|
#
|
|
19
|
-
# route(
|
|
23
|
+
# route('bar') do |r|
|
|
20
24
|
# r.is 'foo' do
|
|
21
25
|
# '/bar/foo'
|
|
22
26
|
# end
|
|
23
27
|
# end
|
|
24
28
|
#
|
|
25
29
|
# route do |r|
|
|
30
|
+
# r.multi_route
|
|
31
|
+
#
|
|
32
|
+
# # or
|
|
33
|
+
#
|
|
26
34
|
# r.on "foo" do
|
|
27
|
-
# route
|
|
35
|
+
# route 'foo'
|
|
28
36
|
# end
|
|
29
37
|
#
|
|
30
38
|
# r.on "bar" do
|
|
31
|
-
# route
|
|
39
|
+
# route 'bar'
|
|
32
40
|
# end
|
|
33
41
|
# end
|
|
34
42
|
#
|
|
35
43
|
# Note that in multi-threaded code, you should not attempt to add a
|
|
36
44
|
# named route after accepting requests.
|
|
45
|
+
#
|
|
46
|
+
# If you want to use the +r.multi_route+ method, use string names for the
|
|
47
|
+
# named routes. Also, you can provide a block to +r.multi_route+ that is
|
|
48
|
+
# called if the route matches but the named route did not handle the
|
|
49
|
+
# request:
|
|
50
|
+
#
|
|
51
|
+
# r.multi_route do
|
|
52
|
+
# "default body"
|
|
53
|
+
# end
|
|
37
54
|
module MultiRoute
|
|
38
55
|
# Initialize storage for the named routes.
|
|
39
56
|
def self.configure(app)
|
|
@@ -47,6 +64,11 @@ class Roda
|
|
|
47
64
|
subclass.instance_variable_set(:@named_routes, @named_routes.dup)
|
|
48
65
|
end
|
|
49
66
|
|
|
67
|
+
# An names for the currently stored named routes
|
|
68
|
+
def named_routes
|
|
69
|
+
@named_routes.keys
|
|
70
|
+
end
|
|
71
|
+
|
|
50
72
|
# Return the named route with the given name.
|
|
51
73
|
def named_route(name)
|
|
52
74
|
@named_routes[name]
|
|
@@ -70,6 +92,26 @@ class Roda
|
|
|
70
92
|
instance_exec(request, &self.class.named_route(name))
|
|
71
93
|
end
|
|
72
94
|
end
|
|
95
|
+
|
|
96
|
+
module RequestClassMethods
|
|
97
|
+
# A regexp matching any of the current named routes.
|
|
98
|
+
def named_route_regexp
|
|
99
|
+
@named_route_regexp ||= /(#{Regexp.union(roda_class.named_routes)})/
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
module RequestMethods
|
|
104
|
+
# Check if the first segment in the path matches any of the current
|
|
105
|
+
# named routes. If so, call that named route. If not, do nothing.
|
|
106
|
+
# If the named route does not handle the request, and a block
|
|
107
|
+
# is given, yield to the block.
|
|
108
|
+
def multi_route
|
|
109
|
+
on self.class.named_route_regexp do |section|
|
|
110
|
+
scope.route(section)
|
|
111
|
+
yield if block_given?
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
73
115
|
end
|
|
74
116
|
|
|
75
117
|
register_plugin(:multi_route, MultiRoute)
|
data/lib/roda/version.rb
CHANGED
|
@@ -5,7 +5,7 @@ describe "multi_route plugin" do
|
|
|
5
5
|
app(:bare) do
|
|
6
6
|
plugin :multi_route
|
|
7
7
|
|
|
8
|
-
route(
|
|
8
|
+
route("get") do |r|
|
|
9
9
|
r.is "" do
|
|
10
10
|
"get"
|
|
11
11
|
end
|
|
@@ -15,7 +15,7 @@ describe "multi_route plugin" do
|
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
route(
|
|
18
|
+
route("post") do |r|
|
|
19
19
|
r.is "" do
|
|
20
20
|
"post"
|
|
21
21
|
end
|
|
@@ -26,15 +26,22 @@ describe "multi_route plugin" do
|
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
route do |r|
|
|
29
|
+
r.on 'foo' do
|
|
30
|
+
r.multi_route do
|
|
31
|
+
"foo"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
29
35
|
r.get do
|
|
30
|
-
route(
|
|
36
|
+
route("get")
|
|
31
37
|
|
|
32
38
|
r.is "b" do
|
|
33
39
|
"getb"
|
|
34
40
|
end
|
|
35
41
|
end
|
|
42
|
+
|
|
36
43
|
r.post do
|
|
37
|
-
route(
|
|
44
|
+
route("post")
|
|
38
45
|
|
|
39
46
|
r.is "b" do
|
|
40
47
|
"postb"
|
|
@@ -55,6 +62,15 @@ describe "multi_route plugin" do
|
|
|
55
62
|
status('/c', 'REQUEST_METHOD'=>'POST').should == 404
|
|
56
63
|
end
|
|
57
64
|
|
|
65
|
+
it "uses multi_route to dispatch to any named route" do
|
|
66
|
+
status('/foo').should == 404
|
|
67
|
+
body('/foo/get/').should == 'get'
|
|
68
|
+
body('/foo/get/a').should == 'geta'
|
|
69
|
+
body('/foo/post/').should == 'post'
|
|
70
|
+
body('/foo/post/a').should == 'posta'
|
|
71
|
+
body('/foo/post/b').should == 'foo'
|
|
72
|
+
end
|
|
73
|
+
|
|
58
74
|
it "handles loading the plugin multiple times correctly" do
|
|
59
75
|
app.plugin :multi_route
|
|
60
76
|
body.should == 'get'
|
|
@@ -71,14 +87,14 @@ describe "multi_route plugin" do
|
|
|
71
87
|
@app = Class.new(@app)
|
|
72
88
|
@app.route do |r|
|
|
73
89
|
r.get do
|
|
74
|
-
route(
|
|
90
|
+
route("post")
|
|
75
91
|
|
|
76
92
|
r.is "b" do
|
|
77
93
|
"1b"
|
|
78
94
|
end
|
|
79
95
|
end
|
|
80
96
|
r.post do
|
|
81
|
-
route(
|
|
97
|
+
route("get")
|
|
82
98
|
|
|
83
99
|
r.is "b" do
|
|
84
100
|
"2b"
|
data/spec/redirect_spec.rb
CHANGED
|
@@ -3,14 +3,25 @@ require File.expand_path("spec_helper", File.dirname(__FILE__))
|
|
|
3
3
|
describe "redirects" do
|
|
4
4
|
it "should be immediately processed" do
|
|
5
5
|
app do |r|
|
|
6
|
-
r.
|
|
7
|
-
r.redirect "/hello"
|
|
6
|
+
r.root do
|
|
7
|
+
r.redirect "/hello"
|
|
8
8
|
"Foo"
|
|
9
9
|
end
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
|
|
11
|
+
r.is "about" do
|
|
12
|
+
r.redirect "/hello", 301
|
|
12
13
|
"Foo"
|
|
13
14
|
end
|
|
15
|
+
|
|
16
|
+
r.is 'foo' do
|
|
17
|
+
r.get do
|
|
18
|
+
r.redirect
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
r.post do
|
|
22
|
+
r.redirect
|
|
23
|
+
end
|
|
24
|
+
end
|
|
14
25
|
end
|
|
15
26
|
|
|
16
27
|
status.should == 302
|
|
@@ -20,5 +31,11 @@ describe "redirects" do
|
|
|
20
31
|
status("/about").should == 301
|
|
21
32
|
header('Location', "/about").should == '/hello'
|
|
22
33
|
body("/about").should == ''
|
|
34
|
+
|
|
35
|
+
status("/foo", 'REQUEST_METHOD'=>'POST').should == 302
|
|
36
|
+
header('Location', "/foo", 'REQUEST_METHOD'=>'POST').should == '/foo'
|
|
37
|
+
body("/foo", 'REQUEST_METHOD'=>'POST').should == ''
|
|
38
|
+
|
|
39
|
+
proc{req('/foo')}.should raise_error(Roda::RodaError)
|
|
23
40
|
end
|
|
24
41
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: roda-cj
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.9.
|
|
4
|
+
version: 0.9.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jeremy Evans
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-08-
|
|
11
|
+
date: 2014-08-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rack
|