named-routes 0.1.2 → 0.2.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/Gemfile +7 -0
- data/Gemfile.lock +30 -0
- data/README.md +2 -2
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/named-routes/routes.rb +33 -14
- data/lib/named-routes/{uri.rb → schemed_uri.rb} +1 -1
- data/lib/named-routes.rb +10 -5
- data/spec/named-routes/routes_spec.rb +45 -35
- data/spec/spec_helper.rb +4 -1
- metadata +86 -12
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
extlib (0.9.15)
|
6
|
+
git (1.2.5)
|
7
|
+
jeweler (1.5.2)
|
8
|
+
bundler (~> 1.0.0)
|
9
|
+
git (>= 1.2.5)
|
10
|
+
rake
|
11
|
+
rake (0.8.7)
|
12
|
+
rr (1.0.2)
|
13
|
+
rspec (2.3.0)
|
14
|
+
rspec-core (~> 2.3.0)
|
15
|
+
rspec-expectations (~> 2.3.0)
|
16
|
+
rspec-mocks (~> 2.3.0)
|
17
|
+
rspec-core (2.3.1)
|
18
|
+
rspec-expectations (2.3.0)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
rspec-mocks (2.3.0)
|
21
|
+
|
22
|
+
PLATFORMS
|
23
|
+
ruby
|
24
|
+
|
25
|
+
DEPENDENCIES
|
26
|
+
diff-lcs (= 1.1.2)
|
27
|
+
extlib (= 0.9.15)
|
28
|
+
jeweler (= 1.5.2)
|
29
|
+
rr (= 1.0.2)
|
30
|
+
rspec (= 2.3.0)
|
data/README.md
CHANGED
@@ -58,7 +58,7 @@ You can access the routes by doing the following.
|
|
58
58
|
path(:user, "/users/:user_id")
|
59
59
|
paths.user(:user_id => 42) # => "/users/42"
|
60
60
|
paths.http.user(:user_id => 42) # => "http://example.com/users/42"
|
61
|
-
paths.https.user(:user_id => 42) # => "
|
61
|
+
paths.https.user(:user_id => 42) # => "https://example.com/users/42"
|
62
62
|
|
63
63
|
It also works with prefixes:
|
64
64
|
|
@@ -68,7 +68,7 @@ It also works with prefixes:
|
|
68
68
|
path(:user, "/users/:user_id")
|
69
69
|
paths.user(:user_id => 42) # => "/users/42"
|
70
70
|
paths.http.user(:user_id => 42) # => "http://example.com/admin/users/42"
|
71
|
-
paths.https.user(:user_id => 42) # => "
|
71
|
+
paths.https.user(:user_id => 42) # => "https://example.com/admin/users/42"
|
72
72
|
|
73
73
|
And with query params:
|
74
74
|
|
data/Rakefile
CHANGED
@@ -22,7 +22,7 @@ begin
|
|
22
22
|
s.has_rdoc = true
|
23
23
|
s.extra_rdoc_files = [ "README.md", "CHANGES" ]
|
24
24
|
s.rdoc_options = ["--main", "README.md", "--inline-source", "--line-numbers"]
|
25
|
-
s.add_dependency "
|
25
|
+
s.add_dependency "extlib", ">= 0.9.15"
|
26
26
|
end
|
27
27
|
rescue LoadError
|
28
28
|
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1
|
1
|
+
0.2.1
|
data/lib/named-routes/routes.rb
CHANGED
@@ -1,32 +1,46 @@
|
|
1
1
|
module NamedRoutes
|
2
2
|
class Routes
|
3
3
|
class_inheritable_accessor :host, :prefix
|
4
|
-
|
4
|
+
|
5
5
|
extend(Module.new do
|
6
6
|
def instance
|
7
7
|
@instance ||= new
|
8
8
|
end
|
9
9
|
|
10
10
|
def http
|
11
|
-
|
11
|
+
SchemedUri.new(self, "http")
|
12
12
|
end
|
13
13
|
|
14
14
|
def https
|
15
|
-
|
15
|
+
SchemedUri.new(self, "https")
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
18
|
+
def route(name, definition, include_prefix=true)
|
19
19
|
full_definition = (include_prefix && prefix) ? File.join("", prefix, definition) : definition
|
20
|
+
_defined_routes[name.to_s] = full_definition
|
20
21
|
define_method name do |*args|
|
21
22
|
self.class.eval(full_definition, [args.first].compact.first || {})
|
22
23
|
end
|
23
24
|
yield full_definition if block_given?
|
24
25
|
full_definition
|
25
26
|
end
|
27
|
+
alias_method :path, :route
|
28
|
+
alias_method :uri, :route
|
29
|
+
|
30
|
+
def defined_routes
|
31
|
+
(ancestors.reverse + [self]).inject({}) do |memo, klass|
|
32
|
+
memo.merge!(klass._defined_routes) if klass.respond_to?(:_defined_routes)
|
33
|
+
memo
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def _defined_routes
|
38
|
+
@_defined_routes ||= {}
|
39
|
+
end
|
26
40
|
|
27
41
|
def eval(definition, params_arg={})
|
28
42
|
params = Mash.new(params_arg)
|
29
|
-
|
43
|
+
uri_string = if params.empty?
|
30
44
|
definition
|
31
45
|
else
|
32
46
|
definition.split("/").map do |segment|
|
@@ -42,28 +56,29 @@ module NamedRoutes
|
|
42
56
|
end.join("/")
|
43
57
|
end
|
44
58
|
unless params.empty?
|
45
|
-
|
59
|
+
uri_string << "?#{escape_params(params).to_params.gsub("%20", "+")}"
|
46
60
|
end
|
47
|
-
|
61
|
+
uri_string
|
48
62
|
end
|
49
63
|
|
50
64
|
# TODO: Create eval_without_prefix
|
51
65
|
|
52
66
|
def escape_params(params={})
|
53
67
|
params.inject({}) do |memo, kv|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
68
|
+
key, value = kv
|
69
|
+
memo[URI.escape(key)] = if value.is_a?(Hash)
|
70
|
+
escape_params(value)
|
71
|
+
elsif value.is_a?(Array)
|
72
|
+
value.map { |v| URI.escape(v.to_s) }
|
58
73
|
else
|
59
|
-
URI.escape(
|
74
|
+
URI.escape(value.to_s)
|
60
75
|
end
|
61
76
|
memo
|
62
77
|
end
|
63
78
|
end
|
64
79
|
|
65
|
-
def normalize(
|
66
|
-
|
80
|
+
def normalize(uri)
|
81
|
+
uri.gsub(Regexp.new("^#{File.join("", prefix.to_s)}"), "/").gsub("//", "/")
|
67
82
|
end
|
68
83
|
|
69
84
|
def method_missing(method_name, *args, &block)
|
@@ -74,5 +89,9 @@ module NamedRoutes
|
|
74
89
|
end
|
75
90
|
end
|
76
91
|
end)
|
92
|
+
|
93
|
+
def as_json(*args)
|
94
|
+
self.class.defined_routes
|
95
|
+
end
|
77
96
|
end
|
78
97
|
end
|
data/lib/named-routes.rb
CHANGED
@@ -2,18 +2,23 @@ require "extlib"
|
|
2
2
|
require "uri"
|
3
3
|
|
4
4
|
module NamedRoutes
|
5
|
-
def
|
6
|
-
|
5
|
+
def named_route(*args, &block)
|
6
|
+
named_routes.uri(*args, &block)
|
7
7
|
end
|
8
|
+
alias_method :path, :named_route
|
9
|
+
alias_method :route, :named_route
|
10
|
+
alias_method :uri, :named_route
|
8
11
|
|
9
|
-
def
|
12
|
+
def named_routes
|
10
13
|
::NamedRoutes::Routes
|
11
14
|
end
|
12
|
-
alias_method :
|
15
|
+
alias_method :paths, :named_routes
|
16
|
+
alias_method :routes, :named_routes
|
17
|
+
alias_method :uris, :named_routes
|
13
18
|
|
14
19
|
extend self
|
15
20
|
end
|
16
21
|
|
17
22
|
dir = File.dirname(__FILE__)
|
18
23
|
require "#{dir}/named-routes/routes"
|
19
|
-
require "#{dir}/named-routes/
|
24
|
+
require "#{dir}/named-routes/schemed_uri"
|
@@ -8,17 +8,17 @@ module NamedRoutes
|
|
8
8
|
|
9
9
|
def routes
|
10
10
|
@routes ||= begin
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
routes_class = Class.new(NamedRoutes::Routes)
|
12
|
+
routes_class.route(:root, "/")
|
13
|
+
routes_class.route(:current_user_category_top_choices, "/current-user/:category/top-choices")
|
14
|
+
routes_class.route(:decision_stream, "/decision-streams/:stream_id")
|
15
|
+
routes_class
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
describe "
|
19
|
+
describe "uri definition" do
|
20
20
|
context "when params hash is not given" do
|
21
|
-
it "returns the definition"
|
21
|
+
it "returns the definition" do
|
22
22
|
routes.root.should == "/"
|
23
23
|
routes.current_user_category_top_choices.should == "/current-user/:category/top-choices"
|
24
24
|
routes.decision_stream.should == "/decision-streams/:stream_id"
|
@@ -26,10 +26,10 @@ module NamedRoutes
|
|
26
26
|
end
|
27
27
|
|
28
28
|
context "when params hash is given" do
|
29
|
-
it "returns the
|
30
|
-
|
29
|
+
it "returns the uri with the param replaced with the given param value with additional params added as url params" do
|
30
|
+
schemed_uri_1 = routes.current_user_category_top_choices(:category => "cars", :foo => "bar", :baz => {"one" => "two three"})
|
31
31
|
|
32
|
-
path, query =
|
32
|
+
path, query = schemed_uri_1.split("?")
|
33
33
|
path.should == "/current-user/cars/top-choices"
|
34
34
|
query.should include("foo=bar")
|
35
35
|
query.should include("baz[one]=two+three")
|
@@ -40,17 +40,17 @@ module NamedRoutes
|
|
40
40
|
context "when a prefix is given" do
|
41
41
|
def routes
|
42
42
|
@routes ||= begin
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
43
|
+
routes_class = Class.new(NamedRoutes::Routes)
|
44
|
+
routes_class.prefix = "general"
|
45
|
+
routes_class.route(:root, "/")
|
46
|
+
routes_class.route(:current_user_category_top_choices, "/current-user/:category/top-choices")
|
47
|
+
routes_class.route(:decision_stream, "/decision-streams/:stream_id")
|
48
|
+
routes_class
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
52
|
context "when default and include_prefix argument is true" do
|
53
|
-
it "appends the prefix to the returned
|
53
|
+
it "appends the prefix to the returned uris" do
|
54
54
|
routes.root.should == "/general/"
|
55
55
|
routes.current_user_category_top_choices.should == "/general/current-user/:category/top-choices"
|
56
56
|
routes.decision_stream.should == "/general/decision-streams/:stream_id"
|
@@ -59,9 +59,9 @@ module NamedRoutes
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
context "when include_prefix argument is false in the
|
63
|
-
it "does not append the prefix to the returned
|
64
|
-
routes.
|
62
|
+
context "when include_prefix argument is false in the uri definition" do
|
63
|
+
it "does not append the prefix to the returned uris" do
|
64
|
+
routes.uri(:raw_path, "/raw/path", false).should == "/raw/path"
|
65
65
|
routes.raw_path.should == "/raw/path"
|
66
66
|
end
|
67
67
|
end
|
@@ -76,10 +76,10 @@ module NamedRoutes
|
|
76
76
|
end
|
77
77
|
|
78
78
|
context "when params hash is given" do
|
79
|
-
it "returns the
|
80
|
-
|
79
|
+
it "returns the uri with the param replaced with the given param value with additional params added as url params" do
|
80
|
+
schemed_uri_1 = routes.current_user_category_top_choices(:category => "cars", :foo => "bar", :baz => {"one" => "two three"})
|
81
81
|
|
82
|
-
path, query =
|
82
|
+
path, query = schemed_uri_1.split("?")
|
83
83
|
path.should == "/current-user/cars/top-choices"
|
84
84
|
query.should include("foo=bar")
|
85
85
|
query.should include("baz[one]=two+three")
|
@@ -88,11 +88,23 @@ module NamedRoutes
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
+
describe ".http" do
|
92
|
+
it "returns a full http schemed_uri (with ::NamedRoutes.host) for the given named route" do
|
93
|
+
routes.http.decision_stream(:stream_id => "11").should == "http://example.com/decision-streams/11"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe ".https" do
|
98
|
+
it "returns a full https schemed_uri (with ::NamedRoutes.host) for the given named route" do
|
99
|
+
routes.https.decision_stream(:stream_id => "11").should == "https://example.com/decision-streams/11"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
91
103
|
describe "#normalize" do
|
92
104
|
def routes
|
93
105
|
@routes ||= begin
|
94
|
-
|
95
|
-
|
106
|
+
route_class = Class.new(NamedRoutes::Routes)
|
107
|
+
route_class
|
96
108
|
end
|
97
109
|
end
|
98
110
|
|
@@ -101,7 +113,7 @@ module NamedRoutes
|
|
101
113
|
routes.prefix.should == nil
|
102
114
|
end
|
103
115
|
|
104
|
-
it "returns the given
|
116
|
+
it "returns the given uri" do
|
105
117
|
routes.normalize("/prefix/foo/bar").should == "/prefix/foo/bar"
|
106
118
|
end
|
107
119
|
end
|
@@ -131,15 +143,13 @@ module NamedRoutes
|
|
131
143
|
end
|
132
144
|
end
|
133
145
|
|
134
|
-
describe "
|
135
|
-
it "returns a
|
136
|
-
routes.
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
it "returns a full https uri (with ::NamedRoutes.host) for the given named route" do
|
142
|
-
routes.https.decision_stream(:stream_id => "11").should == "https://example.com/decision-streams/11"
|
146
|
+
describe "#as_json" do
|
147
|
+
it "returns a hash of all of the route methods as keys and the definions as values" do
|
148
|
+
routes.as_json.should == {
|
149
|
+
"root" => "/",
|
150
|
+
"current_user_category_top_choices" => "/current-user/:category/top-choices",
|
151
|
+
"decision_stream" => "/decision-streams/:stream_id"
|
152
|
+
}
|
143
153
|
end
|
144
154
|
end
|
145
155
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require "rubygems"
|
2
|
+
require "bundler"
|
3
|
+
Bundler.setup
|
2
4
|
|
3
5
|
$LOAD_PATH.unshift(File.expand_path("#{File.dirname(__FILE__)}/../lib"))
|
4
6
|
require "named-routes"
|
@@ -13,5 +15,6 @@ require 'rr'
|
|
13
15
|
|
14
16
|
RSpec.configure do |c|
|
15
17
|
c.mock_with :rr
|
16
|
-
|
18
|
+
c.filter_run :focus => true
|
19
|
+
c.run_all_when_everything_filtered = true
|
17
20
|
end
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: named-routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 31
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
|
-
- 1
|
9
7
|
- 2
|
10
|
-
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Brian Takita
|
@@ -15,24 +14,99 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-
|
17
|
+
date: 2010-12-28 00:00:00 -08:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
21
|
+
name: extlib
|
23
22
|
prerelease: false
|
24
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
24
|
none: false
|
26
25
|
requirements:
|
27
|
-
- - "
|
26
|
+
- - "="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 9
|
31
|
+
- 15
|
32
|
+
version: 0.9.15
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: diff-lcs
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - "="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 1
|
46
|
+
- 2
|
47
|
+
version: 1.1.2
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: jeweler
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - "="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 1
|
60
|
+
- 5
|
61
|
+
- 2
|
62
|
+
version: 1.5.2
|
63
|
+
type: :runtime
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: rr
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - "="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 1
|
75
|
+
- 0
|
76
|
+
- 2
|
77
|
+
version: 1.0.2
|
78
|
+
type: :runtime
|
79
|
+
version_requirements: *id004
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: rspec
|
82
|
+
prerelease: false
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - "="
|
28
87
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 5
|
30
88
|
segments:
|
31
89
|
- 2
|
32
90
|
- 3
|
33
|
-
|
91
|
+
- 0
|
92
|
+
version: 2.3.0
|
34
93
|
type: :runtime
|
35
|
-
version_requirements: *
|
94
|
+
version_requirements: *id005
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: extlib
|
97
|
+
prerelease: false
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
- 9
|
106
|
+
- 15
|
107
|
+
version: 0.9.15
|
108
|
+
type: :runtime
|
109
|
+
version_requirements: *id006
|
36
110
|
description:
|
37
111
|
email: brian.takita@gmail.com
|
38
112
|
executables: []
|
@@ -44,13 +118,15 @@ extra_rdoc_files:
|
|
44
118
|
- README.md
|
45
119
|
files:
|
46
120
|
- CHANGES
|
121
|
+
- Gemfile
|
122
|
+
- Gemfile.lock
|
47
123
|
- MIT.LICENSE
|
48
124
|
- README.md
|
49
125
|
- Rakefile
|
50
126
|
- VERSION
|
51
127
|
- lib/named-routes.rb
|
52
128
|
- lib/named-routes/routes.rb
|
53
|
-
- lib/named-routes/
|
129
|
+
- lib/named-routes/schemed_uri.rb
|
54
130
|
- spec/named-routes/routes_spec.rb
|
55
131
|
- spec/spec_helper.rb
|
56
132
|
has_rdoc: true
|
@@ -70,7 +146,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
146
|
requirements:
|
71
147
|
- - ">="
|
72
148
|
- !ruby/object:Gem::Version
|
73
|
-
hash: 3
|
74
149
|
segments:
|
75
150
|
- 0
|
76
151
|
version: "0"
|
@@ -79,7 +154,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
154
|
requirements:
|
80
155
|
- - ">="
|
81
156
|
- !ruby/object:Gem::Version
|
82
|
-
hash: 3
|
83
157
|
segments:
|
84
158
|
- 0
|
85
159
|
version: "0"
|