mynyml-simple_router 0.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/LICENSE +19 -0
- data/README +0 -0
- data/Rakefile +66 -0
- data/TODO +1 -0
- data/lib/simple_router/dsl.rb +57 -0
- data/lib/simple_router/engines/simple_engine.rb +30 -0
- data/lib/simple_router/routes.rb +36 -0
- data/lib/simple_router.rb +8 -0
- data/simple_router.gemspec +74 -0
- data/test/engines/test_simple_engine.rb +12 -0
- data/test/test_dsl.rb +38 -0
- data/test/test_helper.rb +21 -0
- data/test/test_routes.rb +57 -0
- data/test/test_simple_router.rb +0 -0
- metadata +72 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright © 2009 Martin Aumont (mynyml)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# --------------------------------------------------
|
2
|
+
# based on thin's Rakefile (http://github.com/macournoyer/thin)
|
3
|
+
# --------------------------------------------------
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
require 'pathname'
|
7
|
+
require 'yaml'
|
8
|
+
|
9
|
+
RUBY_1_9 = RUBY_VERSION =~ /^1\.9/
|
10
|
+
WIN = (RUBY_PLATFORM =~ /mswin|cygwin/)
|
11
|
+
SUDO = (WIN ? "" : "sudo")
|
12
|
+
|
13
|
+
def gem
|
14
|
+
RUBY_1_9 ? 'gem19' : 'gem'
|
15
|
+
end
|
16
|
+
|
17
|
+
def all_except(res)
|
18
|
+
Dir['**/*'].reject do |path|
|
19
|
+
Array(res).any? {|re| path.match(re) }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
spec = Gem::Specification.new do |s|
|
24
|
+
s.name = 'simple_router'
|
25
|
+
s.version = '0.1'
|
26
|
+
s.summary = "Minimalistic, simple router meant to be used with pure rack applications."
|
27
|
+
s.description = "Minimalistic, simple router meant to be used with pure rack applications."
|
28
|
+
s.author = "Martin Aumont"
|
29
|
+
s.email = 'mynyml@gmail.com'
|
30
|
+
s.homepage = ''
|
31
|
+
s.has_rdoc = true
|
32
|
+
s.require_path = "lib"
|
33
|
+
s.files = all_except(/doc\/.*/)
|
34
|
+
end
|
35
|
+
|
36
|
+
Rake::GemPackageTask.new(spec) do |p|
|
37
|
+
p.gem_spec = spec
|
38
|
+
end
|
39
|
+
|
40
|
+
desc "Remove package products"
|
41
|
+
task :clean => :clobber_package
|
42
|
+
|
43
|
+
desc "Update the gemspec for GitHub's gem server"
|
44
|
+
task :gemspec do
|
45
|
+
Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) }
|
46
|
+
end
|
47
|
+
|
48
|
+
desc "Install gem"
|
49
|
+
task :install => [:clobber, :package] do
|
50
|
+
sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem"
|
51
|
+
end
|
52
|
+
|
53
|
+
desc "Uninstall gem"
|
54
|
+
task :uninstall => :clean do
|
55
|
+
sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}"
|
56
|
+
end
|
57
|
+
|
58
|
+
desc "Generate rdoc documentation."
|
59
|
+
Rake::RDocTask.new("rdoc") { |rdoc|
|
60
|
+
rdoc.rdoc_dir = 'doc/rdoc'
|
61
|
+
rdoc.title = "Simple Router - Document"
|
62
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
63
|
+
rdoc.options << '--charset' << 'utf-8'
|
64
|
+
rdoc.rdoc_files.include('README')
|
65
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
66
|
+
}
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module SimpleRouter
|
2
|
+
|
3
|
+
# Mixin that provides a Sinatra-line DSL frontend to the routing engine
|
4
|
+
# backend.
|
5
|
+
#
|
6
|
+
# Meant to be minimal, simple and as magic-free as possible.
|
7
|
+
# When mixed in, only adds 5 class methods (#get, #post, #put, #delete and #routes).
|
8
|
+
#
|
9
|
+
# ==== Examples
|
10
|
+
# # simple rack app
|
11
|
+
#
|
12
|
+
# class App
|
13
|
+
# include SimpleRouter::DSL
|
14
|
+
#
|
15
|
+
# get '/' do
|
16
|
+
# 'home'
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# get '/users/:id' do |id|
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# put '/:foo/:bar' do |foo, bar, *params|
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# def call(env)
|
26
|
+
# request = Rack::Request.new(env)
|
27
|
+
#
|
28
|
+
# verb = request.request_method
|
29
|
+
# path = Rack::Utils.unescape(request.path_info)
|
30
|
+
#
|
31
|
+
# action = self.class.routes.match(verb, path).action
|
32
|
+
# action.nil? ? [404, {}, []] : [200, {}, [action.call]]
|
33
|
+
# end
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# ==== Notes
|
37
|
+
# Because the DSL is a simple mixin, it can be used in any class (i.e. not
|
38
|
+
# necessarily a rack app).
|
39
|
+
#
|
40
|
+
module DSL
|
41
|
+
def self.included(base)
|
42
|
+
base.extend(ClassMethods)
|
43
|
+
base.class_eval do
|
44
|
+
@routes = Routes.new
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
attr_reader :routes
|
50
|
+
|
51
|
+
def get( path, opts={}, &block) routes.add(:get, path, opts, &block) end
|
52
|
+
def post( path, opts={}, &block) routes.add(:post, path, opts, &block) end
|
53
|
+
def put( path, opts={}, &block) routes.add(:put, path, opts, &block) end
|
54
|
+
def delete(path, opts={}, &block) routes.add(:delete, path, opts, &block) end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module SimpleRouter
|
2
|
+
module Engines
|
3
|
+
class SimpleEngine
|
4
|
+
|
5
|
+
# Finds a route definition that matches a path
|
6
|
+
#
|
7
|
+
# ===== Arguments
|
8
|
+
# * path: actual path to match (e.g. ENV['PATH_INFO'])
|
9
|
+
# * routes: array of user defined routes
|
10
|
+
#
|
11
|
+
# ===== Returns
|
12
|
+
# Array of two elements:
|
13
|
+
#
|
14
|
+
# * index 0: first matching route
|
15
|
+
# * index 1: array of values for route variables, in the order they were specified
|
16
|
+
#
|
17
|
+
# ===== Examples
|
18
|
+
#
|
19
|
+
# SimpleEngine.match('/foo', ['/', '/foo', '/bar/baz']) #=> ['/foo', []]
|
20
|
+
# SimpleEngine.match('/80/07/01', ['/:year/:month/:day']) #=> ['/foo', ['80', '07', '01']]
|
21
|
+
#
|
22
|
+
def self.match(path, routes)
|
23
|
+
routes.each do |route|
|
24
|
+
return route if route == path
|
25
|
+
end
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module SimpleRouter
|
2
|
+
class Routes < Array #:nodoc:
|
3
|
+
|
4
|
+
# routing engine
|
5
|
+
attr_accessor :engine
|
6
|
+
|
7
|
+
def engine
|
8
|
+
@engine || Engines::SimpleEngine
|
9
|
+
end
|
10
|
+
|
11
|
+
def add(*args, &action)
|
12
|
+
self << Route.new(*args, &action)
|
13
|
+
end
|
14
|
+
|
15
|
+
def match(verb, path)
|
16
|
+
unless self.empty?
|
17
|
+
routes = self.select {|route| route.verb == verb }
|
18
|
+
paths = routes.map {|route| route.path }
|
19
|
+
|
20
|
+
path = self.engine.match(path, paths)
|
21
|
+
routes.detect {|route| route.path == path }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Route < Array #:nodoc:
|
26
|
+
attr_accessor :verb,:path,:options,:action
|
27
|
+
|
28
|
+
def initialize(verb, path, options, &action)
|
29
|
+
self.verb = verb
|
30
|
+
self.path = path
|
31
|
+
self.options = options
|
32
|
+
self.action = action
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_router
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Aumont
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-18 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Minimalistic, simple router meant to be used with pure rack applications.
|
17
|
+
email: mynyml@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- test
|
27
|
+
- test/engines
|
28
|
+
- test/engines/test_simple_engine.rb
|
29
|
+
- test/test_simple_router.rb
|
30
|
+
- test/test_dsl.rb
|
31
|
+
- test/test_routes.rb
|
32
|
+
- test/test_helper.rb
|
33
|
+
- simple_router.gemspec
|
34
|
+
- TODO
|
35
|
+
- lib
|
36
|
+
- lib/simple_router
|
37
|
+
- lib/simple_router/routes.rb
|
38
|
+
- lib/simple_router/engines
|
39
|
+
- lib/simple_router/engines/simple_engine.rb
|
40
|
+
- lib/simple_router/dsl.rb
|
41
|
+
- lib/simple_router.rb
|
42
|
+
- doc
|
43
|
+
- LICENSE
|
44
|
+
- README
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: ""
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.3
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Minimalistic, simple router meant to be used with pure rack applications.
|
73
|
+
test_files: []
|
74
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
|
3
|
+
Engine = SimpleRouter::Engines::SimpleEngine
|
4
|
+
|
5
|
+
class SimpleEngineTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
test "matches simple paths" do
|
8
|
+
Engine.match('/', ['/', '/foo']).should be('/')
|
9
|
+
Engine.match('/foo', ['/', '/foo']).should be('/foo')
|
10
|
+
Engine.match('/bar', ['/', '/foo']).should be(nil)
|
11
|
+
end
|
12
|
+
end
|
data/test/test_dsl.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
|
3
|
+
class App
|
4
|
+
include SimpleRouter::DSL
|
5
|
+
end
|
6
|
+
|
7
|
+
class DslTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
App.routes.clear
|
11
|
+
end
|
12
|
+
|
13
|
+
## API
|
14
|
+
|
15
|
+
test "provides action verb methods" do
|
16
|
+
App.get( '/foo') {}
|
17
|
+
App.post( '/foo') {}
|
18
|
+
App.put( '/foo') {}
|
19
|
+
App.delete('/foo') {}
|
20
|
+
|
21
|
+
App.routes.size.should be(4)
|
22
|
+
end
|
23
|
+
|
24
|
+
test "provides routes object" do
|
25
|
+
App.respond_to?(:routes).should be(true)
|
26
|
+
App.routes.should be_kind_of(SimpleRouter::Routes)
|
27
|
+
end
|
28
|
+
|
29
|
+
## matching
|
30
|
+
|
31
|
+
test "matching routes" do
|
32
|
+
App.get('/foo') { 'foo' }
|
33
|
+
App.get('/bar') { 'bar' }
|
34
|
+
|
35
|
+
App.routes.match(:get, '/foo').should_not be( nil )
|
36
|
+
App.routes.match(:get, '/foo').action.call.should be('foo')
|
37
|
+
end
|
38
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'matchy'
|
5
|
+
require 'pending'
|
6
|
+
begin
|
7
|
+
require 'ruby-debug'
|
8
|
+
rescue LoadError, RuntimeError
|
9
|
+
end
|
10
|
+
|
11
|
+
root = Pathname(__FILE__).dirname.parent.expand_path
|
12
|
+
$:.unshift(root.join('lib'))
|
13
|
+
|
14
|
+
require 'simple_router'
|
15
|
+
|
16
|
+
class Test::Unit::TestCase
|
17
|
+
def self.test(name, &block)
|
18
|
+
name = :"test_#{name.gsub(/\s/,'_')}"
|
19
|
+
define_method(name, &block)
|
20
|
+
end
|
21
|
+
end
|
data/test/test_routes.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
|
3
|
+
Routes = SimpleRouter::Routes
|
4
|
+
|
5
|
+
class RoutesTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@routes = Routes.new
|
9
|
+
@action = lambda {}
|
10
|
+
end
|
11
|
+
|
12
|
+
test "stores route definitions" do
|
13
|
+
@routes.add(:get, '/foo', {}, &@action)
|
14
|
+
@routes.first.path.should be('/foo')
|
15
|
+
end
|
16
|
+
|
17
|
+
## matching
|
18
|
+
|
19
|
+
test "matches a path" do
|
20
|
+
@routes.add(:get, '/foo', {}, &@action)
|
21
|
+
@routes.add(:get, '/bar', {}, &@action)
|
22
|
+
|
23
|
+
@routes.match(:get, '/bar').should_not be(nil)
|
24
|
+
@routes.match(:get, '/bar').path.should be('/bar')
|
25
|
+
end
|
26
|
+
|
27
|
+
test "returns nil when no route matches" do
|
28
|
+
@routes.add(:get, '/foo', {}, &@action)
|
29
|
+
@routes.add(:get, '/bar', {}, &@action)
|
30
|
+
|
31
|
+
@routes.match('/baz', :get).should be(nil)
|
32
|
+
end
|
33
|
+
|
34
|
+
## engine
|
35
|
+
|
36
|
+
test "default engine" do
|
37
|
+
@routes.engine.name.split('::').last.should be('SimpleEngine')
|
38
|
+
end
|
39
|
+
|
40
|
+
test "custom engine" do
|
41
|
+
@routes.engine = ::Object
|
42
|
+
@routes.engine.name.should be('Object')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class RouteTest < Test::Unit::TestCase
|
47
|
+
|
48
|
+
test "internal API" do
|
49
|
+
verb, path, options, action = :get, '/foo', {}, lambda {}
|
50
|
+
|
51
|
+
route = SimpleRouter::Routes::Route.new(verb, path, options, &action)
|
52
|
+
route.verb .should be(verb)
|
53
|
+
route.path .should be(path)
|
54
|
+
route.options .should be(options)
|
55
|
+
route.action .should be(action)
|
56
|
+
end
|
57
|
+
end
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mynyml-simple_router
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Aumont
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-17 21:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Minimalistic, simple router meant to be used with pure rack applications.
|
17
|
+
email: mynyml@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- test
|
27
|
+
- test/engines
|
28
|
+
- test/engines/test_simple_engine.rb
|
29
|
+
- test/test_simple_router.rb
|
30
|
+
- test/test_dsl.rb
|
31
|
+
- test/test_routes.rb
|
32
|
+
- test/test_helper.rb
|
33
|
+
- simple_router.gemspec
|
34
|
+
- TODO
|
35
|
+
- lib
|
36
|
+
- lib/simple_router
|
37
|
+
- lib/simple_router/routes.rb
|
38
|
+
- lib/simple_router/engines
|
39
|
+
- lib/simple_router/engines/simple_engine.rb
|
40
|
+
- lib/simple_router/dsl.rb
|
41
|
+
- lib/simple_router.rb
|
42
|
+
- doc
|
43
|
+
- LICENSE
|
44
|
+
- README
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: ""
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.2.0
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Minimalistic, simple router meant to be used with pure rack applications.
|
71
|
+
test_files: []
|
72
|
+
|