Advertisement

Sinatra

Developer at entp.com
Apr. 13, 2009
Advertisement

Sinatra

  1. Sinatra
  2. Nicolás Sanguinetti foca http://entp.com
  3. ¿Qué es?
  4. Un ejemplo require quot;rubygemsquot; require quot;sinatraquot; get quot;/helloquot; do quot;<h1>Hello World</h1>quot; end y lo guardamos como hello.rb
  5. Un ejemplo $ gem install sinatra $ ruby hello.rb == Sinatra has taken the stage ... >> Listening on 0.0.0.0:4567
  6. Un ejemplo
  7. Features
  8. URLs Parametrizables get quot;/hello/:namequot; do |name| # también con params[:name] quot;<h1>Hello #{name}</h1>quot; end
  9. Vistas get quot;/hello/:namequot; do |name| @name = name erb :hello end y en views/hello.erb <h1>Hello <%= @name %></h1>
  10. Layouts ponemos en views/layout.erb <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot; quot;http://www.w3.org/TR/html4/strict.dtdquot;> <html lang=quot;enquot;> <head> <title>Hello <%= @name || quot;Worldquot; %> </head> <body> <%= yield %> </body> </html> y todas nuestras vistas se renderean “adentro” de esta (reemplazando al yield)
  11. HTTP get quot;/...quot; do put quot;/...quot; do quot;...quot; quot;...quot; end end post quot;/...quot; do delete quot;/...quot; do quot;...quot; quot;...quot; end end
  12. Pero PUT y DELETE... <form action=quot;/put/actionquot; method=quot;postquot;> <input type=quot;hiddenquot; name=quot;_methodquot; value=quot;putquot;> ... </form>
  13. Blog en 15 minutos * * para valores relativos de 15
  14. necesitamos guardar los posts en algún lado, así que... Usando un ORM con Sinatra (por ejemplo, DataMapper) require quot;dm-corequot; require quot;do_sqlite3quot; load quot;lib/models.rbquot; configure do DataMapper.setup(:default, quot;sqlite3:blog.dbquot;) end
  15. lib/models.rb class Post include DataMapper::Resource property :id, Serial property :title, String, :nullable => false property :permalink, String, :nullable => false property :body, Text, :nullable => false before :valid?, :set_permalink has n, :comments private def set_permalink self.permalink = title.gsub(/s+/, quot;-quot;) end end class Comment include DataMapper::Resource property :id, Serial property :post_id, Integer, :nullable => false property :author, String, :nullable => false property :body, Text, :nullable => false belongs_to :post end
  16. Qué queremos en el blog? get quot;/quot; do get quot;/:permalink/editquot; do |permalink| end end get quot;/newquot; do put quot;/:permalinkquot; do |permalink| end end post quot;/quot; do end get quot;/:permalinkquot; do |permalink| end post quot;/:permalink/commentsquot; do |permalink| end delete quot;/:permalinkquot; do |permalink| end
  17. Qué queremos en el blog? get quot;/quot; do get quot;/:permalink/editquot; do |permalink| end end get quot;/newquot; do put quot;/:permalinkquot; do |permalink| end end post quot;/quot; do end get quot;/:permalinkquot; do |permalink| end post quot;/:permalink/commentsquot; do |permalink| end delete quot;/:permalinkquot; do |permalink| end
  18. Qué queremos en el blog? get quot;/quot; do get quot;/:permalink/editquot; do |permalink| end end get quot;/newquot; do put quot;/:permalinkquot; do |permalink| end end post quot;/quot; do end get quot;/:permalinkquot; do |permalink| end post quot;/:permalink/commentsquot; do |permalink| end delete quot;/:permalinkquot; do |permalink| end
  19. Qué queremos en el blog? get quot;/quot; do get quot;/:permalink/editquot; do |permalink| end end get quot;/newquot; do put quot;/:permalinkquot; do |permalink| end end post quot;/quot; do end get quot;/:permalinkquot; do |permalink| end post quot;/:permalink/commentsquot; do |permalink| end delete quot;/:permalinkquot; do |permalink| end
  20. Qué queremos en el blog? get quot;/quot; do get quot;/:permalink/editquot; do |permalink| end end get quot;/newquot; do put quot;/:permalinkquot; do |permalink| end end post quot;/quot; do end get quot;/:permalinkquot; do |permalink| end post quot;/:permalink/commentsquot; do |permalink| end delete quot;/:permalinkquot; do |permalink| end
  21. Qué queremos en el blog? get quot;/quot; do get quot;/:permalink/editquot; do |permalink| end end get quot;/newquot; do put quot;/:permalinkquot; do |permalink| end end post quot;/quot; do end get quot;/:permalinkquot; do |permalink| end post quot;/:permalink/commentsquot; do |permalink| end delete quot;/:permalinkquot; do |permalink| end
  22. Qué queremos en el blog? get quot;/quot; do get quot;/:permalink/editquot; do |permalink| end end get quot;/newquot; do put quot;/:permalinkquot; do |permalink| end end post quot;/quot; do end get quot;/:permalinkquot; do |permalink| end post quot;/:permalink/commentsquot; do |permalink| end delete quot;/:permalinkquot; do |permalink| end
  23. Qué queremos en el blog? get quot;/quot; do get quot;/:permalink/editquot; do |permalink| end end get quot;/newquot; do put quot;/:permalinkquot; do |permalink| end end post quot;/quot; do end get quot;/:permalinkquot; do |permalink| end post quot;/:permalink/commentsquot; do |permalink| end delete quot;/:permalinkquot; do |permalink| end
  24. Qué queremos en el blog? get quot;/quot; do get quot;/:permalink/editquot; do |permalink| end end get quot;/newquot; do put quot;/:permalinkquot; do |permalink| end end post quot;/quot; do end get quot;/:permalinkquot; do |permalink| end post quot;/:permalink/commentsquot; do |permalink| end delete quot;/:permalinkquot; do |permalink| end
  25. Creemos un Post entonces get quot;/newquot; do @post = Post.new erb :new end post quot;/quot; do @post = Post.new(params[:post]) if @post.save redirect quot;/#{@post.permalink}quot; else erb :new end end
  26. views/new.erb <form method=quot;postquot; action=quot;/quot;> <div><label for=quot;post_titlequot;>Title</label> <input type=quot;textquot; name=quot;post[title]quot; id=quot;post_titlequot; value=quot;<%= @post.title %>quot;></div> <div><label for=quot;post_bodyquot;>Your Article</label> <textarea id=quot;post_bodyquot; name=quot;post[body]quot; rows=quot;20quot; cols=quot;40quot;><%= @post.body %></textarea> </div> <div><button type=quot;submitquot;>Post new article</button> or <a href=quot;/#{@post.permalink}quot;>go back</a>.</div> </form>
  27. Listando posts get quot;/quot; do @posts = Post.all erb :index end
  28. views/index.erb <h2>All Posts</h2> <ul id=quot;postsquot;> <% for post in @posts %> <li class=quot;postquot;> <h2> <a href=quot;/<%= post.permalink %>quot;> <%= post.title %> </a> </h2> <%= post.body %> </li> <% end %> </ul>
  29. Un paréntesis: helpers! helpers do def post_path(post) quot;/#{post.permalink}quot; end end
  30. Mostrando un post get quot;/:permalinkquot; do |permalink| @post = Post.first(:permalink => permalink) erb :show end
  31. views/show.erb <h2> <a href=quot;<%= post_path(@post) %>quot;> <%= @post.title %> </a> </h2> <%= @post.body %> <div><a href=quot;/quot;>Go back to the list</a></div>
  32. Parentesis: “partials” views/post.erb <h2> <a href=quot;<%= post_path(post) %>quot;> <%= post.title %> </a> </h2> <%= post.body %> views/post.erb <%= erb(:post, :layout => false, :locals => { :post => @post }) %> <div><a href=quot;/quot;>Go back to the list</a></div>
  33. Comentarios get quot;/:permalinkquot; do |permalink| @post = Post.first(:permalink => permalink) @comment = Comment.new erb :show end post quot;/:permalink/commentsquot; do |permalink| @post = Post.first(:permalink => permalink) @comment = @post.comments.build(params[:comment]) if @comment.save redirect post_path(@post) else erb :show end end
  34. y en show.erb <div id=quot;commentsquot;> <ul> <% for comment in @post.comments %> <li><p><%= escape_html comment.body %></p> <address>&mdash;<%= escape_html comment.author %></address></li> <% end %> </ul> <form method=quot;postquot; action=quot;<%= post_path(@post) %>/commentsquot;> <div><label for=quot;comment_authorquot;>Name</label> <input type=quot;textquot; name=quot;comment[author]quot; id=quot;comment_authorquot; value=quot;<%= @comment.author %>quot;></div> <div><label for=quot;comment_bodyquot;>Your Comment</label> <textarea id=quot;comment_bodyquot; name=quot;comment[body]quot; rows=quot;6quot; cols=quot;40quot;><%= @comment.body %></textarea></div> <div><button type=quot;submitquot;>Leave Comment</button></div> </form> </div>
  35. Embelleciendo un poco... ponemos en views/layout.erb <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot; quot;http://www.w3.org/TR/html4/strict.dtdquot;> <html lang=quot;enquot;> <head> <meta http-equiv=quot;Content-Typequot; content=quot;text/html; charset=utf-8quot;> <link href=quot;/styles.cssquot; rel=quot;stylesheetquot; type=quot;text/cssquot;> <title><%= page_title %></title> </head> <body> <div id=quot;headerquot;> <h1><%= page_title %></h1> </div> <%= yield %> </body> </html>
  36. Embelleciendo un poco... helpers do def post_path(post) quot;/#{post.permalink}quot; end def page_title if @post && !@post.new_record? quot;Awesome Blog | #{@post.title}quot; else quot;Awesome Blogquot; end end end
  37. Embelleciendo un poco... ponemos en views/layout.erb <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot; quot;http://www.w3.org/TR/html4/strict.dtdquot;> <html lang=quot;enquot;> <head> <meta http-equiv=quot;Content-Typequot; content=quot;text/html; charset=utf-8quot;> <link href=quot;/styles.cssquot; rel=quot;stylesheetquot; type=quot;text/cssquot;> <title><%= page_title %></title> </head> <body> <div id=quot;headerquot;> <h1><%= page_title %></h1> </div> <%= yield %> </body> </html>
  38. Embelleciendo un poco... public/styles.css
  39. y editar y borrar... para la próxima
  40. es una interfaz entre servidores y frameworks
  41. class RackIsEasy def initialize(app) @app = app end def call(env) [200, {quot;Content-Typequot; => quot;text/htmlquot;}, [quot;Okquot;]] end end
  42. class AndRackIsPotentiallyDumb def initialize(app) @app = app end def call(env) @app.call(env) end end
  43. HTTP app_1 Middlewares app_2 app_3 App app_4 http://mwrc2009.confreaks.com/ – Jon Crosby
  44. para qué sirve eso?
  45. por ejemplo, caching: class MightyCache def initialize(app) @app = app end def call(env) if response = cache_hit?(env) response else cache_store(env, @app.call(env)) end end # def cache_hit?, cache_store, etc end
  46. Sinatra is on crack
  47. última cosa
  48. prometo que ya me voy
  49. m/
  50. Rails Metals en app/metal/api.rb class Api def self.call(env) if env[quot;PATH_INFOquot;] =~ /^/stuff.json/ [200, {quot;Content-Typequot; => quot;application/jsonquot;}, quot;{}quot;] else [404, {quot;Content-Typequot; => quot;application/jsonquot;}, quot;quot;] end end end
  51. Hard Metal Frank Sinatra Sinatra on Rails
  52. El ejemplo anterior: class Api < Sinatra::Base get quot;/stuffquot;, :provides => quot;application/jsonquot; do quot;{}quot; end end y en config/environment.rb config.gem quot;sinatraquot;
  53. Preguntas? Questions? http://github.com/foca/sinatra-example-blog contacto@nicolassanguinetti.info
Advertisement