mynyml-simple_router 0.8.1 → 0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README +4 -3
- data/Rakefile +1 -1
- data/examples/rack_app.ru +2 -2
- data/lib/simple_router/routes.rb +7 -6
- data/simple_router.gemspec +2 -2
- data/test/engines/test_simple_engine.rb +8 -8
- data/test/test_dsl.rb +1 -1
- data/test/test_routes.rb +6 -5
- metadata +2 -2
data/README
CHANGED
@@ -22,10 +22,10 @@ class App
|
|
22
22
|
|
23
23
|
def call(env)
|
24
24
|
verb, path = ENV['REQUEST_METHOD'], ENV['PATH_INFO']
|
25
|
-
route
|
25
|
+
route = self.class.routes.match(verb, path)
|
26
26
|
route.nil? ?
|
27
27
|
[404, {'Content-Type' => 'text/html'}, '404 page not found'] :
|
28
|
-
[200, {'Content-Type' => 'text/html'}, route.action.call(*
|
28
|
+
[200, {'Content-Type' => 'text/html'}, route.action.call(*route.values)]
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -46,6 +46,7 @@ example above:
|
|
46
46
|
route.verb = :get
|
47
47
|
route.path = '/archives/:year/:month/:day'
|
48
48
|
route.action = lambda {|year, month, day| Article.archived.find_by_date(year, month, day) }
|
49
|
+
route.values = ['2008', '07', '01'] # after being matched with path '/archives/2008/07/01'
|
49
50
|
|
50
51
|
See examples/ directory for executable code examples.
|
51
52
|
|
@@ -64,7 +65,7 @@ This allows, for instance, a new faster engine to be used by an app without
|
|
64
65
|
changes in the top level DSL.
|
65
66
|
|
66
67
|
Engines can also allow route definitions to include more (or less!) features.
|
67
|
-
For example, the built-in SimpleEngine allows route variables and
|
68
|
+
For example, the built-in SimpleEngine allows route variables and extension
|
68
69
|
handling. Others could add variable conditions ( :id => Integer ), mime-type
|
69
70
|
restrictions, etc.
|
70
71
|
|
data/Rakefile
CHANGED
@@ -22,7 +22,7 @@ end
|
|
22
22
|
|
23
23
|
spec = Gem::Specification.new do |s|
|
24
24
|
s.name = 'simple_router'
|
25
|
-
s.version = '0.
|
25
|
+
s.version = '0.9'
|
26
26
|
s.summary = "Simple, minimalistic, standalone router meant to be used with pure rack applications."
|
27
27
|
s.description = "Simple, minimalistic, standalone router meant to be used with pure rack applications."
|
28
28
|
s.author = "Martin Aumont"
|
data/examples/rack_app.ru
CHANGED
@@ -35,10 +35,10 @@ class App
|
|
35
35
|
verb = request.request_method.downcase.to_sym
|
36
36
|
path = Rack::Utils.unescape(request.path_info)
|
37
37
|
|
38
|
-
route
|
38
|
+
route = self.class.routes.match(verb, path)
|
39
39
|
route.nil? ?
|
40
40
|
[404, {'Content-Type' => 'text/html'}, '404 page not found'] :
|
41
|
-
[200, {'Content-Type' => 'text/html'}, [route.action.call(*
|
41
|
+
[200, {'Content-Type' => 'text/html'}, [route.action.call(*route.values.push(request.params))]]
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
data/lib/simple_router/routes.rb
CHANGED
@@ -6,29 +6,30 @@ module SimpleRouter
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def match(verb, path)
|
9
|
-
|
10
|
-
return none if self.empty?
|
9
|
+
return nil if self.empty?
|
11
10
|
|
12
11
|
verb = verb.to_s.downcase.strip.to_sym
|
13
12
|
|
14
13
|
routes = self.select {|route| route.verb == verb }
|
15
14
|
paths = routes.map {|route| route.path }
|
16
15
|
|
17
|
-
path,
|
18
|
-
return
|
16
|
+
path, values = SimpleRouter.engine.match(path, paths)
|
17
|
+
return nil if path.nil?
|
19
18
|
|
20
19
|
route = routes.detect {|route| route.path == path }
|
21
|
-
|
20
|
+
route.values = values
|
21
|
+
route
|
22
22
|
end
|
23
23
|
|
24
24
|
class Route
|
25
|
-
attr_accessor :verb,:path,:options,:action
|
25
|
+
attr_accessor :verb,:path,:options,:action, :values
|
26
26
|
|
27
27
|
def initialize(verb, path, options, &action)
|
28
28
|
self.verb = verb
|
29
29
|
self.path = path
|
30
30
|
self.options = options
|
31
31
|
self.action = action
|
32
|
+
self.values = nil
|
32
33
|
end
|
33
34
|
end
|
34
35
|
end
|
data/simple_router.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: "0.9"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Aumont
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-23 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -33,7 +33,7 @@ class SimpleEngineTest < Test::Unit::TestCase
|
|
33
33
|
vars.should be([])
|
34
34
|
end
|
35
35
|
|
36
|
-
test "treats
|
36
|
+
test "treats extension as pattern part" do
|
37
37
|
path, vars = Base.match('/a/b.xml', ['/:foo/:bar', '/:foo/:bar.:type'])
|
38
38
|
path.should be('/:foo/:bar.:type')
|
39
39
|
vars.should be(['a','b','xml'])
|
@@ -65,7 +65,7 @@ class PatternTest < Test::Unit::TestCase
|
|
65
65
|
pattern.vars.should be(%w( foo bar baz ))
|
66
66
|
end
|
67
67
|
|
68
|
-
test "pattern variables with
|
68
|
+
test "pattern variables with extension" do
|
69
69
|
path = Path.new('/foo/bar/baz.xml')
|
70
70
|
pattern = Pattern.new('/:a/:b/:c.:type')
|
71
71
|
|
@@ -73,35 +73,35 @@ class PatternTest < Test::Unit::TestCase
|
|
73
73
|
pattern.vars.should be(%w( foo bar baz xml ))
|
74
74
|
end
|
75
75
|
|
76
|
-
test "variable pattern matches a path with static
|
76
|
+
test "variable pattern matches a path with static extension" do
|
77
77
|
path = Path.new('/foo/bar.xml')
|
78
78
|
pattern = Pattern.new('/:foo/:bar.xml')
|
79
79
|
|
80
80
|
assert pattern == path
|
81
81
|
end
|
82
82
|
|
83
|
-
test "variable pattern matches a path with variable
|
83
|
+
test "variable pattern matches a path with variable extension" do
|
84
84
|
path = Path.new('/foo/bar.xml')
|
85
85
|
pattern = Pattern.new('/:foo/:bar.:type')
|
86
86
|
|
87
87
|
assert pattern == path
|
88
88
|
end
|
89
89
|
|
90
|
-
test "pattern without
|
90
|
+
test "pattern without extension doesn't match path with extension" do
|
91
91
|
path = Path.new('/foo/bar.xml')
|
92
92
|
pattern = Pattern.new('/:foo/:bar')
|
93
93
|
|
94
94
|
assert pattern != path
|
95
95
|
end
|
96
96
|
|
97
|
-
test "pattern with static
|
97
|
+
test "pattern with static extension doesn't match path without extension" do
|
98
98
|
path = Path.new('/foo/bar')
|
99
99
|
pattern = Pattern.new('/:foo/:bar.xml')
|
100
100
|
|
101
101
|
assert pattern != path
|
102
102
|
end
|
103
103
|
|
104
|
-
test "pattern with variable
|
104
|
+
test "pattern with variable extension doesn't match path without extension" do
|
105
105
|
path = Path.new('/foo/bar')
|
106
106
|
pattern = Pattern.new('/:foo/:bar.:type')
|
107
107
|
|
@@ -116,7 +116,7 @@ class PatternTest < Test::Unit::TestCase
|
|
116
116
|
pattern.variables.should be(%w( foo bar.baz abc ))
|
117
117
|
end
|
118
118
|
|
119
|
-
test "doesn't get confused with
|
119
|
+
test "doesn't get confused with extension when path contains other dots" do
|
120
120
|
path = Path.new('/foo/bar.baz/abc.xml')
|
121
121
|
pattern = Pattern.new('/:a/:b/:c.:type')
|
122
122
|
|
data/test/test_dsl.rb
CHANGED
@@ -33,6 +33,6 @@ class DslTest < Test::Unit::TestCase
|
|
33
33
|
App.get('/bar') { 'bar' }
|
34
34
|
|
35
35
|
App.routes.match(:get, '/foo').should_not be( nil )
|
36
|
-
App.routes.match(:get, '/foo').
|
36
|
+
App.routes.match(:get, '/foo').action.call.should be('foo')
|
37
37
|
end
|
38
38
|
end
|
data/test/test_routes.rb
CHANGED
@@ -21,23 +21,23 @@ class RoutesTest < Test::Unit::TestCase
|
|
21
21
|
@routes.add(:get, '/bar', {}, &@action)
|
22
22
|
|
23
23
|
@routes.match(:get, '/bar').should_not be(nil)
|
24
|
-
@routes.match(:get, '/bar').
|
24
|
+
@routes.match(:get, '/bar').path.should be('/bar')
|
25
25
|
end
|
26
26
|
|
27
27
|
test "returns nil when no route matches" do
|
28
28
|
@routes.add(:get, '/foo', {}, &@action)
|
29
29
|
@routes.add(:get, '/bar', {}, &@action)
|
30
30
|
|
31
|
-
@routes.match('/baz', :get).should be(
|
31
|
+
@routes.match('/baz', :get).should be(nil)
|
32
32
|
end
|
33
33
|
|
34
34
|
test "normalizes passed in verb string" do
|
35
35
|
@routes.add(:get, '/foo', {}, &@action)
|
36
36
|
@routes.add(:get, '/bar', {}, &@action)
|
37
37
|
|
38
|
-
@routes.match('get', '/bar').
|
39
|
-
@routes.match('GET', '/bar').
|
40
|
-
@routes.match(' GET ','/bar').
|
38
|
+
@routes.match('get', '/bar').path.should be('/bar')
|
39
|
+
@routes.match('GET', '/bar').path.should be('/bar')
|
40
|
+
@routes.match(' GET ','/bar').path.should be('/bar')
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -51,5 +51,6 @@ class RouteTest < Test::Unit::TestCase
|
|
51
51
|
route.path .should be(path)
|
52
52
|
route.options .should be(options)
|
53
53
|
route.action .should be(action)
|
54
|
+
route.values .should be(nil)
|
54
55
|
end
|
55
56
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mynyml-simple_router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: "0.9"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Aumont
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-22 21:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|