Rails Metal, Rack, and SinatraPresentation Transcript
Rails Metal,
Rack, and Sinatra
Adam Wiggins
Railsconf 2009
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 the
world 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
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
end
end
app/metal/auctions_api.rb
app/metal/auctions_api.rb
class AuctionsApi
def self.call(env)
# implementation goes here
end
end
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
end
end
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, {quot;Content-Typequot; => quot;text/xmlquot;},
auction.to_xml ]
else
[ 404, {}, '' ]
end
end
end
Tests/Specs
Web Layer
Test::Unit
ActionController
RSpec
Merb
Shoulda
Sinatra
ORM Templating
ActiveRecord Erb
DataMapper Haml
Sequel Erubis
Web Server
HTTP Client
Mongrel
ActiveResource
Thin
RestClient
Ebb
HTTParty
Tests/Specs
Web Layer
Test::Unit
ActionController
ORM Templating
ActiveRecord Erb
Web Server
HTTP Client
Mongrel
ActiveResource
Tests/Specs
Web Layer
Test::Unit
ActionController
ORM Templating
ActiveRecord Erb
Web Server
HTTP Client
Mongrel
ActiveResource
Tests/Specs
Web Layer
Test::Unit
ActionController
RSpec
Merb
Shoulda
Sinatra
ORM Templating
ActiveRecord Erb
DataMapper Haml
Sequel Erubis
Web Server
HTTP Client
Mongrel
ActiveResource
Thin
RestClient
Ebb
HTTParty
Tests/Specs
Web Layer
Test::Unit
ActionController
RSpec
Merb
Shoulda
Sinatra
ORM Templating
ActiveRecord Erb
DataMapper Haml
Sequel Erubis
Web Server
HTTP Client
Mongrel
ActiveResource
Thin
RestClient
Ebb
HTTParty
Tests/Specs
Web Layer
Test::Unit
ActionController
RSpec
Merb
Shoulda
Sinatra
ORM Templating
ActiveRecord Erb
DataMapper Haml
Sequel Erubis
Web Server
HTTP Client
Mongrel
ActiveResource
Thin
RestClient
Ebb
HTTParty
The world of Rack is
now within reach from
Rails
Sinatra
The classy
microframework for Ruby
http://sinatrarb.com
require 'rubygems'
require 'sinatra'
get '/hello' do
quot;Hello, whirledquot;
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/hello
Hello, whirled
A minimalist paradise
require 'rubygems'
require 'sinatra'
require 'lib/article'
post '/articles' do
article = Article.create! params
redirect quot;/articles/#{article.id}quot;
end
get '/articles/:id' do
@article = Article.find(params[:id])
erb :article
end
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
app/metal/articles.rb
require 'sinatra/base'
class Articles < Sinatra::Base
post '/articles' do
article = Article.create! params
redirect quot;/articles/#{article.id}quot;
end
get '/articles/:id' do
@article = Article.find(params[:id])
erb :article
end
end
Back to the auction
example
Back to the auction
example
ActionController
class AuctionsController < ApplicationController
def show
@auction = Auction.find(params[:id])
respond_to do |format|
format.html
format.xml { render :xml => @auction }
end
end
end
Pure Rack
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, {quot;Content-Typequot; => quot;text/xmlquot;},
auction.to_xml ]
else
[ 404, {}, '' ]
end
end
end
Sinatra
get '/auctions/:id.xml'
Auction.find(params[:id]).to_xml
end
Sinatra
get '/auctions/:id.xml'
Auction.find(params[:id]).to_xml
end
Now that’s what I call
minimalist.
The End.
http://railscasts.com/episodes/150-rails-metal
http://rack.rubyforge.org
http://sinatrarb.com
http://adam.blog.heroku.com
Adam Wiggins
Railsconf 2009
1–1 of 1 previous next