Parent

Namespace

Class/Module Index [+]

Quicksearch

Cuba

Attributes

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

Public Class Methods

app() click to toggle source
# File lib/cuba.rb, line 58
def self.app
  @app ||= Rack::Builder.new
end
call(env) click to toggle source
# File lib/cuba.rb, line 74
def self.call(env)
  prototype.call(env)
end
deepclone(obj) click to toggle source
# File lib/cuba.rb, line 89
def self.deepclone(obj)
  Marshal.load(Marshal.dump(obj))
end
define(&block) click to toggle source
# File lib/cuba.rb, line 66
def self.define(&block)
  app.run new(&block)
end
inherited(child) click to toggle source
# File lib/cuba.rb, line 93
def self.inherited(child)
  child.settings.replace(deepclone(settings))
end
new(&blk) click to toggle source
# File lib/cuba.rb, line 102
def initialize(&blk)
  @blk = blk
  @captures = []
end
plugin(mixin) click to toggle source
# File lib/cuba.rb, line 78
def self.plugin(mixin)
  include mixin
  extend  mixin::ClassMethods if defined?(mixin::ClassMethods)

  mixin.setup(self) if mixin.respond_to?(:setup)
end
prototype() click to toggle source
# File lib/cuba.rb, line 70
def self.prototype
  @prototype ||= app.to_app
end
reset!() click to toggle source
# File lib/cuba.rb, line 53
def self.reset!
  @app = nil
  @prototype = nil
end
settings() click to toggle source
# File lib/cuba.rb, line 85
def self.settings
  @settings ||= {}
end
use(middleware, *args, &block) click to toggle source
# File lib/cuba.rb, line 62
def self.use(middleware, *args, &block)
  app.use(middleware, *args, &block)
end

Public Instance Methods

accept(mimetype) click to toggle source

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
# File lib/cuba.rb, line 269
def accept(mimetype)
  lambda do
    accept = String(env["HTTP_ACCEPT"]).split(",")

    if accept.any? { |s| s.strip == mimetype }
      res["Content-Type"] = mimetype
    end
  end
end
call(env) click to toggle source
# File lib/cuba.rb, line 111
def call(env)
  dup.call!(env)
end
call!(env) click to toggle source
# File lib/cuba.rb, line 115
def call!(env)
  @env = env
  @req = settings[:req].new(env)
  @res = settings[:res].new

  # This `catch` statement will either receive a
  # rack response tuple via a `halt`, or will
  # fall back to issuing a 404.
  #
  # When it `catch`es a throw, the return value
  # of this whole `call!` method will be the
  # rack response tuple, which is exactly what we want.
  catch(:halt) do
    instance_eval(&@blk)

    res.status = 404
    res.finish
  end
end
default() click to toggle source

Syntactic sugar for providing catch-all matches.

@example

on default do
  res.write "404"
end
# File lib/cuba.rb, line 285
def default
  true
end
delete() click to toggle source
# File lib/cuba.rb, line 312
def delete; req.delete? end
extension(ext = "\\w+") click to toggle source

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
# File lib/cuba.rb, line 231
def extension(ext = "\\w+")
  lambda { consume("([^\\/]+?)\.#{ext}\\z") }
end
get() click to toggle source

Syntatic sugar for providing HTTP Verb matching.

@example

on get, "signup" do
end

on post, "signup" do
end
# File lib/cuba.rb, line 309
def get;    req.get?    end
halt(response) click to toggle source
# File lib/cuba.rb, line 331
def halt(response)
  throw :halt, response
end
header(key) click to toggle source
# File lib/cuba.rb, line 247
def header(key)
  lambda { env[key.upcase.tr("-","_")] }
end
host(hostname) click to toggle source

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
# File lib/cuba.rb, line 257
def host(hostname)
  hostname === req.host
end
match(matcher, segment = "([^\\/]+)") click to toggle source
# File lib/cuba.rb, line 213
def match(matcher, segment = "([^\\/]+)")
  case matcher
  when String then consume(matcher.gsub(/:\w+/, segment))
  when Regexp then consume(matcher)
  when Symbol then consume(segment)
  when Proc   then matcher.call
  else
    matcher
  end
end
on(*args, &block) click to toggle source

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
# File lib/cuba.rb, line 161
def on(*args, &block)
  try do
    # For every block, we make sure to reset captures so that
    # nesting matchers won't mess with each other's captures.
    @captures = []

    # We stop evaluation of this entire matcher unless
    # each and every `arg` defined for this matcher evaluates
    # to a non-false value.
    #
    # Short circuit examples:
    #    on true, false do
    #
    #    # PATH_INFO=/user
    #    on true, "signup"
    return unless args.all? { |arg| match(arg) }

    # The captures we yield here were generated and assembled
    # by evaluating each of the `arg`s above. Most of these
    # are carried out by #consume.
    yield(*captures)

    halt res.finish
  end
end
param(key) click to toggle source

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
# File lib/cuba.rb, line 243
def param(key)
  lambda { captures << req[key] unless req[key].to_s.empty? }
end
post() click to toggle source
# File lib/cuba.rb, line 310
def post;   req.post?   end
put() click to toggle source
# File lib/cuba.rb, line 311
def put;    req.put?    end
root() click to toggle source

Access the root of the application.

@example

# GET /
on root do
  res.write "Home"
end
# File lib/cuba.rb, line 297
def root
  env["PATH_INFO"] == "/" || env["PATH_INFO"] == ""
end
run(app) click to toggle source

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
# File lib/cuba.rb, line 327
def run(app)
  halt app.call(req.env)
end
session() click to toggle source
# File lib/cuba.rb, line 135
def session
  env["rack.session"] || raise(RuntimeError,
    "You're missing a session handler. You can get started " +
    "by adding Cuba.use Rack::Session::Cookie")
end
settings() click to toggle source
# File lib/cuba.rb, line 107
def settings
  self.class.settings
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.