rails metal, rack, and sinatra

57
Adam Wiggins Railsconf 2009 Rails Metal, Rack, and Sinatra

Upload: adam-wiggins

Post on 18-May-2015

12.770 views

Category:

Technology


0 download

DESCRIPTION

Slides from my Railsconf 2009 talk

TRANSCRIPT

Page 1: Rails Metal, Rack, and Sinatra

Adam WigginsRailsconf 2009

Rails Metal,Rack, and Sinatra

Page 2: Rails Metal, Rack, and Sinatra

Show of hands, how many of you...

Page 3: Rails Metal, Rack, and Sinatra

metal

Page 4: Rails Metal, Rack, and Sinatra

Show of hands, how many of you...

Page 5: Rails Metal, Rack, and Sinatra
Page 6: Rails Metal, Rack, and Sinatra

“The gateway drug”

Page 7: Rails Metal, Rack, and Sinatra

“The gateway drug”*

* it’s a myth, but makes good analogy

Page 8: Rails Metal, Rack, and Sinatra

Rails Metal is a gateway

Page 9: Rails Metal, Rack, and Sinatra

Rails Metal is a gateway to theworld of Rack

Page 10: Rails Metal, Rack, and Sinatra

What can you do with Metal?

Page 11: Rails Metal, Rack, and Sinatra

Replace selected URLs for a speed boost

Page 12: Rails Metal, Rack, and Sinatra

Example: auction site

Page 13: Rails Metal, Rack, and Sinatra
Page 14: Rails Metal, Rack, and Sinatra

Example: auction site

Page 15: Rails Metal, Rack, and Sinatra

Example: auction site

on

Page 16: Rails Metal, Rack, and Sinatra
Page 17: Rails Metal, Rack, and Sinatra
Page 18: Rails Metal, Rack, and Sinatra

Majority of traffic goes to:

GET /auctions/id.xml

Page 19: Rails Metal, Rack, and Sinatra

app/controller/auctions_controller.rb

Page 20: Rails Metal, Rack, and Sinatra

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

app/metal/auctions_api.rb

Page 22: Rails Metal, Rack, and Sinatra

class AuctionsApi def self.call(env) # implementation goes here

endend

app/metal/auctions_api.rb

Page 23: Rails Metal, Rack, and Sinatra

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

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

[ 200, {"Content-Type"=>"text/plain"}, "Hello, Rack!"]

Page 26: Rails Metal, Rack, and Sinatra

[ 200, {"Content-Type"=>"text/plain"}, "Hello, Rack!"]

Page 27: Rails Metal, Rack, and Sinatra
Page 28: Rails Metal, Rack, and Sinatra

http://www.slideshare.net/adamwiggins/ruby-isnt-just-about-rails-presentation

Page 29: Rails Metal, Rack, and Sinatra

An explosion of Ruby projects in the past 2 years

Page 30: Rails Metal, Rack, and Sinatra

Web LayerActionControllerMerbSinatra

ORMActiveRecordDataMapperSequel

Tests/SpecsTest::UnitRSpecShoulda

TemplatingErbHamlErubis

HTTP ClientActiveResourceRestClientHTTParty

Web ServerMongrelThinEbb

Page 31: Rails Metal, Rack, and Sinatra

Web LayerActionController

ORMActiveRecord

Tests/SpecsTest::Unit

TemplatingErb

HTTP ClientActiveResource

Web ServerMongrel

Page 32: Rails Metal, Rack, and Sinatra

Web LayerActionController

ORMActiveRecord

Tests/SpecsTest::Unit

TemplatingErb

HTTP ClientActiveResource

Web ServerMongrel

Page 33: Rails Metal, Rack, and Sinatra

Web LayerActionControllerMerbSinatra

ORMActiveRecordDataMapperSequel

Tests/SpecsTest::UnitRSpecShoulda

TemplatingErbHamlErubis

HTTP ClientActiveResourceRestClientHTTParty

Web ServerMongrelThinEbb

Page 34: Rails Metal, Rack, and Sinatra

Web LayerActionControllerMerbSinatra

ORMActiveRecordDataMapperSequel

Tests/SpecsTest::UnitRSpecShoulda

TemplatingErbHamlErubis

HTTP ClientActiveResourceRestClientHTTParty

Web ServerMongrelThinEbb

Page 35: Rails Metal, Rack, and Sinatra

Web LayerActionControllerMerbSinatra

ORMActiveRecordDataMapperSequel

Tests/SpecsTest::UnitRSpecShoulda

TemplatingErbHamlErubis

HTTP ClientActiveResourceRestClientHTTParty

Web ServerMongrelThinEbb

Page 36: Rails Metal, Rack, and Sinatra
Page 37: Rails Metal, Rack, and Sinatra
Page 38: Rails Metal, Rack, and Sinatra

The world of Rack is now within reach from Rails

Page 39: Rails Metal, Rack, and Sinatra

The classy microframework for Ruby

http://sinatrarb.com

Sinatra

Page 40: Rails Metal, Rack, and Sinatra

require 'rubygems'require 'sinatra'

get '/hello' do "Hello, whirled"end

Page 41: Rails Metal, Rack, and Sinatra

$ 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

$ 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

A minimalist paradise

Page 44: Rails Metal, Rack, and Sinatra

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

Sinatra in your Rails app?

Page 46: Rails Metal, Rack, and Sinatra

Replace selected URLs for a speed boost

Page 47: Rails Metal, Rack, and Sinatra

Replace selected URLs for a speed boost

Page 48: Rails Metal, Rack, and Sinatra

Replace selected URLs with Sinatra

Page 49: Rails Metal, Rack, and Sinatra

app/metal/articles.rb

Page 50: Rails Metal, Rack, and Sinatra

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

Back to the auction example

Page 52: Rails Metal, Rack, and Sinatra

Back to the auction example

Page 53: Rails Metal, Rack, and Sinatra

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

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

get '/auctions/:id.xml' Auction.find(params[:id]).to_xmlend

Sinatra

Page 56: Rails Metal, Rack, and Sinatra

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

The End.

http://adam.blog.heroku.com

Adam WigginsRailsconf 2009

http://railscasts.com/episodes/150-rails-metal

http://sinatrarb.comhttp://rack.rubyforge.org