Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Cuba

Introduction to Cuba

Introduction to Cuba "The Ruby Microframework"

Francesco Rodríguez

September 04, 2014
Tweet

More Decks by Francesco Rodríguez

Other Decks in Programming

Transcript

  1. Cuba.define do! on "home" do # PATH == "/home"! !

    res.write("Hello Cuba")! end! end
  2. Cuba.define do! on "home" do # PATH == "/home"! !

    res.write("Hello Cuba")! end! ! on "about" do # PATH == "/about"! ! res.write("About us”)! end! end
  3. Cuba.define do! on "hello/:name" do |name|! ! res.write("Hello #{ name

    }")! end! end! ! # /hello/frodsan => Hello frodsan
  4. Cuba.define do! on /hello\/(^[a-zA-Z]+$)/ do |name|! ! res.write("Hello #{ name

    }")! end! end! ! # /hello/frodsan => Hello frodsan! # /hello/123 =>
  5. Cuba.define do! on :name do |name|! ! res.write("Hello #{ name

    }")! end! end! ! # /frodsan => Hello frodsan! # /123 => Hello 123
  6. Cuba.define do! # Any expression that! # evaluates to `true`.!

    on (*) do! ! res.write("Hello Cuba")! end! end
  7. Cuba.define do! # Any expression that! # evaluates to `true`.!

    on true do! ! res.write("Hello Cuba")! end! end
  8. Cuba.define do! on false do! ! res.write("⊙﹏⊙")! end! ! on

    true do! ! res.write("the winner")! end! end
  9. Cuba.define do! on authenticated? do! # . . .! end!

    ! on true do! ! res.status = 401! ! res.write("Not authorized")! end! end
  10. Cuba.define do! on "posts" do! # /posts/new! on "new" do!

    # . . .! end! ! ! ! # /posts/mypost! on :slug do |slug|! ! # . . .! end! end! end
  11. Cuba.define do! on "posts" do! on "new" do! ! #

    . . .! ! # on true do! on default do! res.status = 404! res.write("Not Found")! end! end! end
  12. Cuba.define do! on "posts" do! on get do! # METHOD

    == GET! end! ! on post do! on param("post") do |post|! end! end! end! end
  13. class Users < Cuba
 define do! on "posts" do! on

    "new" do! ! # . . .! end! ! on :slug do |slug|! ! # . . .! end! end! end! end
  14. Cuba.define do! on authenticated? do! run(Users)! end! ! on default

    do! ! res.status = 401! ! res.write("Not authorized")! end! end
  15. ʕ•ᴥ•ʔ ! • hache • mote • mote-render • malone


    • ohm • ost • rack-protection • requests • scrivener
 • shield
  16. ! on "signup" do! on post, param("user") do |params|! signup

    = Signup.new(params)! ! on signup.valid? do! user = User.create(params)! authenticate(user)! ! res.redirect("/dashboard")! end! ! on default do! end! end! end!
  17. ! on "posts" do! on post, param("post") do |params|! end!

    ! on :id do |id|! post = Post[id]! ! on default do! render("post", post: post)! end! end! ! on default do! render("posts")! end! end!