rails metal, rack, and sinatra
DESCRIPTION
Slides from my Railsconf 2009 talkTRANSCRIPT
Adam WigginsRailsconf 2009
Rails Metal,Rack, and Sinatra
Show of hands, how many of you...
metal
Show of hands, how many of you...
“The gateway drug”
“The gateway drug”*
* it’s a myth, but makes good analogy
Rails Metal is a gateway
Rails Metal is a gateway to theworld of Rack
What can you do with Metal?
Replace selected URLs for a speed boost
Example: auction site
Example: auction site
Example: auction site
on
Majority of traffic goes to:
GET /auctions/id.xml
app/controller/auctions_controller.rb
class AuctionsController < ApplicationController def show @auction = Auction.find(params[:id])
respond_to do |format| format.html format.xml { render :xml => @auction } end endend
app/controller/auctions_controller.rb
app/metal/auctions_api.rb
class AuctionsApi def self.call(env) # implementation goes here
endend
app/metal/auctions_api.rb
class AuctionsApi def self.call(env) url_pattern = %r{/auctions/(\d+).xml}
if m = env['PATH_INFO'].match(url_pattern) # render the auction api
else # pass (do nothing) end endend
app/metal/auctions_api.rb
class AuctionsApi def self.call(env) url_pattern = %r{/auctions/(\d+).xml}
if m = env['PATH_INFO'].match(url_pattern) auction = Auction.find(m[1]) [ 200, {"Content-Type" => "text/xml"}, auction.to_xml ] else [ 404, {}, '' ] end endend
app/metal/auctions_api.rb
[ 200, {"Content-Type"=>"text/plain"}, "Hello, Rack!"]
[ 200, {"Content-Type"=>"text/plain"}, "Hello, Rack!"]
http://www.slideshare.net/adamwiggins/ruby-isnt-just-about-rails-presentation
An explosion of Ruby projects in the past 2 years
Web LayerActionControllerMerbSinatra
ORMActiveRecordDataMapperSequel
Tests/SpecsTest::UnitRSpecShoulda
TemplatingErbHamlErubis
HTTP ClientActiveResourceRestClientHTTParty
Web ServerMongrelThinEbb
Web LayerActionController
ORMActiveRecord
Tests/SpecsTest::Unit
TemplatingErb
HTTP ClientActiveResource
Web ServerMongrel
Web LayerActionController
ORMActiveRecord
Tests/SpecsTest::Unit
TemplatingErb
HTTP ClientActiveResource
Web ServerMongrel
Web LayerActionControllerMerbSinatra
ORMActiveRecordDataMapperSequel
Tests/SpecsTest::UnitRSpecShoulda
TemplatingErbHamlErubis
HTTP ClientActiveResourceRestClientHTTParty
Web ServerMongrelThinEbb
Web LayerActionControllerMerbSinatra
ORMActiveRecordDataMapperSequel
Tests/SpecsTest::UnitRSpecShoulda
TemplatingErbHamlErubis
HTTP ClientActiveResourceRestClientHTTParty
Web ServerMongrelThinEbb
Web LayerActionControllerMerbSinatra
ORMActiveRecordDataMapperSequel
Tests/SpecsTest::UnitRSpecShoulda
TemplatingErbHamlErubis
HTTP ClientActiveResourceRestClientHTTParty
Web ServerMongrelThinEbb
The world of Rack is now within reach from Rails
The classy microframework for Ruby
http://sinatrarb.com
Sinatra
require 'rubygems'require 'sinatra'
get '/hello' do "Hello, whirled"end
$ ruby hello.rb== Sinatra/0.9.1.1 has taken the stage>> Thin web server (v1.0.0)>> Maximum connections set to 1024>> Listening on 0.0.0.0:4567
$ ruby hello.rb== Sinatra/0.9.1.1 has taken the stage>> Thin web server (v1.0.0)>> Maximum connections set to 1024>> Listening on 0.0.0.0:4567
$ curl http://localhost:4567/helloHello, whirled
A minimalist paradise
require 'rubygems'require 'sinatra'require 'lib/article'
post '/articles' do article = Article.create! params redirect "/articles/#{article.id}"end
get '/articles/:id' do @article = Article.find(params[:id]) erb :articleend
Sinatra in your Rails app?
Replace selected URLs for a speed boost
Replace selected URLs for a speed boost
Replace selected URLs with Sinatra
app/metal/articles.rb
require 'sinatra/base'
class Articles < Sinatra::Base post '/articles' do article = Article.create! params redirect "/articles/#{article.id}" end
get '/articles/:id' do @article = Article.find(params[:id]) erb :article endend
app/metal/articles.rb
Back to the auction example
Back to the auction example
class AuctionsController < ApplicationController def show @auction = Auction.find(params[:id])
respond_to do |format| format.html format.xml { render :xml => @auction } end endend
ActionController
class AuctionsApi def self.call(env) url_pattern = /^\/auctions\/(\d+).xml$/
if m = env['PATH_INFO'].match(url_pattern) auction = Auction.find(m[1]) [ 200, {"Content-Type" => "text/xml"}, auction.to_xml ] else [ 404, {}, '' ] end endend
Pure Rack
get '/auctions/:id.xml' Auction.find(params[:id]).to_xmlend
Sinatra
Sinatra
Now that’s what I call
minimalist.
get '/auctions/:id.xml' Auction.find(params[:id]).to_xmlend
The End.
http://adam.blog.heroku.com
Adam WigginsRailsconf 2009
http://railscasts.com/episodes/150-rails-metal
http://sinatrarb.comhttp://rack.rubyforge.org