http_router 0.1.3 → 0.1.4

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -140,35 +140,38 @@ class HttpRouter
140
140
  end
141
141
 
142
142
  def find_on_parts(request, parts, params)
143
- if @linear && !@linear.empty?
144
- whole_path = parts.join('/')
145
- next_node = @linear.find do |(tester, node)|
146
- if tester.is_a?(Regexp) and match = tester.match(whole_path) #and match.index == 0 TODO
147
- whole_path.slice!(0,match[0].size)
148
- parts.replace(router.split(whole_path))
149
- node
150
- elsif tester.respond_to?(:matches) and new_params = tester.matches(request.env, parts, whole_path)
151
- params << new_params
152
- node
153
- else
154
- nil
143
+ if !parts.empty?
144
+ if @linear && !@linear.empty?
145
+ whole_path = parts.join('/')
146
+ next_node = @linear.find do |(tester, node)|
147
+ if tester.is_a?(Regexp) and match = tester.match(whole_path) #and match.index == 0 TODO
148
+ whole_path.slice!(0,match[0].size)
149
+ parts.replace(router.split(whole_path))
150
+ node
151
+ elsif tester.respond_to?(:matches) and new_params = tester.matches(request.env, parts, whole_path)
152
+ params << new_params
153
+ node
154
+ else
155
+ nil
156
+ end
157
+ end
158
+ if next_node and match = next_node.last.find_on_parts(request, parts, params)
159
+ return match
155
160
  end
156
161
  end
157
- if next_node and match = next_node.last.find_on_parts(request, parts, params)
158
- return match
162
+ if match = @lookup && @lookup[parts.first]
163
+ parts.shift
164
+ return match.find_on_parts(request, parts, params)
165
+ elsif @catchall
166
+ params << @catchall.variable.matches(request.env, parts, whole_path)
167
+ parts.shift
168
+ return @catchall.find_on_parts(request, parts, params)
169
+ elsif parts.size == 1 && parts.first == '' && (value && value.route.trailing_slash_ignore? || router.ignore_trailing_slash?)
170
+ parts.shift
171
+ return find_on_parts(request, parts, params)
159
172
  end
160
173
  end
161
- if match = @lookup && @lookup[parts.first]
162
- parts.shift
163
- match.find_on_parts(request, parts, params)
164
- elsif @catchall
165
- params << @catchall.variable.matches(request.env, parts, whole_path)
166
- parts.shift
167
- @catchall.find_on_parts(request, parts, params)
168
- elsif parts.size == 1 && parts.first == '' && (value && value.route.trailing_slash_ignore? || router.ignore_trailing_slash?)
169
- parts.shift
170
- find_on_parts(request, parts, params)
171
- elsif request_node
174
+ if request_node
172
175
  request_node.find_on_request_methods(request)
173
176
  elsif arbitrary_node
174
177
  arbitrary_node.find_on_arbitrary(request)
@@ -1,7 +1,7 @@
1
1
  class HttpRouter
2
2
  class Route
3
3
  attr_reader :dest, :paths
4
- attr_accessor :trailing_slash_ignore, :partially_match, :default_values
4
+ attr_accessor :trailing_slash_ignore, :partially_match, :default_values, :router
5
5
 
6
6
  def initialize(base, path)
7
7
  @router = base
@@ -47,7 +47,8 @@ class HttpRouter
47
47
 
48
48
  def name(name)
49
49
  @name = name
50
- router.named_routes[name] = self
50
+ router.named_routes[@name] = self if @name && compiled?
51
+ self
51
52
  end
52
53
 
53
54
  def default(v)
@@ -127,8 +128,9 @@ class HttpRouter
127
128
  !@paths.nil?
128
129
  end
129
130
 
130
- def compile
131
- unless @paths
131
+ def compile(force = false)
132
+ if force || @paths.nil?
133
+ router.named_routes[@name] = self if @name
132
134
  @paths = compile_paths
133
135
  @paths.each_with_index do |p1, i|
134
136
  @paths[i+1, @paths.size].each do |p2|
data/lib/http_router.rb CHANGED
@@ -26,11 +26,13 @@ class HttpRouter
26
26
  end
27
27
 
28
28
  def initialize(options = nil, &block)
29
+ @options = options
29
30
  @default_app = options && options[:default_app] || proc{|env| ::Rack::Response.new("Not Found", 404).finish }
30
31
  @ignore_trailing_slash = options && options.key?(:ignore_trailing_slash) ? options[:ignore_trailing_slash] : true
31
32
  @redirect_trailing_slash = options && options.key?(:redirect_trailing_slash) ? options[:redirect_trailing_slash] : false
32
33
  @routes = []
33
34
  @named_routes = {}
35
+ @init_block = block
34
36
  reset!
35
37
  instance_eval(&block) if block
36
38
  end
@@ -145,6 +147,17 @@ class HttpRouter
145
147
  Glob.new(self, *args)
146
148
  end
147
149
 
150
+ def dup
151
+ dup_router = HttpRouter.new(@options, &@init_block)
152
+ @routes.each do |r|
153
+ new_route = r.dup
154
+ new_route.router = dup_router
155
+ dup_router.routes << new_route
156
+ new_route.compile(true)
157
+ end
158
+ dup_router
159
+ end
160
+
148
161
  private
149
162
 
150
163
  def consume_path!(request, response)
data/spec/misc_spec.rb CHANGED
@@ -15,9 +15,11 @@ describe "HttpRouter" do
15
15
  end
16
16
 
17
17
  context "instance_eval block" do
18
- #HttpRouter.new {
19
- # add('/test').to :test
20
- #}.recognize(Rack::MockRequest.env_for('/test', :method => 'GET')).dest.should == :test
18
+ it "run the routes" do
19
+ HttpRouter.new {
20
+ add('/test').to :test
21
+ }.recognize(Rack::MockRequest.env_for('/test', :method => 'GET')).dest.should == :test
22
+ end
21
23
  end
22
24
 
23
25
  context "exceptions" do
@@ -34,4 +36,22 @@ describe "HttpRouter" do
34
36
  end
35
37
 
36
38
  end
39
+
40
+ context "dupping" do
41
+ it "run the routes" do
42
+ pending
43
+ r1 = HttpRouter.new {
44
+ add('/test').name(:test_route).to :test
45
+ }
46
+ r2 = r1.dup
47
+ r2.add('/test2').to(:test2)
48
+ r1.recognize(Rack::MockRequest.env_for('/test2')).should be_nil
49
+ r2.recognize(Rack::MockRequest.env_for('/test2')).should_not be_nil
50
+
51
+ r1.named_routes[:test_route].should == r1.routes.first
52
+ r2.named_routes[:test_route].should == r2.routes.first
53
+ end
54
+ end
55
+
56
+
37
57
  end
@@ -89,6 +89,15 @@ describe "HttpRouter#recognize" do
89
89
 
90
90
  end
91
91
 
92
+ context "variables" do
93
+ it "should recognize a simple variable" do
94
+ @router.add("/foo").to(:test1)
95
+ @router.add("/foo/:id").to(:test2)
96
+ @router.recognize(Rack::MockRequest.env_for('/foo')).dest.should == :test1
97
+ @router.recognize(Rack::MockRequest.env_for('/foo/id')).dest.should == :test2
98
+ end
99
+ end
100
+
92
101
  context "request methods" do
93
102
  it "should pick a specific request_method" do
94
103
  route = @router.post("/test").to(:test)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_router
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull