def remap(map)
longest_path_first = lambda do |(host, location, _, _)|
[host ? -host.size : NEGATIVE_INFINITY, -location.size]
end
@mapping = map.map { |location, app|
if location =~ %r{\Ahttps?://(.*?)(/.*)}
host, location = $1, $2
else
host = nil
end
unless location[0] == ?/
raise ArgumentError, "paths need to start with /"
end
location = location.chomp('/')
match = Regexp.new("^#{Regexp.quote(location).gsub('/', '/+')}(.*)", nil, 'n')
[host, location, match, app]
}.sort_by(&longest_path_first)
end