h1. Rails Metal, Rack, and Sinatra Adam Wiggins Raise of hands: More people know about pot than about Rails metal h2. Rails Metal is a gateway to the world of Rack h2. What can you do with Metal? h3. Replace selected URLs for a speed boost Example: Auction site (eBay on Rails) The auction show page is probably hit the most (example with Rick Astley: Greatest Hits - laughs) Most traffic goes to: GET /auctions/id.xml We can get a big benefit from replacing just that URL h4. Standard Rails AuctionsController with format.xml h4. Metal app/metal/auctions_api.rb class AuctionsApi def self.call(env) url_pattern = %r{/auctions/(\d+).xml} # Do your own routing if m = env['PATH_INFO'].match(url_pattern) # render the auction api auction = Auction.find(m[1]) [200, {"Content-Type" => "text/xml"}, auction.to_xml] # Return value from rack else # pass - do nothing [400, {}] # Return value from rack end end end h2. What is Rack? It stands between frameworks as Rails and web servers. Ruby Isn't Just about Rails (slides available in SlideShare) h3. An explosion of Ruby project in the past 2 years h4. Web Layer ActionController Merb Sinatra h4. ORM ActiveRecord Datamapper Sequal h4. Tests/Specs Test::Unit RSpec Shoulda h4. Templating Erb HAML Erubis h4. HTTP Client ActiveResource RestClient HTTParty h4. Web server Mongrel Thin Ebb h3. Rails are from Mars, Rack are from Venus But starting from Rails 2.3, Rails is now based in Rack Rails Metal is where the genious is here, it lets us tap into the great things in Rack-land This will be very uniting for the Ruby community h3. The world of Rack is now within the reach from Rails How does that happen? h2. Sinatra The classy microframework for RUby http://sinatrarb.com h3. Example require 'rubygems' require 'sinatra' get '/hello' do "Hello, whirled" end This is not a snippet, this is a full Sinatra Application h3. A minimalist paradise post 'articles' article = Article.create! params redirect "/articles/#{article.id}" end get '/articles/:id' do @article = Article.find(params[:id]) erb :article end h3. Sinatra in our Rails app? We can replace selected URLs for a speed boost - or we can replace them with Sinatra h4. Example - app/metal/articles.rb class Articles < Sinatra::Application set :environment, 'production' ... (same code as above) end You can stick this right into an existing Rails app. h3. Replacing the pure Rack example with Sinatra The AuctionApi example is very verbose But the Sintra example is minimalist: get '/auctions/:id.xml' do Auction.find params[:id].to_xml end h2. Resources http://railscasts.com/episodes/150-rais-metal rack.rubyforge.or sinatrarb.com adam.blog.heruko.com h2. Questions h3. What's the speed difference between using Rack and Sinatra for Metal? It will give you a real speed boost, but you shouldn't do it just for that