Giter VIP home page Giter VIP logo

cuba's Introduction

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

cuba's People

Contributors

foca avatar soveran avatar vangberg avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.