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