Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
146 lines (104 sloc) 3.17 KB

Cuba

n. a microframework for web development.

Cuba and Rum, by Jan Sochor

Community

Meet us on IRC: #cuba.rb on freenode.net

Description

Cuba is a microframework for web development originally inspired by Rum, a tiny but powerful mapper for Rack applications.

It integrates many templates via Tilt, and testing via Cutest and Capybara.

Usage

Here's a simple application:

# cat hello_world.rb
require "cuba"

Cuba.use Rack::Session::Cookie

Cuba.define do
  on get do
    on "hello" do
      res.write "Hello world!"
    end

    on true do
      res.redirect "/hello"
    end
  end
end

# cat hello_world_test.rb
require "cuba/test"

scope do
  test "Homepage" do
    visit "/"

    assert has_content?("Hello world!")
  end
end

To run it, you can create a config.ru:

# cat config.ru
require "hello_world"

run Cuba

Here's an example showcasing how different matchers work:

require "cuba"

Cuba.use Rack::Session::Cookie

Cuba.define do

  # /
  on "" do
    res.write "Home"
  end

  # /about
  on "about" do
    res.write "About"
  end

  # /styles/basic.css
  on "styles", extension("css") do |file|
    res.write "Filename: #{file}" #=> "Filename: basic"
  end

  # /post/2011/02/16/hello
  on "post/:y/:m/:d/:slug" do |y, m, d, slug|
    res.write "#{y}-#{m}-#{d} #{slug}" #=> "2011-02-16 hello"
  end

  # /username/foobar
  on "username/:username" do |username|

    user = User.find_by_username(username) # username == "foobar"

    # /username/foobar/posts
    on "posts" do

      # You can access `user` here, because the `on` blocks
      # are closures.
      res.write "Total Posts: #{user.posts.size}" #=> "Total Posts: 6"
    end

    # /username/foobar/following
    on "following" do
      res.write user.following.size #=> "1301"
    end
  end

  # /search?q=barbaz
  on "search", param("q") do |query|
    res.write "Searched for #{query}" #=> "Searched for barbaz"
  end

  on post do
    on "login"

      # POST /login, user: foo, pass: baz
      on param("user"), param("pass") do |user, pass|
        res.write "#{user}:#{pass}" #=> "foo:baz"
      end

      # If the params `user` and `pass` are not provided, this block will
      # get executed.
      on true do
        res.write "You need to provide user and pass!"
      end
    end
  end
end

That's it, you can now run rackup and enjoy what you have just created.

To read more about testing, check the documentation for Cutest and Capybara.

Installation

$ gem install cuba
Jump to Line
Something went wrong with that request. Please try again.