Railsconf: Rails Metal, Rack and Sinatra – Adam Wiggings (Heroku)

May 7, 2009 Pivotal Labs

Rails Metal

  • Rails Metal is a gateway to the exciting and possibly dangerous world of Rack.
  • Replace selected URLs for a speed boost.
  • Bypasses the Rails router. You have to do your own routing.
  • All Metal endpoints are tried before Rails routing.

Lets say in an alternate universe you are running EBay on Rails.
Your most active page is bots hitting your API to get auction status: GET /auctions/1234567.xml

Replace app/controllers/autctions_controller.rb

With 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)
      Auctions.find(m[1])
      # This is a Rack return value:
      [ 200, { "Content-Type" => 'text/xml" }, auction.to_xml ]
    end
  end
end

Sinatra

Sinatra is an extremely minimalist web framework. It works in the Rack framework. This is an entire Sinatra application:

require 'rubygems'
require 'sinatra'

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

Run it:

$ ruby hello.rb
# Starts up server on port 4567
$ curl http://localhost:4567/hello
Hello, whirled

Here is the alternate universe auction example with Sinatra:

class AuctionsApi < Sinatra::Application
  get '/autions/:id.xml'
    Auction.find params[:id].to_xml
  end
end

About the Author

Biography

Previous
Railsconf: Giving Rails the Big F – Surviving Facebook Integration Unscarred, Greg Borenstein
Railsconf: Giving Rails the Big F – Surviving Facebook Integration Unscarred, Greg Borenstein

The Facebook API Menagerie Facebook API has many different parts: Canvas Apps FB requests from your site...

Next
Follow-ups from my RailsConf talk
Follow-ups from my RailsConf talk

First, thanks to everyone who came - especially those who laughed at all the right spots. If I didn't get ...

×

Subscribe to our Newsletter

!
Thank you!
Error - something went wrong!