The so-called writings of Michael Gorsuch.

A Taste of Cuba

Late last week I ran across a neat little web framework called Cuba. I write small tools, so most of my work can easily fit into something like this. Something that it has that I’ve been dying for: the ability to match urls components only once. See my snippit below for an example:

require 'cuba'

Cuba.define do
  on "api/v1" do
    on "rooms/:id" do |id|
      @room = Room[id] 

      # below could be hit like so:
      # /api/v1/rooms/123/messages
      on "messages" do
        res.write "#{@room.name} has #{@room.messages.size} messages."
      end
    end 
  end

  on "api/v2" do
    # ...
  end
end

I’ll be applying this to a real-life production problem this week. For more information, see this great presentation.