http_router 0.3.12 → 0.3.13

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/lib/http_router.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'rack'
2
+ require 'set'
2
3
  require 'url_mount'
3
- require 'ext/rack/uri_escape'
4
4
  require 'http_router/node'
5
5
  require 'http_router/root'
6
6
  require 'http_router/variable'
@@ -55,7 +55,6 @@ class HttpRouter
55
55
  # * :middleware -- On recognition, store the route Response in env['router.response'] and always call the default app. Defaults to +false+.
56
56
  def initialize(*args, &block)
57
57
  default_app, options = args.first.is_a?(Hash) ? [nil, args.first] : [args.first, args[1]]
58
-
59
58
  @options = options
60
59
  @default_app = default_app || options && options[:default_app] || proc{|env| Rack::Response.new("Not Found", 404).finish }
61
60
  @ignore_trailing_slash = options && options.key?(:ignore_trailing_slash) ? options[:ignore_trailing_slash] : true
@@ -190,10 +189,10 @@ class HttpRouter
190
189
  else
191
190
  env['router'] = self
192
191
  if response = recognize(request) and !@middleware
193
- if response.matched? && response.route.dest && response.route.dest.respond_to?(:call)
192
+ if response.matched? && response.route.dest
194
193
  process_params(env, response)
195
194
  consume_path!(request, response) if response.partial_match?
196
- return response.route.dest.call(env)
195
+ return response.route.dest.call(env) if response.route.dest.respond_to?(:call)
197
196
  elsif !response.matched?
198
197
  return [response.status, response.headers, []]
199
198
  end
@@ -256,11 +255,19 @@ class HttpRouter
256
255
  Parts.new(path)
257
256
  end
258
257
 
