Class: Jellyfish::URLMap
- Inherits:
-
Object
- Object
- Jellyfish::URLMap
- Defined in:
- lib/jellyfish/urlmap.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(mapped) ⇒ URLMap
constructor
A new instance of URLMap.
Constructor Details
#initialize(mapped) ⇒ URLMap
Returns a new instance of URLMap.
4 5 6 7 8 9 10 11 |
# File 'lib/jellyfish/urlmap.rb', line 4 def initialize mapped string = mapped.keys.sort_by{ |k| -k.size }. map{ |k| Regexp.escape(k).gsub('/', '/+') }. join('|') @mapped = mapped @routes = Regexp.new("\\A(?:#{string})(?:/|\\z)", 'i', 'n') end |
Instance Method Details
#call(env) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/jellyfish/urlmap.rb', line 13 def call env path_info = env['PATH_INFO'] matched = @routes.match(path_info).to_s.chomp('/') squeezed = matched.squeeze('/') if app = @mapped[squeezed] app.call(env.merge('PATH_INFO' => path_info[matched.size..-1], 'SCRIPT_NAME' => env['SCRIPT_NAME'] + squeezed)) else [404, {}, []] end end |