Permalink
Browse files

have decompile work properly with Sinatra 1.3

  • Loading branch information...
1 parent a946e99 commit 7c4eb00e4d7839d6675a4bd463813d3ad2edf802 @rkh committed Aug 17, 2011
Showing with 19 additions and 1 deletion.
  1. +17 −1 lib/sinatra/decompile.rb
  2. +2 −0 spec/decompile_spec.rb
View
@@ -1,5 +1,6 @@
require 'sinatra/base'
require 'backports'
+require 'uri'
module Sinatra
@@ -73,15 +74,20 @@ def decompile(pattern, keys = nil, *)
keys, str = keys.try(:dup), pattern.inspect
return pattern unless str.start_with? '/' and str.end_with? '/'
str.gsub! /^\/\^?|\$?\/$/, ''
+ str.gsub! encoded(' '), ' '
return pattern if str =~ /^[\.\+]/
- str.gsub! /\([^\(]*\)/ do |part|
+ str.gsub! /\([^\(\)]*\)/ do |part|
case part
when '(.*?)'
return pattern if keys.shift != 'splat'
'*'
when '([^\/?#]+)'
return pattern if keys.empty?
":" << keys.shift
+ when /^\(\?\:\\?(.)\|/
+ char = $1
+ return pattern unless encoded(char) == part
+ Regexp.escape(char)
else
return pattern
end
@@ -91,6 +97,16 @@ def decompile(pattern, keys = nil, *)
$2
end
end
+
+ private
+
+ def encoded(char)
+ return super if defined? super
+ enc = URI.encode(char)
+ enc = "(?:#{Regexp.escape enc}|#{URI.encode char, /./})" if enc == char
+ enc = "(?:#{enc}|#{encoded('+')})" if char == " "
+ enc
+ end
end
register Decompile
View
@@ -28,6 +28,8 @@
it { should decompile("/*/foo/*") }
it { should decompile("*") }
it { should decompile(":name.:format") }
+ it { should decompile("a b") }
+ it { should decompile("a+b") }
it { should decompile(/./) }
it { should decompile(/f(oo)/) }
it { should decompile(/ba+r/) }

0 comments on commit 7c4eb00

Please sign in to comment.