Lightweight Web
Services with Sinatra
        Gerred Dillon
     Westin Consulting, LLC
Sinatra is...
Simple.
Simple.



require ‘rubygems’
require ‘sinatra’

get ‘/’ do
 “Hello, world!”
end
Extensible.
Extensible.

require ‘rubygems’
require ‘sinatra’
require ‘active_record’

class Post < ActiveRecord::Base
end

post ‘/cre...
Testable.
Testable.



•Rack::Test
Testable.



•Rack::Test
•Cucumber-Sinatra
Testable.



•Rack::Test
•Cucumber-Sinatra
•RSpec
And most importantly...
And most importantly...




Incredibly lightweight.
Memory Allocation Compared




                             Source: Matt Aimonetti (Merbist)
Memory Allocation Compared

On a request to a simple application...




                                          Source: ...
Memory Allocation Compared

On a request to a simple application...
•Rails 2.3.x allocates 170,000 objects




           ...
Memory Allocation Compared

On a request to a simple application...
•Rails 2.3.x allocates 170,000 objects
•Rails 3 alloca...
Memory Allocation Compared

On a request to a simple application...
•Rails 2.3.x allocates 170,000 objects
•Rails 3 alloca...
Memory Allocation Compared

On a request to a simple application...
•Rails 2.3.x allocates 170,000 objects
•Rails 3 alloca...
Memory Allocation Compared

On a request to a simple application...
•Rails 2.3.x allocates 170,000 objects
•Rails 3 alloca...
Rack
Rack

ActiveModel
Rack

 ActiveModel

ActiveResource
Rack

  ActiveModel

ActiveResource

ActionController
Rack

  ActiveModel

ActiveResource

ActionController

  ActionView
Rack          ActionMailer

  ActiveModel

ActiveResource

ActionController

  ActionView
Rack          ActionMailer

  ActiveModel      ActiveSupport

ActiveResource

ActionController

  ActionView
Rack          ActionMailer

  ActiveModel      ActiveSupport

ActiveResource        Logger

ActionController

  ActionView
Rack          ActionMailer

  ActiveModel      ActiveSupport

ActiveResource        Logger

ActionController    Rails Core...
Rack          ActionMailer

  ActiveModel      ActiveSupport

ActiveResource        Logger

ActionController    Rails Core...
Rack
Rack


Sinatra
Rack


Sinatra


          Application
Rack


Sinatra                 ???


          Application
In the wild...
In the wild...
Scanty
In the wild...
Scanty




                  Haze
In the wild...
Scanty




                    Haze
     Amnesia
In the wild...
Scanty
                            PasteBooth



                    Haze
     Amnesia
In the wild...
Scanty
                            PasteBooth

     UploadBooth

                    Haze
     Amnesia
In the wild...
Scanty
                            PasteBooth

     UploadBooth

                    Haze
     Amnesia
    ...
In the wild...
   Scanty
                                PasteBooth

         UploadBooth

                        Haze
Gi...
In the wild...
   Scanty
                                PasteBooth

         UploadBooth

                        Haze
Gi...
In the wild...
   Scanty
                                PasteBooth

         UploadBooth
                                ...
What about...
awsmimg


A basic image uploader in 59 lines of code.
    Built with BDD concerns in mind.
Deploying Sinatra
Deploying Sinatra

   Passenger
Deploying Sinatra

   Passenger
   Capinatra
Deploying Sinatra

   Passenger
   Capinatra
    Heroku
Deploying Sinatra

           Passenger
           Capinatra
            Heroku


You need a rackup (config.ru) file!
Actually using it...
Actually using it...

•Rails and Sinatra
Actually using it...

•Rails and Sinatra
•ActiveResource and RESTful APIs
Actually using it...

•Rails and Sinatra
•ActiveResource and RESTful APIs
•CouchDB Middleware
Actually using it...

•Rails and Sinatra
•ActiveResource and RESTful APIs
•CouchDB Middleware
•Small, atomic applications
Using Sinatra as a lightweight web service
Using Sinatra as a lightweight web service
Upcoming SlideShare
Loading in …5
×

Using Sinatra as a lightweight web service

