rails metal, rack, and sinatra
DESCRIPTION
Slides from my Railsconf 2009 talkTRANSCRIPT
![Page 1: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/1.jpg)
Adam WigginsRailsconf 2009
Rails Metal,Rack, and Sinatra
![Page 2: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/2.jpg)
Show of hands, how many of you...
![Page 3: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/3.jpg)
metal
![Page 4: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/4.jpg)
Show of hands, how many of you...
![Page 5: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/5.jpg)
![Page 6: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/6.jpg)
“The gateway drug”
![Page 7: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/7.jpg)
“The gateway drug”*
* it’s a myth, but makes good analogy
![Page 8: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/8.jpg)
Rails Metal is a gateway
![Page 9: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/9.jpg)
Rails Metal is a gateway to theworld of Rack
![Page 10: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/10.jpg)
What can you do with Metal?
![Page 11: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/11.jpg)
Replace selected URLs for a speed boost
![Page 12: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/12.jpg)
Example: auction site
![Page 13: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/13.jpg)
![Page 14: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/14.jpg)
Example: auction site
![Page 15: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/15.jpg)
Example: auction site
on
![Page 16: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/16.jpg)
![Page 17: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/17.jpg)
![Page 18: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/18.jpg)
Majority of traffic goes to:
GET /auctions/id.xml
![Page 19: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/19.jpg)
app/controller/auctions_controller.rb
![Page 20: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/20.jpg)
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
![Page 21: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/21.jpg)
app/metal/auctions_api.rb
![Page 22: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/22.jpg)
class AuctionsApi def self.call(env) # implementation goes here
endend
app/metal/auctions_api.rb
![Page 23: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/23.jpg)
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
![Page 24: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/24.jpg)
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
![Page 25: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/25.jpg)
[ 200, {"Content-Type"=>"text/plain"}, "Hello, Rack!"]
![Page 26: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/26.jpg)
[ 200, {"Content-Type"=>"text/plain"}, "Hello, Rack!"]
![Page 27: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/27.jpg)
![Page 28: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/28.jpg)
http://www.slideshare.net/adamwiggins/ruby-isnt-just-about-rails-presentation
![Page 29: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/29.jpg)
An explosion of Ruby projects in the past 2 years
![Page 30: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/30.jpg)
Web LayerActionControllerMerbSinatra
ORMActiveRecordDataMapperSequel
Tests/SpecsTest::UnitRSpecShoulda
TemplatingErbHamlErubis
HTTP ClientActiveResourceRestClientHTTParty
Web ServerMongrelThinEbb
![Page 31: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/31.jpg)
Web LayerActionController
ORMActiveRecord
Tests/SpecsTest::Unit
TemplatingErb
HTTP ClientActiveResource
Web ServerMongrel
![Page 32: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/32.jpg)
Web LayerActionController
ORMActiveRecord
Tests/SpecsTest::Unit
TemplatingErb
HTTP ClientActiveResource
Web ServerMongrel
![Page 33: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/33.jpg)
Web LayerActionControllerMerbSinatra
ORMActiveRecordDataMapperSequel
Tests/SpecsTest::UnitRSpecShoulda
TemplatingErbHamlErubis
HTTP ClientActiveResourceRestClientHTTParty
Web ServerMongrelThinEbb
![Page 34: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/34.jpg)
Web LayerActionControllerMerbSinatra
ORMActiveRecordDataMapperSequel
Tests/SpecsTest::UnitRSpecShoulda
TemplatingErbHamlErubis
HTTP ClientActiveResourceRestClientHTTParty
Web ServerMongrelThinEbb
![Page 35: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/35.jpg)
Web LayerActionControllerMerbSinatra
ORMActiveRecordDataMapperSequel
Tests/SpecsTest::UnitRSpecShoulda
TemplatingErbHamlErubis
HTTP ClientActiveResourceRestClientHTTParty
Web ServerMongrelThinEbb
![Page 36: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/36.jpg)
![Page 37: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/37.jpg)
![Page 38: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/38.jpg)
The world of Rack is now within reach from Rails
![Page 39: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/39.jpg)
The classy microframework for Ruby
http://sinatrarb.com
Sinatra
![Page 40: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/40.jpg)
require 'rubygems'require 'sinatra'
get '/hello' do "Hello, whirled"end
![Page 41: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/41.jpg)
$ 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
![Page 42: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/42.jpg)
$ 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
![Page 43: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/43.jpg)
A minimalist paradise
![Page 44: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/44.jpg)
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
![Page 45: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/45.jpg)
Sinatra in your Rails app?
![Page 46: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/46.jpg)
Replace selected URLs for a speed boost
![Page 47: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/47.jpg)
Replace selected URLs for a speed boost
![Page 48: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/48.jpg)
Replace selected URLs with Sinatra
![Page 49: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/49.jpg)
app/metal/articles.rb
![Page 50: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/50.jpg)
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
![Page 51: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/51.jpg)
Back to the auction example
![Page 52: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/52.jpg)
Back to the auction example
![Page 53: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/53.jpg)
class AuctionsController < ApplicationController def show @auction = Auction.find(params[:id])
respond_to do |format| format.html format.xml { render :xml => @auction } end endend
ActionController
![Page 54: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/54.jpg)
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
![Page 55: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/55.jpg)
get '/auctions/:id.xml' Auction.find(params[:id]).to_xmlend
Sinatra
![Page 56: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/56.jpg)
Sinatra
Now that’s what I call
minimalist.
get '/auctions/:id.xml' Auction.find(params[:id]).to_xmlend
![Page 57: Rails Metal, Rack, and Sinatra](https://reader033.vdocuments.us/reader033/viewer/2022052523/5559321fd8b42a543d8b4978/html5/thumbnails/57.jpg)
The End.
http://adam.blog.heroku.com
Adam WigginsRailsconf 2009
http://railscasts.com/episodes/150-rails-metal
http://sinatrarb.comhttp://rack.rubyforge.org