DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Basic Sinatra Routes

04.16.2010
| 5296 views |
  • submit to reddit
        In the following example the routes can determine if the URI is an alias, a file, a directory, or is the root path. 
#!/usr/bin/ruby  

# file: test.rb

require 'sinatra'


get '/' do
  "home"
end

get '/:file*.*' do
  "file"
end

get '/:alias' do
  "alias"
end

get '/:directory/' do  
  "directory"
end

<pre>
Tested (http://niko:4567)        observed
-----------------------          --------
              /README            alias
              /README/           directory
              /README/fun.txt    file
              /fun               alias
              /                  home
              /home.txt          file
              /home.txt/         file 
              /home/file.txt     file
              /home/f            Sinatra doesn't know this ditty
              /home/             directory
              /home?edit=1       alias
              /home/?edit=1      directory
              /home.xml?edit=1   file
</pre>

Note: Alias in this context means a URI which will be redirected to another URI

Resources:
 - <a href="http://www.sinatrarb.com/intro">Sinatra: README</a> [sinatrarb.com]