http_router 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -39,6 +39,8 @@ Maps a route. The format for variables in paths is:
39
39
 
40
40
  Everything else is treated literally. Optional parts are surrounded by brackets. Partially matching paths have a trailing <tt>*</tt>. Optional trailing slash matching is done with <tt>/?</tt>.
41
41
 
42
+ As well, you can escape the following characters with a backslash: <tt>( ) : *</tt>
43
+
42
44
  Once you have a route object, use <tt>HttpRouter::Route#to</tt> to add a destination and <tt>HttpRouter::Route#name</tt> to name it.
43
45
 
44
46
  e.g.
@@ -103,17 +103,16 @@ class HttpRouter
103
103
  end
104
104
  case RequestNode::RequestMethods.index(method) <=> RequestNode::RequestMethods.index(current_node.request_method)
105
105
  when 0 #use this node
106
- if request_options[method].is_a?(Regexp)
107
- new_node = router.request_node
108
- current_nodes[current_node_index] = new_node
109
- current_node.create_linear
110
- current_node.linear << [request_options[method], new_node]
111
- elsif request_options[method].is_a?(Array)
112
- current_node.create_lookup
113
- current_nodes[current_node_index] = request_options[method].map{|val| current_node.lookup[val] ||= router.request_node}
114
- else
115
- current_node.create_lookup
116
- current_nodes[current_node_index] = (current_node.lookup[request_options[method]] ||= router.request_node)
106
+ Array(request_options[method]).each_with_index do |request_value, index|
107
+ if request_value.is_a?(Regexp)
108
+ new_node = router.request_node
109
+ current_nodes[index == 0 ? current_node_index : current_nodes.length] = new_node
110
+ current_node.create_linear
111
+ current_node.linear << [request_value, new_node]
112
+ else
113
+ current_node.create_lookup
114
+ current_nodes[index == 0 ? current_node_index : current_nodes.length] = (current_node.lookup[request_value] ||= router.request_node)
115
+ end
117
116
  end
118
117
  when 1 #this node is farther ahead
119
118
  current_nodes[current_node_index] = (current_node.catchall ||= router.request_node)
@@ -201,7 +200,7 @@ class HttpRouter
201
200
  end
202
201
 
203
202
  class RequestNode < Node
204
- RequestMethods = [:request_method, :host, :port, :scheme]
203
+ RequestMethods = [:request_method, :host, :port, :scheme, :user_agent, :ip, :fullpath, :query_string]
205
204
  attr_accessor :request_method
206
205
 
207
206
  def find_on_request_methods(request, alternate_request_methods)
@@ -211,7 +210,7 @@ class HttpRouter
211
210
  next_node = @linear.find do |(regexp, node)|
212
211
  regexp === request_value
213
212
  end
214
- next_node &&= next_node.find_on_request_methods(request, alternate_request_methods)
213
+ next_node &&= next_node.last.find_on_request_methods(request, alternate_request_methods)
215
214
  return next_node if next_node
216
215
  end
217
216
  if @lookup and next_node = (@lookup[request_value] && @lookup[request_value].find_on_request_methods(request, alternate_request_methods))
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  class HttpRouter #:nodoc
3
- VERSION = '0.3.6'.freeze
3
+ VERSION = '0.3.7'
4
4
  end
@@ -20,13 +20,23 @@ describe "HttpRouter#recognize" do
20
20
  end
21
21
  end
22
22
 
23
- context("with escaped ()'s") do
23
+ context("with escaping") do
24
24
  it "should recognize ()" do
25
25
  route = @router.add('/test\(:variable\)').to(:test)
26
26
  response = @router.recognize(Rack::MockRequest.env_for('/test(hello)'))
27
27
  response.route.should == route
28
28
  response.params.first.should == 'hello'
29
29
  end
30
+
31
+ it "should recognize :" do
32
+ route = @router.add('/test\:variable').to(:test)
33
+ @router.recognize(Rack::MockRequest.env_for('/test:variable')).dest.should == :test
34
+ end
35
+
36
+ it "should recognize *" do
37
+ route = @router.add('/test\*variable').to(:test)
38
+ @router.recognize(Rack::MockRequest.env_for('/test*variable')).dest.should == :test
39
+ end
30
40
  end
31
41
 
32
42
  context("with partial matching") do
@@ -169,6 +179,13 @@ describe "HttpRouter#recognize" do
169
179
  @router.recognize(Rack::MockRequest.env_for('http://host2/test', :method => 'POST')).dest.should == :host2_post
170
180
  end
171
181
 
182
+ it "should be able to use regexp in request method conditions" do
183
+ @router.get("/test").host(/host1/).to(:with_regexp)
184
+ @router.get("/test").to(:without_regexp)
185
+ @router.recognize(Rack::MockRequest.env_for('http://host2/test')).dest.should == :without_regexp
186
+ @router.recognize(Rack::MockRequest.env_for('http://host2.host1.com/test')).dest.should == :with_regexp
187
+ end
188
+
172
189
  it "should try both specific and non-specifc routes" do
173
190
  @router.post("/test").host('host1').to(:post_host1)
174
191
  @router.add("/test").host('host2').to(:any_post2)
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: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 6
10
- version: 0.3.6
9
+ - 7
10
+ version: 0.3.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-31 00:00:00 -04:00
18
+ date: 2010-07-31 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency