I am trying to make a small sinatra example that will print a calendar view for either a month, day or year depending on the URL. Such that
localhost:4567/calendar/2012 will print a year view calendar
localhost:4567/calendar/2012/9 will print a month calendar
localhost:4567/calendar/2012/9/15 will print a day view calendar
I have this working as this:
require 'sinatra'
get '/calendar/:year/:month/:day/?' do
"printing daily calendar for #{params[:year]}/#{params[:month]}/#{params[:day]}"
end
get '/calendar/:year/:month/?' do
"printing monthly calendar for #{params[:year]}/#{params[:month]}"
end
get '/calendar/:year/?' do
"printing yearly calendar for #{params[:year]}"
end
My question is, can I further refine those routes with some kind of RegEx so that I can say the url only counts if the :month portion is between 1 and 12 and the :day portion is between 1 and 31?
Yes. You can specify regex in sinatra routing condition. See sinatra readme for more information.