Scenario
I have a Sinatra App
I have a route that fetches articles based on a certain named path
# Get Articles for a certain time period
get '/frontpage/:type' do
case params[:type]
when "today"
@news = Article.find(...)
when "yesterday"
@news = Article.find(...)
when "week-ago"
@news = Article.find(...)
when "month-ago"
@news = Article.find(...)
else
not_found
end
erb :frontpage
end
Question
Is it possible to keep this route "/frontpage/:type" and show a .json page if for example someone ask for "/frontpage/:today.json" instead of "/frontpage/:type" ?
OR
Is it better to create a separate route specifically for requests for JSON ?
You will have to create a new route.
Though, you can factor your code like that: