iwamot-rupta 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ Rupta 0.9.2 (2009-09-20)
2
+
3
+ * restructured whole API
4
+
1
5
  Rupta 0.9.1 (2009-06-25)
2
6
 
3
7
  * changed the factory method to static
data/README.rdoc CHANGED
@@ -1,8 +1,8 @@
1
1
  = README
2
2
 
3
- == Descrption
3
+ == Description
4
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.
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
6
 
7
7
  == Installation
8
8
 
@@ -10,56 +10,61 @@ Rupta is a router library for Web applications. The routes can be defined with {
10
10
 
11
11
  == Examples
12
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
13
+ === examples/routing.rb
22
14
 
23
- === examples/routing_with_yaml.rb
24
-
25
- yaml_path = File.expand_path('fixtures/routes.yml', File.dirname(__FILE__))
15
+ routes = {'bookmark' => ['http://example.com/bookmarks/{bookmark_id}',
16
+ 'http://example.com/bookmarks/{bookmark_id}.{format}'],
17
+ 'static_path' => ['http://example.com/static_path'],
18
+ 'search' => ['http://example.com/search']}
26
19
 
27
20
  require 'rupta/factory'
28
- rupta = Rupta::Factory.create
21
+ router = Rupta::Factory.new.create
29
22
 
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"}]
23
+ # Rupta#detect returns the name of a detected route,
24
+ # a matched URI template and extracted URI parameters
25
+ p router.detect(route, 'http://example.com/bookmarks/123.atom')
26
+ # => ["bookmark",
27
+ # "http://example.com/bookmarks/{bookmark_id}.{format}",
28
+ # {"bookmark_id"=>"123", "format"=>"atom"}]
33
29
 
34
30
  # if the uri is static, the extracted parameters is empty
35
- p rupta.detect(yaml_path, 'http://example.com/static_path')
31
+ p router.detect(routes, 'http://example.com/static_path')
36
32
  # => ["static_path", "http://example.com/static_path", {}]
37
33
 
38
34
  # query strings are omitted
39
- p rupta.detect(yaml_path, 'http://example.com/search?q=foo')
35
+ p router.detect(routes, 'http://example.com/search?q=foo')
40
36
  # => ["search", "http://example.com/search", {}]
41
37
 
42
38
  # when no route is found, Rupta#detect returns nil
43
- p rupta.detect(yaml_path, 'http://example.com/not_found')
39
+ p router.detect(routes, 'http://example.com/not_found')
44
40
  # => nil
45
41
 
46
- === examples/routing_with_hash.rb
42
+ === examples/fixtures/routes.yml
47
43
 
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']}
44
+ bookmark:
45
+ - http://example.com/bookmarks/{bookmark_id}
46
+ - http://example.com/bookmarks/{bookmark_id}.{format}
47
+ static_path:
48
+ - http://example.com/static_path
49
+ search:
50
+ - http://example.com/search
52
51
 
53
- require 'rupta/factory'
54
- rupta = Rupta::Factory.create
52
+ === examples/routing_with_yaml_file.rb
55
53
 
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"}]
54
+ yaml_path = File.expand_path('fixtures/routes.yml', File.dirname(__FILE__))
55
+
56
+ # use Rupta::Yaml::Factory, if you define routes in a YAML file
57
+ require 'rupta/yaml/factory'
58
+ rupta = Rupta::Yaml::Factory.new.create
59
+
60
+ p rupta.detect(yaml_path, 'http://example.com/bookmarks/123.atom')
61
+ # => ["bookmark",
62
+ # "http://example.com/bookmarks/{bookmark_id}.{format}",
63
+ # {"bookmark_id"=>"123", "format"=>"atom"}]
59
64
 
60
65
  == Requirement
61
66
 
62
- Rupta-0.9.1 was tested with Ruby 1.8.7 and 1.9.1.
67
+ Rupta-0.9.2 was tested with Ruby 1.9.1.
63
68
 
64
69
  == Dependencies
65
70
 
data/lib/rupta.rb CHANGED
@@ -1,11 +1,10 @@
1
1
  class Rupta
2
- def initialize(uri_purifier, uri_extractor, yaml_loader)
2
+ def initialize(uri_purifier, uri_extractor)
3
3
  @uri_purifier = uri_purifier
4
4
  @uri_extractor = uri_extractor
5
- @yaml_loader = yaml_loader
6
5
  end
7
6
 
8
- def detect_with_hash(routes, uri, omittable_uri_components = [:query])
7
+ def detect(routes, uri, omittable_uri_components = [:query])
9
8
  pure_uri = @uri_purifier.purify(uri, omittable_uri_components)
10
9
  routes.each do |route_name, uri_templates|
11
10
  uri_template, uri_parameters = @uri_extractor.extract(uri_templates, pure_uri)
@@ -13,10 +12,4 @@ class Rupta
13
12
  end
14
13
  nil
15
14
  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
15
  end
data/lib/rupta/factory.rb CHANGED
@@ -1,16 +1,19 @@
1
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'
2
+ require 'rupta/helper/uri_extract_processor/default'
3
+ require 'rupta/helper/uri_extractor'
4
+ require 'rupta/helper/uri_parser'
5
+ require 'rupta/helper/uri_purifier'
6
6
 
7
7
  class Rupta
8
8
  class Factory
9
- def self.create(uri_extract_processor = Rupta::UriExtractProcessor::Default.new)
9
+ def initialize(uri_extract_processor = Rupta::Helper::UriExtractProcessor::Default.new)
10
+ @uri_extract_processor = uri_extract_processor
11
+ end
12
+
13
+ def create
10
14
  Rupta.new(
11
- Rupta::UriPurifier.new,
12
- Rupta::UriExtractor.new(uri_extract_processor),
13
- Rupta::YamlLoader.new
15
+ Rupta::Helper::UriPurifier.new(Rupta::Helper::UriParser.new),
16
+ Rupta::Helper::UriExtractor.new(@uri_extract_processor)
14
17
  )
15
18
  end
16
19
  end
@@ -0,0 +1,11 @@
1
+ class Rupta
2
+ module Helper
3
+ module UriExtractProcessor
4
+ class Default
5
+ def match(name)
6
+ '[^/.]*'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ require 'rubygems' if RUBY_VERSION < '1.9.0'
2
+ require 'addressable/template'
3
+
4
+ class Rupta
5
+ module Helper
6
+ class UriExtractor
7
+ def initialize(uri_extract_processor)
8
+ @uri_extract_processor = uri_extract_processor
9
+ end
10
+
11
+ def extract(uri_templates, uri)
12
+ uri_templates.each do |uri_template|
13
+ uri_parameters = Addressable::Template.new(uri_template).extract(uri, @uri_extract_processor)
14
+ return [uri_template, uri_parameters] if uri_parameters
15
+ end
16
+ nil
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems' if RUBY_VERSION < '1.9.0'
2
+ require 'addressable/uri'
3
+
4
+ class Rupta
5
+ module Helper
6
+ class UriParser
7
+ def parse(uri)
8
+ Addressable::URI.parse(uri)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ class Rupta
2
+ module Helper
3
+ class UriPurifier
4
+ def initialize(uri_parser)
5
+ @uri_parser = uri_parser
6
+ end
7
+
8
+ def purify(uri, omittable_uri_components)
9
+ @uri_parser.parse(uri).omit(*omittable_uri_components)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require 'yaml'
2
+
3
+ class Rupta
4
+ module Helper
5
+ class YamlLoader
6
+ def load_file(path)
7
+ YAML.load_file(path)
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/rupta/yaml.rb ADDED
@@ -0,0 +1,13 @@
1
+ class Rupta
2
+ class Yaml
3
+ def initialize(router, yaml_loader)
4
+ @router = router
5
+ @yaml_loader = yaml_loader
6
+ end
7
+
8
+ def detect(path, uri, omittable_uri_components = [:query])
9
+ routes = @yaml_loader.load_file(path)
10
+ @router.detect(routes, uri, omittable_uri_components)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ require 'rupta/factory'
2
+ require 'rupta/helper/uri_extract_processor/default'
3
+ require 'rupta/helper/yaml_loader'
4
+ require 'rupta/yaml'
5
+
6
+ class Rupta
7
+ class Yaml
8
+ class Factory
9
+ def initialize(uri_extract_processor = Rupta::Helper::UriExtractProcessor::Default.new)
10
+ @uri_extract_processor = uri_extract_processor
11
+ end
12
+
13
+ def create
14
+ Rupta::Yaml.new(
15
+ Rupta::Factory.new(@uri_extract_processor).create,
16
+ Rupta::Helper::YamlLoader.new
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -5,7 +5,7 @@ rescue LoadError
5
5
  require 'rupta/factory'
6
6
  end
7
7
 
8
- describe "Rupta#detect_with_hash" do
8
+ describe "Rupta#detect" do
9
9
  before(:all) do
10
10
  @routes = {'bookmark' => ['http://example.com/bookmarks/{bookmark_id}',
11
11
  'http://example.com/bookmarks/{bookmark_id}.{format}.{lang}',
@@ -14,11 +14,11 @@ describe "Rupta#detect_with_hash" do
14
14
  'static_path' => ['http://example.com/static_path'],
15
15
  'search' => ['http://example.com/search']}
16
16
 
17
- @rupta = Rupta::Factory.create
17
+ @router = Rupta::Factory.new.create
18
18
  end
19
19
 
20
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')
21
+ route_name, uri_template, uri_parameters = @router.detect(@routes, 'http://example.com/bookmarks/123.atom')
22
22
  route_name.should == 'bookmark'
23
23
  uri_template.should == 'http://example.com/bookmarks/{bookmark_id}.{format}'
24
24
  uri_parameters.values_at('bookmark_id', 'format').should == ['123', 'atom']
@@ -26,14 +26,14 @@ describe "Rupta#detect_with_hash" do
26
26
 
27
27
  describe "when passed a static URI" do
28
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')
29
+ route_name, uri_template, uri_parameters = @router.detect(@routes, 'http://example.com/static_path')
30
30
  uri_parameters.should be_empty
31
31
  end
32
32
  end
33
33
 
34
34
  describe "when passed a URI which has query strings" do
35
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')
36
+ route_name, uri_template, uri_parameters = @router.detect(@routes, 'http://example.com/search?q=foo')
37
37
  route_name.should == 'search'
38
38
  uri_template.should == 'http://example.com/search'
39
39
  uri_parameters.should be_empty
@@ -42,7 +42,7 @@ describe "Rupta#detect_with_hash" do
42
42
 
43
43
  describe "when passed an invalid URI" do
44
44
  it "should return nil" do
45
- result = @rupta.detect_with_hash(@routes, 'http://example.com/not_found')
45
+ result = @router.detect(@routes, 'http://example.com/not_found')
46
46
  result.should be_nil
47
47
  end
48
48
  end
@@ -1,18 +1,18 @@
1
1
  begin
2
- require 'rupta/factory'
2
+ require 'rupta/yaml/factory'
3
3
  rescue LoadError
4
4
  $LOAD_PATH << File.expand_path('../lib', File.dirname(__FILE__))
5
- require 'rupta/factory'
5
+ require 'rupta/yaml/factory'
6
6
  end
7
7
 
8
- describe "Rupta#detect" do
8
+ describe "Rupta::Yaml#detect" do
9
9
  before(:all) do
10
10
  @yaml_path = File.expand_path('fixtures/routes.yml', File.dirname(__FILE__))
11
- @rupta = Rupta::Factory.create
11
+ @router = Rupta::Yaml::Factory.new.create
12
12
  end
13
13
 
14
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')
15
+ route_name, uri_template, uri_parameters = @router.detect(@yaml_path, 'http://example.com/bookmarks/123.atom')
16
16
  route_name.should == 'bookmark'
17
17
  uri_template.should == 'http://example.com/bookmarks/{bookmark_id}.{format}'
18
18
  uri_parameters.values_at('bookmark_id', 'format').should == ['123', 'atom']
@@ -20,14 +20,14 @@ describe "Rupta#detect" do
20
20
 
21
21
  describe "when passed a static URI" do
22
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')
23
+ route_name, uri_template, uri_parameters = @router.detect(@yaml_path, 'http://example.com/static_path')
24
24
  uri_parameters.should be_empty
25
25
  end
26
26
  end
27
27
 
28
28
  describe "when passed a URI which has query strings" do
29
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')
30
+ route_name, uri_template, uri_parameters = @router.detect(@yaml_path, 'http://example.com/search?q=foo')
31
31
  route_name.should == 'search'
32
32
  uri_template.should == 'http://example.com/search'
33
33
  uri_parameters.should be_empty
@@ -36,7 +36,7 @@ describe "Rupta#detect" do
36
36
 
37
37
  describe "when passed an invalid URI" do
38
38
  it "should return nil" do
39
- result = @rupta.detect(@yaml_path, 'http://example.com/not_found')
39
+ result = @router.detect(@yaml_path, 'http://example.com/not_found')
40
40
  result.should be_nil
41
41
  end
42
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iwamot-rupta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - IWAMOTO Takashi
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-25 00:00:00 -07:00
12
+ date: 2009-09-20 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -37,12 +37,16 @@ files:
37
37
  - README.rdoc
38
38
  - lib/rupta.rb
39
39
  - lib/rupta/factory.rb
40
- - lib/rupta/uri_extract_processor/default.rb
41
- - lib/rupta/uri_extractor.rb
42
- - lib/rupta/uri_purifier.rb
43
- - lib/rupta/yaml_loader.rb
40
+ - lib/rupta/helper/uri_extract_processor/default.rb
41
+ - lib/rupta/helper/uri_extractor.rb
42
+ - lib/rupta/helper/uri_parser.rb
43
+ - lib/rupta/helper/uri_purifier.rb
44
+ - lib/rupta/helper/yaml_loader.rb
45
+ - lib/rupta/yaml.rb
46
+ - lib/rupta/yaml/factory.rb
44
47
  has_rdoc: false
45
48
  homepage: http://github.com/iwamot/rupta
49
+ licenses:
46
50
  post_install_message:
47
51
  rdoc_options:
48
52
  - --charset=UTF-8
@@ -63,10 +67,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
67
  requirements: []
64
68
 
65
69
  rubyforge_project:
66
- rubygems_version: 1.2.0
70
+ rubygems_version: 1.3.5
67
71
  signing_key:
68
72
  specification_version: 3
69
73
  summary: A template-based URI router
70
74
  test_files:
71
- - spec/routing_with_yaml_spec.rb
72
- - spec/routing_with_hash_spec.rb
75
+ - spec/routing_spec.rb
76
+ - spec/routing_with_yaml_file_spec.rb
@@ -1,9 +0,0 @@
1
- class Rupta
2
- module UriExtractProcessor
3
- class Default
4
- def match(name)
5
- '[^/.]*'
6
- end
7
- end
8
- end
9
- end
@@ -1,18 +0,0 @@
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
@@ -1,10 +0,0 @@
1
- require 'rubygems' if RUBY_VERSION < '1.9.0'
2
- require 'addressable/uri'
3
-
4
- class Rupta
5
- class UriPurifier
6
- def purify(uri, omittable_uri_components)
7
- Addressable::URI.parse(uri).omit(*omittable_uri_components)
8
- end
9
- end
10
- end
@@ -1,9 +0,0 @@
1
- require 'yaml'
2
-
3
- class Rupta
4
- class YamlLoader
5
- def load_file(path)
6
- YAML.load_file(path)
7
- end
8
- end
9
- end