Class Cuba
In: lib/cuba/version.rb
lib/cuba.rb
Parent: Object

Methods

_call   accept   app   build   call   call   default   define   delete   extension   get   halt   header   host   match   method_added   method_added   new   on   param   post   prototype   put   render   reset!   run   use  

Classes and Modules

Class Cuba::RedefinitionError

Constants

VERSION = "2.2.1"

Attributes

captures  [R] 
env  [R] 
req  [R] 
res  [R] 

Public Class methods

In order to prevent people from overriding the standard Cuba methods like `get`, `put`, etc, we add this as a safety measure.

Public Instance methods

If you want to match against the HTTP_ACCEPT value.

@example

  # HTTP_ACCEPT=application/xml
  on accept("application/xml") do
    # automatically set to application/xml.
    res.write res["Content-Type"]
  end

Syntactic sugar for providing catch-all matches.

@example

  on default do
    res.write "404"
  end

A matcher for files with a certain extension.

@example

  # PATH_INFO=/style/app.css
  on "style", extension("css") do |file|
    res.write file # writes app
  end

Syntatic sugar for providing HTTP Verb matching.

@example

  on get, "signup" do
  end

  on post, "signup" do
  end

Useful for matching against the request host (i.e. HTTP_HOST).

@example

  on host("account1.example.com"), "api" do
    res.write "You have reached the API of account1."
  end

The heart of the path / verb / any condition matching.

@example

  on get do
    res.write "GET"
  end

  on get, "signup" do
    res.write "Signup
  end

  on "user/:id" do |uid|
    res.write "User: #{uid}"
  end

  on "styles", extension("css") do |file|
    res.write render("styles/#{file}.sass")
  end

Used to ensure that certain request parameters are present. Acts like a precondition / assertion for your route.

@example

  # POST with data like user[fname]=John&user[lname]=Doe
  on "signup", param("user") do |atts|
    User.create(atts)
  end

Render any type of template file supported by Tilt.

@example

  # Renders home, and is assumed to be HAML.
  render("home.haml")

  # Renders with some local variables
  render("home.haml", site_name: "My Site")

  # Renders with HAML options
  render("home.haml", {}, ugly: true, format: :html5)

  # Renders in layout
  render("layout.haml") { render("home.haml") }

If you want to halt the processing of an existing handler and continue it via a different handler.

@example

  def redirect(*args)
    run Cuba.new { on(default) { res.redirect(*args) }}
  end

  on "account" do
    redirect "/login" unless session["uid"]

    res.write "Super secure account info."
  end

[Validate]