# File lib/cuba.rb, line 58 def self.app @app ||= Rack::Builder.new end
# File lib/cuba.rb, line 74 def self.call(env) prototype.call(env) end
# File lib/cuba.rb, line 89 def self.deepclone(obj) Marshal.load(Marshal.dump(obj)) end
# File lib/cuba.rb, line 66 def self.define(&block) app.run new(&block) end
# File lib/cuba.rb, line 93 def self.inherited(child) child.settings.replace(deepclone(settings)) end
# File lib/cuba.rb, line 102 def initialize(&blk) @blk = blk @captures = [] end
# 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
# File lib/cuba.rb, line 70 def self.prototype @prototype ||= app.to_app end
# File lib/cuba.rb, line 53 def self.reset! @app = nil @prototype = nil end
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
# 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
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
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
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
# File lib/cuba.rb, line 331 def halt(response) throw :halt, response end
# File lib/cuba.rb, line 247 def header(key) lambda { env[key.upcase.tr("-","_")] } 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
# File lib/cuba.rb, line 257 def host(hostname) hostname === req.host end
# 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
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
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
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
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
Generated with the Darkfish Rdoc Generator 2.