Skip to content
This repository
Fabian Rodriguez
file 338 lines (281 sloc) 7.127 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
require "rack"
require "time"

class Cuba
  class Response
    attr_accessor :status

    attr :headers

    def initialize(status = 200,
                   headers = { "Content-Type" => "text/html; charset=utf-8" })

      @status = status
      @headers = headers
      @body = []
      @length = 0
    end

    def [](key)
      @headers[key]
    end

    def []=(key, value)
      @headers[key] = value
    end

    def write(str)
      s = str.to_s

      @length += s.bytesize
      @headers["Content-Length"] = @length.to_s
      @body << s
    end

    def redirect(path, status = 302)
      @headers["Location"] = path
      @status = status
    end

    def finish
      [@status, @headers, @body]
    end

    def set_cookie(key, value)
      Rack::Utils.set_cookie_header!(@headers, key, value)
    end

    def delete_cookie(key, value = {})
      Rack::Utils.delete_cookie_header!(@headers, key, value)
    end
  end

  def self.reset!
    @app = nil
    @prototype = nil
  end

  def self.app
    @app ||= Rack::Builder.new
  end

  def self.use(middleware, *args, &block)
    app.use(middleware, *args, &block)
  end

  def self.define(&block)
    app.run new(&block)
  end

  def self.prototype
    @prototype ||= app.to_app
  end

  def self.call(env)
    prototype.call(env)
  end

  def self.plugin(mixin)
    include mixin
    extend mixin::ClassMethods if defined?(mixin::ClassMethods)

    mixin.setup(self) if mixin.respond_to?(:setup)
  end

  def self.settings
    @settings ||= {}
  end

  def self.deepclone(obj)
    Marshal.load(Marshal.dump(obj))
  end

  def self.inherited(child)
    child.settings.replace(deepclone(settings))
  end

  attr :env
  attr :req
  attr :res
  attr :captures

  def initialize(&blk)
    @blk = blk
    @captures = []
  end

  def settings
    self.class.settings
  end

  def call(env)
    dup.call!(env)
  end

  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

  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

  # 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
  #
  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

  # @private Used internally by #on to ensure that SCRIPT_NAME and
  # PATH_INFO are reset to their proper values.
  def try
    script, path = env["SCRIPT_NAME"], env["PATH_INFO"]

    yield

  ensure
    env["SCRIPT_NAME"], env["PATH_INFO"] = script, path
  end
  private :try

  def consume(pattern)
    matchdata = env["PATH_INFO"].match(/\A\/(#{pattern})(\/|\z)/)

    return false unless matchdata

    path, *vars = matchdata.captures

    env["SCRIPT_NAME"] += "/#{path}"
    env["PATH_INFO"] = "#{vars.pop}#{matchdata.post_match}"

    captures.push(*vars)
  end
  private :consume

  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

  # 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
  def extension(ext = "\\w+")
    lambda { consume("([^\\/]+?)\.#{ext}\\z") }
  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
  def param(key)
    lambda { captures << req[key] unless req[key].to_s.empty? }
  end

  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
  def host(hostname)
    hostname === req.host
  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
  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

  # Syntactic sugar for providing catch-all matches.
  #
  # @example
  # on default do
  # res.write "404"
  # end
  def default
    true
  end

  # Access the root of the application.
  #
  # @example
  #
  # # GET /
  # on root do
  # res.write "Home"
  # end
  def root
    env["PATH_INFO"] == "/" || env["PATH_INFO"] == ""
  end

  # Syntatic sugar for providing HTTP Verb matching.
  #
  # @example
  # on get, "signup" do
  # end
  #
  # on post, "signup" do
  # end
  def get; req.get? end
  def post; req.post? end
  def put; req.put? end
  def delete; req.delete? 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
  def run(app)
    halt app.call(req.env)
  end

  def halt(response)
    throw :halt, response
  end
end

Cuba.settings[:req] = Rack::Request
Cuba.settings[:res] = Cuba::Response
Something went wrong with that request. Please try again.