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

Ruby + Cuba + Ohm

Ruby + Cuba + Ohm

Cómo armar tu primer web app con Ruby sin rails, sin saber Ruby ni rails. Usando Cuba y Ohm con Redis.

Demo: https://github.com/kevinjhanna/barcampba12

BarCamp Buenos Aires 2012

5cca0e2c073c82240a361b7d870c08b2?s=128

Kevin J. Hanna

November 04, 2012
Tweet

Transcript

  1. Cómo armar tu primer web app con Ruby sin rails,

    sin saber Ruby ni rails. Usando Cuba y Ohm con Redis.
  2. @KevinJHanna

  3. None
  4. Ruby > ["foo", "bar"] => ["foo", "bar"] > hash =

    {foo: "bar"} => {:foo=>"bar"} > hash[:foo] => "bar"
  5. > name = "kevin" => "kevin" > "mi nombre es

    #{name}" => "mi nombre es kevin"
  6. Ruby On Rails framework: Cuba

  7. gems $ gem install cuba Cuba

  8. # cat app.rb require "cuba" Cuba.define do on root do

    res.write "Hola, barcamp!" end end
  9. # cat config.ru require "app" run Cuba

  10. $ shotgun -p 8080 $ gem install shotgun

  11. # cat app.rb require "cuba" Cuba.define do on root do

    res.write "Hola, barcamp!" end end
  12. # cat app.rb require "cuba" require "cuba/render" Cuba.plugin Cuba::Render Cuba.define

    do on root do res.write "Hola, barcamp!" end end
  13. # cat app.rb require "cuba" require "cuba/render" Cuba.plugin Cuba::Render Cuba.define

    do on root do res.write "Hola, barcamp!" end end
  14. # cat app.rb require "cuba" require "cuba/render" Cuba.plugin Cuba::Render Cuba.define

    do on root do res.write "Hola, barcamp!" end end
  15. # cat app.rb require "cuba" require "cuba/render" Cuba.plugin Cuba::Render Cuba.define

    do on root do res.write view("home") end end
  16. # cat app.rb require "cuba" require "cuba/render" Cuba.plugin Cuba::Render Cuba.define

    do on root do res.write(view("home")) end end
  17. # cat views/layout.erb <!DOCTYPE html> <html> <head> </head> <body> </body>

    </html>
  18. # cat views/layout.erb <!DOCTYPE html> <html> <head> </head> <body> <%=

    content %> </body> </html>
  19. # cat views/home.erb <h1>Charlas del BarCamp</h1>

  20. None
  21. # cat app.rb Cuba.define do on root do res.write(view("home")) end

    end # cat views/home.erb <h1>Charlas del BarCamp</h1>
  22. # cat app.rb Cuba.define do on root do res.write(view("home", place:

    "sala")) end end # cat views/home.erb <h1>Charlas del BarCamp</h1>
  23. # cat app.rb Cuba.define do on root do res.write(view("home", place:

    "sala")) end end # cat views/home.erb <h1>Charlas del BarCamp</h1> <p>En <%= place %></p>
  24. None
  25. # cat app.rb Cuba.define do on root do res.write(view("home", place:

    "sala")) end end
  26. # cat app.rb Cuba.define do on root do res.write(view("home", place:

    req.GET["place"])) end end
  27. # cat app.rb Cuba.define do on "lugares/:place" do |place| res.write(view("home",

    place: place)) end end
  28. NoSQL

  29. redis> SET mykey "Hello" OK redis> GET mykey "Hello"

  30. Ohm ॐ class Talk < Ohm::Model attribute :title attribute :description

    attribute :author end
  31. talk = Talk.create( title: "Cómo armar un app en Ruby",

    description: "Es muy divertida", author:"Kevin J. Hanna" ) talk.id # => 1 talk.title # => "Cómo armar un app en Ruby" talk == Talk[1] # => true
  32. # cat lib/talk.rb class Talk < Ohm::Model attribute :title attribute

    :description attribute :author end
  33. require "cuba" require "cuba/render" require "ohm" require_relative "lib/talk" Ohm.connect(host: "localhost",

    port: "6379") Cuba.plugin Cuba::Render Cuba.define do ... end
  34. require "cuba" require "cuba/render" require "ohm" require_relative "lib/talk" Ohm.connect(host: "localhost",

    port: "6379") Cuba.plugin Cuba::Render Cuba.define do ... end
  35. Cuba.define do end

  36. Cuba.define do on "charlas/crear" do on get do res.write(view("create")) end

    end end <h1>Subí tu charla de barcamp</h1> <form method=post action="/charlas/crear"> <input type=text name=title> <input type=text name=author> <textarea name=description></textarea> <button type=submit>Crear</button> </form>
  37. Cuba.define do on "charlas/crear" do on get do res.write(view("create")) end

    end end
  38. Cuba.define do on "charlas/crear" do on get do res.write(view("create")) end

    on post do talk = Talk.create( title: req.POST["title"], author: req.POST["author"], description: req.POST["description"], ) res.redirect("/charlas/#{talk.id}", 302) end end end
  39. Cuba.define do on "charlas/crear" do on get do res.write(view("create")) end

    on post do talk = Talk.create( title: req.POST["title"], author: req.POST["author"], description: req.POST["description"], ) res.redirect("/charlas/#{talk.id}", 302) end end on get, "charlas/:id" do |id| talk = Talk[id] res.write(view("talk", talk: talk)) end end
  40. Cuba.define do ... on root do res.write(view("home") end end

  41. Cuba.define do ... on root do res.write(view("home", talks: Talk.all)) end

    end <% talks.each do |talk| %> <p><%= talk.title %></p> <% end %>
  42. # cat lib/talk.rb class Talk < Ohm::Model attribute :title attribute

    :description attribute :author end
  43. # cat lib/talk.rb class Talk < Ohm::Model attribute :title attribute

    :description attribute :author collection :comments, :Comment end
  44. # cat lib/comment.rb class Comment < Ohm::Model attribute :body attribute

    :rating reference :talk, :Talk end
  45. Comment.create( talk_id: 1, body: "bueno", rating: 3 ) Comment[1].talk.title #

    => "Cómo armar un app en Ruby" Talk[1].comments # => [Comment[1]] * Comment[1].body # => "bueno"
  46. <header> <h1><%= talk.title %></h1> </header> <p>Autor: <%= talk.author %></p> <p><%=

    talk.description %></p> <h2>Escribir comentario</h2> <form method=post action="<%= "/charlas/#{talk.id}" %>"> <textarea name=body></textarea> <input type=text name=rating></input> <button type=submit>Comentar</button> </form>
  47. on get, "charlas/:id" do |id| talk = Talk[id] res.write(view("talk", talk:

    talk)) end
  48. on "charlas/:id" do |id| on get do res.write(view("talk", talk: Talk[id]))

    end end
  49. on "charlas/:id" do |id| on get do res.write(view("talk", talk: Talk[id]))

    end on post do Comment.create( rating: req.POST["rating"], body: req.POST["body"], talk_id: id, ) res.redirect("/charlas/#{id}", 302) end end
  50. <header> <h1><%= talk.title %></h1> </header> <p>Autor: <%= talk.author %></p> <p><%=

    talk.description %></p> <h2>Escribir comentario</h2> <form method=post action="<%= "/charlas/#{talk.id}" %>"> <textarea name=body></textarea> <input type=text name=rating></input> <button type=submit>Comentar</button> </form>
  51. <header> <h1><%= talk.title %></h1> </header> <p>Autor: <%= talk.author %></p> <p><%=

    talk.description %></p> <h2>Escribir comentario</h2> <form method=post action="<%= "/charlas/#{talk.id}" %>"> <textarea name=body></textarea> <input type=text name=rating></input> <button type=submit>Comentar</button> </form> <% talk.comments.each do |comment| %> <p><%= comment.rating %></p> <% end %>
  52. None
  53. None
  54. no vimos validaciones pero es super facil leyendo el readme

    de ohm
  55. gracias!

  56. @KevinJHanna Talk[1].preguntas? http://tryruby.org https://github.com/soveran/cuba https://github.com/soveran/ohm http://redis.io/ http://ruby-doc.org/