Roda – the routing tree
Roda (by Jeremy Evans, the author of Sequel) is a modern, very fast web framework. Instead of a flat list of routes like Sinatra, you climb a tree: the route block receives the request r, and you decide piece by piece what happens with the path:
install_gem "roda"
require "roda"
class Shop < Roda
route do |r|
r.root do
"<h1>Chunky's Shop</h1>
<a href='/bacon'>Bacon</a>
<a href='/greet/Chunky'>Greeting</a>"
end
r.get "bacon" do
"<p>Bacon: 3 strips for 2 francs.</p><a href='/'>Back</a>"
end
r.get "greet", String do |name|
"Hello, #{name}! <a href='/'>Back</a>"
end
end
end
show_browser Shop, "/"Read the tree top to bottom: r.root catches /, r.get "bacon" catches GET /bacon. It gets interesting with r.get "greet", String: that matches /greet/<anything>, and the path segment arrives as the block parameter name – try /greet/Ada in the mini browser! If nothing matches, Roda automatically answers with 404.
Look it up: Roda documentation · Conventions for bigger apps
Task: Add the route
r.get "order", Integer do |amount| … end to the kiosk so that e.g. /order/5 returns 5 strips of bacon, coming right up! (use interpolation). The mini browser below still shows a 404.install_gem "roda"
require "roda"
class Kiosk < Roda
route do |r|
r.root do
"<h1>Kiosk</h1><a href='/order/5'>Order 5 strips</a>"
end
# r.get "order", Integer do |amount|
# ...
# end
end
end
show_browser Kiosk, "/order/5"