mynyml-simple_router 0.8 → 0.8.1
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/README +76 -0
- data/Rakefile +3 -3
- data/lib/simple_router.rb +5 -0
- data/lib/simple_router/routes.rb +3 -8
- data/simple_router.gemspec +4 -4
- data/test/test_routes.rb +6 -8
- data/test/test_simple_router.rb +23 -0
- metadata +4 -4
data/README
CHANGED
@@ -0,0 +1,76 @@
|
|
1
|
+
Small and simple standalone router, meant for use with Rack applications.
|
2
|
+
|
3
|
+
PROBLEMS/FEATURES
|
4
|
+
o Familiar Sinatra-like DSL for defining actions
|
5
|
+
o Modular architecture (see ROUTING ENGINES section)
|
6
|
+
o No magic
|
7
|
+
o Low level / Highly flexible
|
8
|
+
o Simple code
|
9
|
+
|
10
|
+
|
11
|
+
EXAMPLES
|
12
|
+
class App
|
13
|
+
include SimpleRouter::DSL
|
14
|
+
|
15
|
+
get '/' do
|
16
|
+
'home'
|
17
|
+
end
|
18
|
+
|
19
|
+
get '/archives/:year/:month/:day' do |year, month, day|
|
20
|
+
Article.archived.find_by_date(year, month, day)
|
21
|
+
end
|
22
|
+
|
23
|
+
def call(env)
|
24
|
+
verb, path = ENV['REQUEST_METHOD'], ENV['PATH_INFO']
|
25
|
+
route, args = self.class.routes.match(verb, path)
|
26
|
+
route.nil? ?
|
27
|
+
[404, {'Content-Type' => 'text/html'}, '404 page not found'] :
|
28
|
+
[200, {'Content-Type' => 'text/html'}, route.action.call(*args)]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
The SimpleRouter::DSL mixin provides 5 class methods:
|
33
|
+
|
34
|
+
#get, #post, #put, #delete and #routes
|
35
|
+
|
36
|
+
The four verb methods allow you to define actions that will match a request for
|
37
|
+
the verb and route signature it defines.
|
38
|
+
|
39
|
+
The #routes method responds to #match and will return the first matching route
|
40
|
+
(in order they were defined), and the values of the variables defined in the
|
41
|
+
route signature if any (:year, :month, ...).
|
42
|
+
|
43
|
+
Finally, the route returned is a simple object: for the second route in the
|
44
|
+
example above:
|
45
|
+
|
46
|
+
route.verb = :get
|
47
|
+
route.path = '/archives/:year/:month/:day'
|
48
|
+
route.action = lambda {|year, month, day| Article.archived.find_by_date(year, month, day) }
|
49
|
+
|
50
|
+
See examples/ directory for executable code examples.
|
51
|
+
|
52
|
+
|
53
|
+
ROUTING ENGINES
|
54
|
+
By design, the code is seperated in two main components:
|
55
|
+
|
56
|
+
o DSL for defining routes/actions and retrieving requested action block.
|
57
|
+
o Routing engine
|
58
|
+
|
59
|
+
Different engines can be used by assigning them to the router:
|
60
|
+
|
61
|
+
SimpleRouter.engine = UltraFastEngine
|
62
|
+
|
63
|
+
This allows, for instance, a new faster engine to be used by an app without
|
64
|
+
changes in the top level DSL.
|
65
|
+
|
66
|
+
Engines can also allow route definitions to include more (or less!) features.
|
67
|
+
For example, the built-in SimpleEngine allows route variables and extention
|
68
|
+
handling. Others could add variable conditions ( :id => Integer ), mime-type
|
69
|
+
restrictions, etc.
|
70
|
+
|
71
|
+
ENGINE WRITERS
|
72
|
+
Engines only need to conform to a simple interface. See the wiki for the
|
73
|
+
details. (TODO: add interface spec to wiki)
|
74
|
+
|
75
|
+
LICENSE
|
76
|
+
MIT. See LICENSE file.
|
data/Rakefile
CHANGED
@@ -22,9 +22,9 @@ end
|
|
22
22
|
|
23
23
|
spec = Gem::Specification.new do |s|
|
24
24
|
s.name = 'simple_router'
|
25
|
-
s.version = '0.8'
|
26
|
-
s.summary = "
|
27
|
-
s.description = "
|
25
|
+
s.version = '0.8.1'
|
26
|
+
s.summary = "Simple, minimalistic, standalone router meant to be used with pure rack applications."
|
27
|
+
s.description = "Simple, minimalistic, standalone router meant to be used with pure rack applications."
|
28
28
|
s.author = "Martin Aumont"
|
29
29
|
s.email = 'mynyml@gmail.com'
|
30
30
|
s.homepage = ''
|
data/lib/simple_router.rb
CHANGED
data/lib/simple_router/routes.rb
CHANGED
@@ -1,13 +1,6 @@
|
|
1
1
|
module SimpleRouter
|
2
2
|
class Routes < Array
|
3
3
|
|
4
|
-
# routing engine
|
5
|
-
attr_accessor :engine
|
6
|
-
|
7
|
-
def engine
|
8
|
-
@engine || Engines::SimpleEngine
|
9
|
-
end
|
10
|
-
|
11
4
|
def add(*args, &action)
|
12
5
|
self << Route.new(*args, &action)
|
13
6
|
end
|
@@ -16,10 +9,12 @@ module SimpleRouter
|
|
16
9
|
none = [nil, nil]
|
17
10
|
return none if self.empty?
|
18
11
|
|
12
|
+
verb = verb.to_s.downcase.strip.to_sym
|
13
|
+
|
19
14
|
routes = self.select {|route| route.verb == verb }
|
20
15
|
paths = routes.map {|route| route.path }
|
21
16
|
|
22
|
-
path, vars =
|
17
|
+
path, vars = SimpleRouter.engine.match(path, paths)
|
23
18
|
return none if path.nil?
|
24
19
|
|
25
20
|
route = routes.detect {|route| route.path == path }
|
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:
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Aumont
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-19 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description:
|
16
|
+
description: Simple, minimalistic, standalone router meant to be used with pure rack applications.
|
17
17
|
email: mynyml@gmail.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -71,6 +71,6 @@ rubyforge_project:
|
|
71
71
|
rubygems_version: 1.3.3
|
72
72
|
signing_key:
|
73
73
|
specification_version: 3
|
74
|
-
summary:
|
74
|
+
summary: Simple, minimalistic, standalone router meant to be used with pure rack applications.
|
75
75
|
test_files: []
|
76
76
|
|
data/test/test_routes.rb
CHANGED
@@ -31,15 +31,13 @@ class RoutesTest < Test::Unit::TestCase
|
|
31
31
|
@routes.match('/baz', :get).should be([nil,nil])
|
32
32
|
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
@routes.engine.name.split('::').last.should be('SimpleEngine')
|
38
|
-
end
|
34
|
+
test "normalizes passed in verb string" do
|
35
|
+
@routes.add(:get, '/foo', {}, &@action)
|
36
|
+
@routes.add(:get, '/bar', {}, &@action)
|
39
37
|
|
40
|
-
|
41
|
-
@routes.
|
42
|
-
@routes.
|
38
|
+
@routes.match('get', '/bar').first.path.should be('/bar')
|
39
|
+
@routes.match('GET', '/bar').first.path.should be('/bar')
|
40
|
+
@routes.match(' GET ','/bar').first.path.should be('/bar')
|
43
41
|
end
|
44
42
|
end
|
45
43
|
|
data/test/test_simple_router.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
|
3
|
+
class App
|
4
|
+
include SimpleRouter::DSL
|
5
|
+
end
|
6
|
+
|
7
|
+
class SimpleRouterTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
SimpleRouter.engine = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
## engine
|
14
|
+
|
15
|
+
test "default engine" do
|
16
|
+
SimpleRouter.engine.name.split('::').last.should be('SimpleEngine')
|
17
|
+
end
|
18
|
+
|
19
|
+
test "custom engine" do
|
20
|
+
SimpleRouter.engine = ::Object
|
21
|
+
SimpleRouter.engine.name.should be('Object')
|
22
|
+
end
|
23
|
+
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:
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Aumont
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-18 21:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description:
|
16
|
+
description: Simple, minimalistic, standalone router meant to be used with pure rack applications.
|
17
17
|
email: mynyml@gmail.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -69,6 +69,6 @@ rubyforge_project:
|
|
69
69
|
rubygems_version: 1.2.0
|
70
70
|
signing_key:
|
71
71
|
specification_version: 3
|
72
|
-
summary:
|
72
|
+
summary: Simple, minimalistic, standalone router meant to be used with pure rack applications.
|
73
73
|
test_files: []
|
74
74
|
|