Rails Metal, Rack, and Sinatra

15,952 views

Published on

Slides from my Railsconf 2009 talk

Published in: Technology, Business
1 Comment
44 Likes
Statistics
Notes
  • http://www.fioricetsupply.com is the place to resolve the price problem. Buy now and make a deal for you.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
15,952
On SlideShare
0
From Embeds
0
Number of Embeds
1,054
Actions
Shares
0
Downloads
247
Comments
1
Likes
44
Embeds 0
No embeds

No notes for slide

Rails Metal, Rack, and Sinatra

  1. Rails Metal, Rack, and Sinatra Adam Wiggins Railsconf 2009
  2. Show of hands, how many of you...
  3. metal
  4. Show of hands, how many of you...
  5. “The gateway drug”
  6. “The gateway drug”* * it’s a myth, but makes good analogy
  7. Rails Metal is a gateway
  8. Rails Metal is a gateway to the world of Rack
  9. What can you do with Metal?
  10. Replace selected URLs for a speed boost
  11. Example: auction site
  12. Example: auction site
  13. Example: auction site on
  14. Majority of traffic goes to: GET /auctions/id.xml
  15. app/controller/auctions_controller.rb
  16. app/controller/auctions_controller.rb class AuctionsController < ApplicationController def show @auction = Auction.find(params[:id]) respond_to do |format| format.html format.xml { render :xml => @auction } end end end
  17. app/metal/auctions_api.rb
  18. app/metal/auctions_api.rb class AuctionsApi def self.call(env) # implementation goes here end end
  19. 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
  20. 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
  21. [ 200, {quot;Content-Typequot;=>quot;text/plainquot;}, quot;Hello, Rack!quot; ]
  22. [ 200, {quot;Content-Typequot;=>quot;text/plainquot;}, quot;Hello, Rack!quot; ]
  23. http://www.slideshare.net/adamwiggins/ ruby-isnt-just-about-rails-presentation
  24. An explosion of Ruby projects in the past 2 years
  25. Tests/Specs Web Layer Test::Unit ActionController RSpec Merb Shoulda Sinatra ORM Templating ActiveRecord Erb DataMapper Haml Sequel Erubis Web Server HTTP Client Mongrel ActiveResource Thin RestClient Ebb HTTParty
  26. Tests/Specs Web Layer Test::Unit ActionController ORM Templating ActiveRecord Erb Web Server HTTP Client Mongrel ActiveResource
  27. Tests/Specs Web Layer Test::Unit ActionController ORM Templating ActiveRecord Erb Web Server HTTP Client Mongrel ActiveResource
  28. Tests/Specs Web Layer Test::Unit ActionController RSpec Merb Shoulda Sinatra ORM Templating ActiveRecord Erb DataMapper Haml Sequel Erubis Web Server HTTP Client Mongrel ActiveResource Thin RestClient Ebb HTTParty
  29. Tests/Specs Web Layer Test::Unit ActionController RSpec Merb Shoulda Sinatra ORM Templating ActiveRecord Erb DataMapper Haml Sequel Erubis Web Server HTTP Client Mongrel ActiveResource Thin RestClient Ebb HTTParty
  30. Tests/Specs Web Layer Test::Unit ActionController RSpec Merb Shoulda Sinatra ORM Templating ActiveRecord Erb DataMapper Haml Sequel Erubis Web Server HTTP Client Mongrel ActiveResource Thin RestClient Ebb HTTParty
  31. The world of Rack is now within reach from Rails
  32. Sinatra The classy microframework for Ruby http://sinatrarb.com
  33. require 'rubygems' require 'sinatra' get '/hello' do quot;Hello, whirledquot; end
  34. $ 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
  35. $ 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
  36. A minimalist paradise
  37. 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
  38. Sinatra in your Rails app?
  39. Replace selected URLs for a speed boost
  40. Replace selected URLs for a speed boost
  41. Replace selected URLs with Sinatra
  42. app/metal/articles.rb
  43. 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
  44. Back to the auction example
  45. Back to the auction example
  46. 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
  47. 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
  48. Sinatra get '/auctions/:id.xml' Auction.find(params[:id]).to_xml end
  49. Sinatra get '/auctions/:id.xml' Auction.find(params[:id]).to_xml end Now that’s what I call minimalist.
  50. The End. http://railscasts.com/episodes/150-rails-metal http://rack.rubyforge.org http://sinatrarb.com http://adam.blog.heroku.com Adam Wiggins Railsconf 2009

×