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.
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
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
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>
46. 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
51. 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
53. 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;