Sinatra
Upcoming SlideShare
Loading in...5
×
 

Sinatra

on

  • 8,818 views

Charla brindada en Locos x Rails en Buenos Aires, Argentina, el 4 de abril de 2009 sobre el framework web Sinatra....

Charla brindada en Locos x Rails en Buenos Aires, Argentina, el 4 de abril de 2009 sobre el framework web Sinatra.

Breve descripción del framework y sus capacidades, una muy breve introducción a Rack y cómo funciona, y una muestra rápida de los Rails Metals, y de cómo integrar aplicaciones sinatra a nuestras aplicaciones en Rails.

Statistics

Views

Total Views
8,818
Views on SlideShare
8,765
Embed Views
53

Actions

Likes
6
Downloads
138
Comments
0

4 Embeds 53

http://www.slideshare.net 42
http://www.linkedin.com 6
http://coderwall.com 4
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Sinatra Sinatra Presentation Transcript

  • Sinatra
  • Nicolás Sanguinetti foca http://entp.com
  • ¿Qué es?
  • 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
  • Un ejemplo $ gem install sinatra $ ruby hello.rb == Sinatra has taken the stage ... >> Listening on 0.0.0.0:4567
  • Un ejemplo
  • Features
  • URLs Parametrizables get quot;/hello/:namequot; do |name| # también con params[:name] quot;<h1>Hello #{name}</h1>quot; end
  • Vistas get quot;/hello/:namequot; do |name| @name = name erb :hello end y en views/hello.erb <h1>Hello <%= @name %></h1>
  • 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)
  • 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
  • Pero PUT y DELETE... <form action=quot;/put/actionquot; method=quot;postquot;> <input type=quot;hiddenquot; name=quot;_methodquot; value=quot;putquot;> ... </form>
  • Blog en 15 minutos * * para valores relativos de 15
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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>
  • Listando posts get quot;/quot; do @posts = Post.all erb :index end
  • 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>
  • Un paréntesis: helpers! helpers do def post_path(post) quot;/#{post.permalink}quot; end end
  • Mostrando un post get quot;/:permalinkquot; do |permalink| @post = Post.first(:permalink => permalink) erb :show end
  • 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>
  • 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>
  • 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
  • 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>
  • 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>
  • 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
  • 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>
  • Embelleciendo un poco... public/styles.css
  • y editar y borrar... para la próxima
  • es una interfaz entre servidores y frameworks
  • class RackIsEasy def initialize(app) @app = app end def call(env) [200, {quot;Content-Typequot; => quot;text/htmlquot;}, [quot;Okquot;]] end end
  • class AndRackIsPotentiallyDumb def initialize(app) @app = app end def call(env) @app.call(env) end end
  • HTTP app_1 Middlewares app_2 app_3 App app_4 http://mwrc2009.confreaks.com/ – Jon Crosby
  • para qué sirve eso?
  • 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
  • Sinatra is on crack
  • última cosa
  • prometo que ya me voy
  • m/
  • 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
  • Hard Metal Frank Sinatra Sinatra on Rails
  • 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;
  • Preguntas? Questions? http://github.com/foca/sinatra-example-blog contacto@nicolassanguinetti.info