elcuervo's nest

Cuba Libre chico

RATS

I've been wandering in the Ruby world a year now, from Rails to Sinatra. But sometimes i'd really want things simpler and perhaps more clean of developing web applications. Specially when you can get something fast and get total control. Thats when i've found Cuba a great web framework.

In that spirit Cuba its a powerful web framework aiming simplicity, speed and clean syntaxt. Cuba inherits all its power from Rack and Rum but now overpowers it to make it even cooler. Being refactored some time ago now looks a lot like Sinatra routes, here's an example

require 'cuba'

Cuba.define do
  # /
  on "" do
    res.write "Home page"
  end

  # /users/:username
  on "users/:username" do |username|
    user = User.find_by_username(username)

    # GET /users/:username
    on get do
      res.write "Hello, i'm #{user.name}"
    end

    # POST /users/:username w/params[:user]
    on post, param('user') do |query|
      if user
        user.update_attributes query
        res.write "user: #{user.id} updated!"
      end
    end

  end

end

This encourages a cleaner and simpler way of writing routes, besides it also includes the power of Tilt

require 'cuba'

Cuba.define do
  on "register" do
    @message = "Hello dude!"
    res.write render("templates/register.erb")
  end
end

I liked Cuba so much i started a little lib to include all the contrib of Cuba, and i called it cuba-sugar

Cuba Sugar

Which adds some things not so much, just to make writing a little easier following the way Cuba is doing stuff right now.

require 'cuba'
require 'cuba/sugar'

Cuba.define do
  on "faq" do
    # You can output the return of the block as the response
    as do
      "This are the most common questions"
    end
  end

  on "fancy" do
    @captcha = 123456
    as { render('templates/captcha.haml') }
  end

  on "coffee" do
    # And you can choose what HTTP code and extra headers
    as 419, {'Content-Type' => "text/teapot"} do
      "I DONT KNOW :(((("
    end
  end

  on "users/:username/json" do |username|
    user = User.find_by_username(username)
    as_json do
      {
        name:           user.name,
        location:       user.location,
        is_it_friday:   Time.now.friday?
      }
    end
  end

end