iwamot-rupta 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +70 -0
- data/lib/rupta.rb +22 -0
- data/lib/rupta/factory.rb +17 -0
- data/lib/rupta/uri_extract_processor/default.rb +9 -0
- data/lib/rupta/uri_extractor.rb +18 -0
- data/lib/rupta/uri_purifier.rb +10 -0
- data/lib/rupta/yaml_loader.rb +9 -0
- data/spec/routing_with_hash_spec.rb +49 -0
- data/spec/routing_with_yaml_spec.rb +43 -0
- metadata +71 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 IWAMOTO Takashi
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
= README
|
2
|
+
|
3
|
+
== Descrption
|
4
|
+
|
5
|
+
Rupta is a router library for Web applications. The routes can be defined with {URI Template}[http://bitworking.org/projects/URI-Templates/]. The usage is so simple. See the following examples.
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
$ sudo gem install iwamot-rupta --source http://gems.github.com
|
10
|
+
|
11
|
+
== Examples
|
12
|
+
|
13
|
+
=== examples/fixtures/routes.yml
|
14
|
+
|
15
|
+
bookmark:
|
16
|
+
- http://example.com/bookmarks/{bookmark_id}
|
17
|
+
- http://example.com/bookmarks/{bookmark_id}.{format}
|
18
|
+
static_path:
|
19
|
+
- http://example.com/static_path
|
20
|
+
search:
|
21
|
+
- http://example.com/search
|
22
|
+
|
23
|
+
=== examples/routing_with_yaml.rb
|
24
|
+
|
25
|
+
yaml_path = File.expand_path('fixtures/routes.yml', File.dirname(__FILE__))
|
26
|
+
|
27
|
+
require 'rupta/factory'
|
28
|
+
rupta = Rupta::Factory.new.create
|
29
|
+
|
30
|
+
# Rupta#detect returns the name of a detected route, a matched URI template and extracted URI parameters
|
31
|
+
p rupta.detect(yaml_path, 'http://example.com/bookmarks/123.atom')
|
32
|
+
# => ["user", "http://example.com/bookmarks/{bookmark_id}.{format}", {"bookmark_id"=>"123", "format"=>"atom"}]
|
33
|
+
|
34
|
+
# if the uri is static, the extracted parameters is empty
|
35
|
+
p rupta.detect(yaml_path, 'http://example.com/static_path')
|
36
|
+
# => ["static_path", "http://example.com/static_path", {}]
|
37
|
+
|
38
|
+
# query strings are omitted
|
39
|
+
p rupta.detect(yaml_path, 'http://example.com/search?q=foo')
|
40
|
+
# => ["search", "http://example.com/search", {}]
|
41
|
+
|
42
|
+
# when no route is found, Rupta#detect returns nil
|
43
|
+
p rupta.detect(yaml_path, 'http://example.com/not_found')
|
44
|
+
# => nil
|
45
|
+
|
46
|
+
=== examples/routing_with_hash.rb
|
47
|
+
|
48
|
+
routes = {'bookmark' => ['http://example.com/bookmarks/{bookmark_id}',
|
49
|
+
'http://example.com/bookmarks/{bookmark_id}.{format}'],
|
50
|
+
'static_path' => ['http://example.com/static_path'],
|
51
|
+
'search' => ['http://example.com/search']}
|
52
|
+
|
53
|
+
require 'rupta/factory'
|
54
|
+
rupta = Rupta::Factory.new.create
|
55
|
+
|
56
|
+
# use Rupta#detect_with_hash, if you can create the routes manually
|
57
|
+
p rupta.detect_with_hash(route, 'http://example.com/bookmarks/123.atom')
|
58
|
+
# => ["user", "http://example.com/bookmarks/{bookmark_id}.{format}", {"bookmark_id"=>"123", "format"=>"atom"}]
|
59
|
+
|
60
|
+
== Requirement
|
61
|
+
|
62
|
+
Rupta-0.9.0 was tested with Ruby 1.8.7 and 1.9.1.
|
63
|
+
|
64
|
+
== Dependencies
|
65
|
+
|
66
|
+
* addressable-2.1.0
|
67
|
+
|
68
|
+
== Copyright
|
69
|
+
|
70
|
+
Copyright (c) 2009 IWAMOTO Takashi. See LICENSE for details.
|
data/lib/rupta.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class Rupta
|
2
|
+
def initialize(uri_purifier, uri_extractor, yaml_loader)
|
3
|
+
@uri_purifier = uri_purifier
|
4
|
+
@uri_extractor = uri_extractor
|
5
|
+
@yaml_loader = yaml_loader
|
6
|
+
end
|
7
|
+
|
8
|
+
def detect_with_hash(routes, uri, omittable_uri_components = [:query])
|
9
|
+
pure_uri = @uri_purifier.purify(uri, omittable_uri_components)
|
10
|
+
routes.each do |route_name, uri_templates|
|
11
|
+
uri_template, uri_parameters = @uri_extractor.extract(uri_templates, pure_uri)
|
12
|
+
return [route_name, uri_template, uri_parameters] if uri_template
|
13
|
+
end
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def detect_with_file(yaml_path, uri, omittable_uri_components = [:query])
|
18
|
+
self.detect_with_hash(@yaml_loader.load_file(yaml_path), uri, omittable_uri_components)
|
19
|
+
end
|
20
|
+
|
21
|
+
alias :detect :detect_with_file
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rupta'
|
2
|
+
require 'rupta/uri_extract_processor/default'
|
3
|
+
require 'rupta/uri_extractor'
|
4
|
+
require 'rupta/uri_purifier'
|
5
|
+
require 'rupta/yaml_loader'
|
6
|
+
|
7
|
+
class Rupta
|
8
|
+
class Factory
|
9
|
+
def create(uri_extract_processor = Rupta::UriExtractProcessor::Default.new)
|
10
|
+
Rupta.new(
|
11
|
+
Rupta::UriPurifier.new,
|
12
|
+
Rupta::UriExtractor.new(uri_extract_processor),
|
13
|
+
Rupta::YamlLoader.new
|
14
|
+
)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems' if RUBY_VERSION < '1.9.0'
|
2
|
+
require 'addressable/template'
|
3
|
+
|
4
|
+
class Rupta
|
5
|
+
class UriExtractor
|
6
|
+
def initialize(uri_extract_processor)
|
7
|
+
@uri_extract_processor = uri_extract_processor
|
8
|
+
end
|
9
|
+
|
10
|
+
def extract(uri_templates, uri)
|
11
|
+
uri_templates.each do |uri_template|
|
12
|
+
uri_parameters = Addressable::Template.new(uri_template).extract(uri, @uri_extract_processor)
|
13
|
+
return [uri_template, uri_parameters] if uri_parameters
|
14
|
+
end
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
begin
|
2
|
+
require 'rupta/factory'
|
3
|
+
rescue LoadError
|
4
|
+
$LOAD_PATH << File.expand_path('../lib', File.dirname(__FILE__))
|
5
|
+
require 'rupta/factory'
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "Rupta#detect_with_hash" do
|
9
|
+
before(:all) do
|
10
|
+
@routes = {'bookmark' => ['http://example.com/bookmarks/{bookmark_id}',
|
11
|
+
'http://example.com/bookmarks/{bookmark_id}.{format}.{lang}',
|
12
|
+
'http://example.com/bookmarks/{bookmark_id}.{format}/',
|
13
|
+
'http://example.com/bookmarks/{bookmark_id}.{format}'],
|
14
|
+
'static_path' => ['http://example.com/static_path'],
|
15
|
+
'search' => ['http://example.com/search']}
|
16
|
+
|
17
|
+
@rupta = Rupta::Factory.new.create
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should detect a correct route" do
|
21
|
+
route_name, uri_template, uri_parameters = @rupta.detect_with_hash(@routes, 'http://example.com/bookmarks/123.atom')
|
22
|
+
route_name.should == 'bookmark'
|
23
|
+
uri_template.should == 'http://example.com/bookmarks/{bookmark_id}.{format}'
|
24
|
+
uri_parameters.values_at('bookmark_id', 'format').should == ['123', 'atom']
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "when passed a static URI" do
|
28
|
+
it "should return an empty hash as the URI parameters" do
|
29
|
+
route_name, uri_template, uri_parameters = @rupta.detect_with_hash(@routes, 'http://example.com/static_path')
|
30
|
+
uri_parameters.should be_empty
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "when passed a URI which has query strings" do
|
35
|
+
it "should omit the query strings" do
|
36
|
+
route_name, uri_template, uri_parameters = @rupta.detect_with_hash(@routes, 'http://example.com/search?q=foo')
|
37
|
+
route_name.should == 'search'
|
38
|
+
uri_template.should == 'http://example.com/search'
|
39
|
+
uri_parameters.should be_empty
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "when passed an invalid URI" do
|
44
|
+
it "should return nil" do
|
45
|
+
result = @rupta.detect_with_hash(@routes, 'http://example.com/not_found')
|
46
|
+
result.should be_nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
begin
|
2
|
+
require 'rupta/factory'
|
3
|
+
rescue LoadError
|
4
|
+
$LOAD_PATH << File.expand_path('../lib', File.dirname(__FILE__))
|
5
|
+
require 'rupta/factory'
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "Rupta#detect" do
|
9
|
+
before(:all) do
|
10
|
+
@yaml_path = File.expand_path('fixtures/routes.yml', File.dirname(__FILE__))
|
11
|
+
@rupta = Rupta::Factory.new.create
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should detect a correct route" do
|
15
|
+
route_name, uri_template, uri_parameters = @rupta.detect(@yaml_path, 'http://example.com/bookmarks/123.atom')
|
16
|
+
route_name.should == 'bookmark'
|
17
|
+
uri_template.should == 'http://example.com/bookmarks/{bookmark_id}.{format}'
|
18
|
+
uri_parameters.values_at('bookmark_id', 'format').should == ['123', 'atom']
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "when passed a static URI" do
|
22
|
+
it "should return an empty hash as the URI parameters" do
|
23
|
+
route_name, uri_template, uri_parameters = @rupta.detect(@yaml_path, 'http://example.com/static_path')
|
24
|
+
uri_parameters.should be_empty
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "when passed a URI which has query strings" do
|
29
|
+
it "should omit the query strings" do
|
30
|
+
route_name, uri_template, uri_parameters = @rupta.detect(@yaml_path, 'http://example.com/search?q=foo')
|
31
|
+
route_name.should == 'search'
|
32
|
+
uri_template.should == 'http://example.com/search'
|
33
|
+
uri_parameters.should be_empty
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "when passed an invalid URI" do
|
38
|
+
it "should return nil" do
|
39
|
+
result = @rupta.detect(@yaml_path, 'http://example.com/not_found')
|
40
|
+
result.should be_nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: iwamot-rupta
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- IWAMOTO Takashi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-21 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: addressable
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.1.0
|
24
|
+
version:
|
25
|
+
description: Rupta is a router library for Web applications. The routes can be defined with URI Template.
|
26
|
+
email: hello@iwamot.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- LICENSE
|
36
|
+
- README.rdoc
|
37
|
+
- lib/rupta.rb
|
38
|
+
- lib/rupta/factory.rb
|
39
|
+
- lib/rupta/uri_extract_processor/default.rb
|
40
|
+
- lib/rupta/uri_extractor.rb
|
41
|
+
- lib/rupta/uri_purifier.rb
|
42
|
+
- lib/rupta/yaml_loader.rb
|
43
|
+
has_rdoc: false
|
44
|
+
homepage: http://github.com/iwamot/rupta
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.2.0
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: A template-based URI router
|
69
|
+
test_files:
|
70
|
+
- spec/routing_with_yaml_spec.rb
|
71
|
+
- spec/routing_with_hash_spec.rb
|