258
+ def self.uri_escape!(s)
259
+ s.to_s.gsub!(/([^:\/?\[\]\-_~\.!\$&'\(\)\*\+,;=@a-zA-Z0-9]+)/n) { "%#{$1.unpack('H2'*$1.size).join('%').upcase}" }
260
+ end
261
+
262
+ def self.uri_unescape(s)
263
+ s.to_s.gsub(/((?:%[0-9a-fA-F]{2})+)/n){ [$1.delete('%')].pack('H*') }
264
+ end
265
+
259
266
  private
260
267
 
261
268
  def consume_path!(request, response)
262
269
  request.env["SCRIPT_NAME"] = (request.env["SCRIPT_NAME"] + response.matched_path)
263
- request.env["PATH_INFO"] = response.remaining_path || ""
270
+ request.env["PATH_INFO"] = response.remaining_path.nil? || response.remaining_path == '' ? '/' : response.remaining_path
264
271
  end
265
272
 
266
273
  def process_params(env, response)
@@ -149,7 +149,7 @@ class HttpRouter
149
149
  next_node = @linear.find do |(tester, node)|
150
150
  if tester.respond_to?(:matches?) and match = tester.matches?(parts)
151
151
  dupped_parts = parts.dup
152
- params.push((val = tester.consume(match, dupped_parts) and val.is_a?(Array)) ? val.map{|v| Rack::Utils.uri_unescape(v)} : Rack::Utils.uri_unescape(val))
152
+ params.push((val = tester.consume(match, dupped_parts) and val.is_a?(Array)) ? val.map{|v| HttpRouter.uri_unescape(v)} : HttpRouter.uri_unescape(val))
153
153
  parts.replace(dupped_parts) if response = node.find_on_parts(request, dupped_parts, params)
154
154
  elsif tester.respond_to?(:match) and match = tester.match(parts.whole_path) and match.begin(0) == 0
155
155
  dupped_parts = router.split(parts.whole_path[match[0].size, parts.whole_path.size])
@@ -164,7 +164,7 @@ class HttpRouter
164
164
  parts.shift
165
165
  return match.find_on_parts(request, parts, params)
166
166
  elsif @catchall
167
- params.push((val = @catchall.variable.consume(nil, parts) and val.is_a?(Array)) ? val.map{|v| Rack::Utils.uri_unescape(v)} : Rack::Utils.uri_unescape(val))
167
+ params.push((val = @catchall.variable.consume(nil, parts) and val.is_a?(Array)) ? val.map{|v| HttpRouter.uri_unescape(v)} : HttpRouter.uri_unescape(val))
168
168
  return @catchall.find_on_parts(request, parts, params)
169
169
  end
170
170
  end
@@ -46,7 +46,7 @@ class HttpRouter
46
46
  path = raw_url(args, options)
47
47
  raise InvalidRouteException.new if path !~ @path_validation_regex
48
48
  raise TooManyParametersException.new unless args.empty?
49
- Rack::Utils.uri_escape!(path)
49
+ HttpRouter.uri_escape!(path)
50
50
  generate_querystring(path, options)
51
51
  path
52
52
  end
@@ -39,7 +39,7 @@ class HttpRouter
39
39
  alias_method :destination, :dest
40
40
 
41
41
  def partial_match?
42
- remaining_path || route.partially_match?
42
+ route.partially_match?
43
43
  end
44
44
  end
45
45
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  class HttpRouter #:nodoc
3
- VERSION = '0.3.12'
3
+ VERSION = '0.3.13'
4
4
  end
@@ -40,12 +40,20 @@ describe "HttpRouter#recognize" do
40
40
  end
41
41
 
42
42
  context("with partial matching") do
43
- it "should match partially or completely" do
44
- route = @router.add("/test*").to(:test)
45
- @router.recognize(Rack::MockRequest.env_for('/test')).route.should == route
46
- response = @router.recognize(Rack::MockRequest.env_for('/test/optional'))
47
- response.route.should == route
48
- response.remaining_path.should == '/optional'
43
+ before(:each) do
44
+ route = @router.add("/test*").to{|env| []}
45
+ end
46
+
47
+ it "should match partially" do
48
+ route = @router.add("/test*").to{|env| env['PATH_INFO'].should == '/optional'; [200, {}, []]}
49
+ response = @router.call(Rack::MockRequest.env_for('/test/optional'))
50
+ response[0].should == 200
51
+ end
52
+
53
+ it "should match partially and use / for rest if the route matched completely" do
54
+ route = @router.add("/test*").to{|env| env['PATH_INFO'].should == '/'; [200, {}, []]}
55
+ response = @router.call(Rack::MockRequest.env_for('/test'))
56
+ response[0].should == 200
49
57
  end
50
58
 
51
59
  end
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: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 12
10
- version: 0.3.12
9
+ - 13
10
+ version: 0.3.13
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-08-08 00:00:00 -07:00
18
+ date: 2010-08-20 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -171,7 +171,6 @@ files:
171
171
  - http_router.gemspec
172
172
  - lib/ext/rack/rack_mapper.rb
173
173
  - lib/ext/rack/rack_urlmap.rb
174
- - lib/ext/rack/uri_escape.rb
175
174
  - lib/http_router.rb
176
175
  - lib/http_router/glob.rb
177
176
  - lib/http_router/interface/sinatra.rb
@@ -1,26 +0,0 @@
1
- module Rack::Utils
2
- def uri_escape(s)
3
- s.to_s.gsub(/([^:\/?\[\]\-_~\.!\$&'\(\)\*\+,;=@a-zA-Z0-9]+)/n) {
4
- '%'<<$1.unpack('H2'*$1.size).join('%').upcase
5
- }
6
- end
7
- module_function :uri_escape
8
- end unless Rack::Utils.respond_to?(:uri_escape)
9
-
10
- module Rack::Utils
11
- def uri_escape!(s)
12
- s.to_s.gsub!(/([^:\/?\[\]\-_~\.!\$&'\(\)\*\+,;=@a-zA-Z0-9]+)/n) {
13
- '%'<<$1.unpack('H2'*$1.size).join('%').upcase
14
- }
15
- end
16
- module_function :uri_escape!
17
- end unless Rack::Utils.respond_to?(:uri_escape!)
18
-
19
- module Rack::Utils
20
- def uri_unescape(s)
21
- s.to_s.gsub(/((?:%[0-9a-fA-F]{2})+)/n){
22
- [$1.delete('%')].pack('H*')
23
- }
24
- end
25
- module_function :uri_unescape
26
- end unless Rack::Utils.respond_to?(:uri_unescape)