How do you create case-insensitive routes in Sinatra?

I play with Sinatra and I would like to make one of my routes case insensitive. I tried to add a route as follows:

get "(?i)/tileflood/?" do end 

But it does not match the permutation / tileflood as expected. I checked the following regular expression on rubular.com and it only matches the fine. Did I miss something?

 \/(?i)tileflood\/? 
+7
source share
1 answer

You want to have a real regex for your route:

 require 'sinatra' get %r{^/tileflood/?$}i do request.url + "\n" end 

Evidence:

 smagic:~ phrogz$ curl http://localhost:4567/tileflood http://localhost:4567/tileflood smagic:~ phrogz$ curl http://localhost:4567/tIlEflOOd http://localhost:4567/tIlEflOOd smagic:~ phrogz$ curl http://localhost:4567/TILEFLOOD/ http://localhost:4567/TILEFLOOD/ smagic:~ phrogz$ curl http://localhost:4567/TILEFLOOD/z <!DOCTYPE html> <html> <head> <style type="text/css"> body { text-align:center;font-family:helvetica,arial;font-size:22px; color:#888;margin:20px} #c {margin:0 auto;width:500px;text-align:left} </style> </head> <body> <h2>Sinatra doesn't know this ditty.</h2> <img src='/__sinatra__/404.png'> <div id="c"> Try this: <pre>get '/TILEFLOOD/z' do "Hello World" end</pre> </div> </body> </html> smagic:~ phrogz$ curl http://localhost:4567/tileflaad <!DOCTYPE html> <html> <head> <style type="text/css"> body { text-align:center;font-family:helvetica,arial;font-size:22px; color:#888;margin:20px} #c {margin:0 auto;width:500px;text-align:left} </style> </head> <body> <h2>Sinatra doesn't know this ditty.</h2> <img src='/__sinatra__/404.png'> <div id="c"> Try this: <pre>get '/tileflaad' do "Hello World" end</pre> </div> </body> </html> 
+8
source

All Articles