3,601 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,601
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide















































  • Using Sinatra as a lightweight web service

    1. 1. Lightweight Web Services with Sinatra Gerred Dillon Westin Consulting, LLC
    2. 2. Sinatra is...
    3. 3. Simple.
    4. 4. Simple. require ‘rubygems’ require ‘sinatra’ get ‘/’ do “Hello, world!” end
    5. 5. Extensible.
    6. 6. Extensible. require ‘rubygems’ require ‘sinatra’ require ‘active_record’ class Post < ActiveRecord::Base end post ‘/create’ do Post.create(:body => “Hello, world!”) end
    7. 7. Testable.
    8. 8. Testable. •Rack::Test
    9. 9. Testable. •Rack::Test •Cucumber-Sinatra
    10. 10. Testable. •Rack::Test •Cucumber-Sinatra •RSpec
    11. 11. And most importantly...
    12. 12. And most importantly... Incredibly lightweight.
    13. 13. Memory Allocation Compared Source: Matt Aimonetti (Merbist)
    14. 14. Memory Allocation Compared On a request to a simple application... Source: Matt Aimonetti (Merbist)
    15. 15. Memory Allocation Compared On a request to a simple application... •Rails 2.3.x allocates 170,000 objects Source: Matt Aimonetti (Merbist)
    16. 16. Memory Allocation Compared On a request to a simple application... •Rails 2.3.x allocates 170,000 objects •Rails 3 allocates 8,500 objects Source: Matt Aimonetti (Merbist)
    17. 17. Memory Allocation Compared On a request to a simple application... •Rails 2.3.x allocates 170,000 objects •Rails 3 allocates 8,500 objects •Sinatra allocates 151 objects Source: Matt Aimonetti (Merbist)
    18. 18. Memory Allocation Compared On a request to a simple application... •Rails 2.3.x allocates 170,000 objects •Rails 3 allocates 8,500 objects •Sinatra allocates 151 objects •Rack allocates 7 objects Source: Matt Aimonetti (Merbist)
    19. 19. Memory Allocation Compared On a request to a simple application... •Rails 2.3.x allocates 170,000 objects •Rails 3 allocates 8,500 objects •Sinatra allocates 151 objects •Rack allocates 7 objects Application performance directly corresponds to Ruby’s garbage collector! The garbage collector will run every ~6 requests on Rails 3. Source: Matt Aimonetti (Merbist)
    20. 20. Rack
    21. 21. Rack ActiveModel
    22. 22. Rack ActiveModel ActiveResource
    23. 23. Rack ActiveModel ActiveResource ActionController
    24. 24. Rack ActiveModel ActiveResource ActionController ActionView
    25. 25. Rack ActionMailer ActiveModel ActiveResource ActionController ActionView
    26. 26. Rack ActionMailer ActiveModel ActiveSupport ActiveResource ActionController ActionView
    27. 27. Rack ActionMailer ActiveModel ActiveSupport ActiveResource Logger ActionController ActionView
    28. 28. Rack ActionMailer ActiveModel ActiveSupport ActiveResource Logger ActionController Rails Core ActionView
    29. 29. Rack ActionMailer ActiveModel ActiveSupport ActiveResource Logger ActionController Rails Core ActionView Application
    30. 30. Rack
    31. 31. Rack Sinatra
    32. 32. Rack Sinatra Application
    33. 33. Rack Sinatra ??? Application
    34. 34. In the wild...
    35. 35. In the wild... Scanty
    36. 36. In the wild... Scanty Haze
    37. 37. In the wild... Scanty Haze Amnesia
    38. 38. In the wild... Scanty PasteBooth Haze Amnesia
    39. 39. In the wild... Scanty PasteBooth UploadBooth Haze Amnesia
    40. 40. In the wild... Scanty PasteBooth UploadBooth Haze Amnesia Chargd
    41. 41. In the wild... Scanty PasteBooth UploadBooth Haze GitHub Amnesia Chargd
    42. 42. In the wild... Scanty PasteBooth UploadBooth Haze GitHub Amnesia Chargd Webnailer
    43. 43. In the wild... Scanty PasteBooth UploadBooth Integrity Haze GitHub Amnesia Chargd Webnailer
    44. 44. What about...
    45. 45. awsmimg A basic image uploader in 59 lines of code. Built with BDD concerns in mind.
    46. 46. Deploying Sinatra
    47. 47. Deploying Sinatra Passenger
    48. 48. Deploying Sinatra Passenger Capinatra
    49. 49. Deploying Sinatra Passenger Capinatra Heroku
    50. 50. Deploying Sinatra Passenger Capinatra Heroku You need a rackup (config.ru) file!
    51. 51. Actually using it...
    52. 52. Actually using it... •Rails and Sinatra
    53. 53. Actually using it... •Rails and Sinatra •ActiveResource and RESTful APIs
    54. 54. Actually using it... •Rails and Sinatra •ActiveResource and RESTful APIs •CouchDB Middleware
    55. 55. Actually using it... •Rails and Sinatra •ActiveResource and RESTful APIs •CouchDB Middleware •Small, atomic applications

    ×