Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create case insensitive routes in Sinatra?

Tags:

sinatra

I'm playing around with Sinatra, and I would like to make one of my routes case insensitive. I tried adding the route like this:

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

But it doesn't match any permutation of /tileflood as expected. I tested the following regex on rubular.com, and it matches just fine. Am I missing something?

\/(?i)tileflood\/?
like image 450
Jonas Follesø Avatar asked Dec 23 '10 18:12

Jonas Follesø


1 Answers

You want a real regexp for your route:

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

Proof:

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>
like image 83
Phrogz Avatar answered Oct 30 '22 01:10

Phrogz