23. 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)
# render the auction api
else
# pass (do nothing)
end
end
end
24. 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)
auction = Auction.find(m[1])
[ 200, {quot;Content-Typequot; => quot;text/xmlquot;},
auction.to_xml ]
else
[ 404, {}, '' ]
end
end
end
41. $ 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
42. $ 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/hello
Hello, whirled
44. require 'rubygems'
require 'sinatra'
require 'lib/article'
post '/articles' do
article = Article.create! params
redirect quot;/articles/#{article.id}quot;
end
get '/articles/:id' do
@article = Article.find(params[:id])
erb :article
end
50. app/metal/articles.rb
require 'sinatra/base'
class Articles < Sinatra::Base
post '/articles' do
article = Article.create! params
redirect quot;/articles/#{article.id}quot;
end
get '/articles/:id' do
@article = Article.find(params[:id])
erb :article
end
end
53. ActionController
class AuctionsController < ApplicationController
def show
@auction = Auction.find(params[:id])
respond_to do |format|
format.html
format.xml { render :xml => @auction }
end
end
end
54. Pure Rack
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, {quot;Content-Typequot; => quot;text/xmlquot;},
auction.to_xml ]
else
[ 404, {}, '' ]
end
end
end