SlideShare a Scribd company logo
1 of 32
Rack is Spectacular
Learning Rack by making a cheap Rails knockoff
WARNING
CODE HEAVY
  HTTP
  Ruby
  Rails
WARNING
   CODE HEAVY

 So get the code
http://bit.ly/spectac

        c9c340bb
WARNING
 CODE HEAVY
 NOT READY
    FOR
PRODUCTION
    c9c340bb
Rack

• Standard Ruby-HTTP interface
• Write (framework) once
• Run anywhere (that is Rack-compatible)
• Rails, Sinatra, Camping
• Mongrel, Passenger, Unicorn
A Rack Application
                                  config.ru
  use Rack::ContentLength
   
  run(proc do
    [ 200, { 'Content-type' => 'text/plain'}, 'hello world']
  end)



Response Code
                         Response Headers
                                                               Response Body

                                 c9c340bb
What is Rails

• ActionPack
 • Router - maps URLs to code
 • Controller - environment for code
 • View - generating response body
• ActiveRecord - database access
The Essential Rails

• ActionPack
 • Router - maps URLs to code
 • Controller - environment for code
 • View - generating response body
• ActiveRecord - database access
config.ru
              require 'lib/spectacular.rb'
              use Rack::ContentLength
               
              run(Spectacular::App.new)


                          lib/spectacular/app.rb
module Spectacular
  class App
    def call(environment)
      [ 200, { 'Content-type' => 'text/plain'}, 'hello world']
    end
  end
end


                                   0fad6a02
lib/spectacular/app.rb

module Spectacular
  class App
…
    def call(environment)
      method = environment['REQUEST_METHOD']
      path = environment['PATH_INFO']
      @dispatcher.dispatch method, path, environment
    rescue => exception
      error_backtrace exception, environment
    end
 
    private
    def error_backtrace(exception, environment)
      exception_info = “some html” 
      [500, { 'Content-type' => 'text/html'}, exception_info]
    end
…
  end
end




                             2a0d1445
lib/spectacular/app.rb

module Spectacular
  class App
…
    def call(environment)
              breaks; no dispatcher yet
      method = environment['REQUEST_METHOD']
      path = environment['PATH_INFO']
      @dispatcher.dispatch method, path, environment
    rescue => exception
      error_backtrace exception, environment
    end
 
    private
    def error_backtrace(exception, environment)
      exception_info = “some html” 
      [500, { 'Content-type' => 'text/html'}, exception_info]
    end
…
  end
end




                             2a0d1445
Dispatch & Routing are
the most complicated
 parts of Rails
              [citation needed]
But That’s Okay

• We’re not building Rails
• It’s okay if we Fail to Scale
• Just use an array to map paths to
  controllers and actions
lib/spectacular/dispatcher.rb
    def get_route(path)
      @routes.detect{ |r| path === r[0] }
    end
…
    class RoutePen
      def route(path, controller, method)
        @routes << [path, controller, method]
      end
    end




                      9cfb9609
lib/spectacular/controller.rb
    def response_for(method)
      @method = method
      send @method
      [response_code, headers, body]
    end
…
    def template_file
      File.join(APP_DIR, 'views', controller_name, "#{@method}.html.haml")
    end
…
    def body
      return @body if defined? @body
      template = File.read template_file
      engine = Haml::Engine.new template
      @body = engine.render
    end




                                           ce2f00a3
app/controllers/hello_controller.rb
class HelloController < Spectacular::Controller
  def index
  end
end




       app/views/hello/index.html.haml
                       hello world




                       ce2f00a3
Wildcards
          lib/spectacular/dispatcher.rb

def get_route(path)
 @routes.detect{ |r| r[0] === path }
end




                   48b08205
===
0 :> /abc/ === 'abc'
   # => true
0 :> String === 'abc'
   # => true
0 :> /too+t/ === 'toooooooooot'
   # => true
A Wildcard Route

                app/routes.rb
route /too+t/, TootController, :toot




                  48b08205
Using the Path
    app/controllers/toot_controller.rb
class TootController < Spectacular::Controller
 def toot
   @toot = @path.gsub('/','')
 end
end



       app/views/toot/toot.html.haml

             %h1=@toot
                     48b08205
404


• When we can’t figure out how to process a
  request, that should be a “404”
• We can simply make a wildcard route
app/routes.rb
route //, ErrorController, :not_found



       app/controllers/error_controller.rb
   class ErrorController < Spectacular::Controller
    def not_found
      @response_code = 404
    end
   end




                         2a2653b2
What’s Left

• Only renders HAML
• No layouts
• Can’t easily render a different view
• No query parameters
• No POST
thanks

• Bryce Kerley; mercenary developer
• bkerley@brycekerley.net
• http://twitter.com/bonzoesc
• Code on Github: http://bit.ly/spectac

More Related Content

What's hot

What's hot (20)

Web service with Laravel
Web service with LaravelWeb service with Laravel
Web service with Laravel
 
Ember - introduction
Ember - introductionEmber - introduction
Ember - introduction
 
Laravel - Website Development in Php Framework.
Laravel - Website Development in Php Framework.Laravel - Website Development in Php Framework.
Laravel - Website Development in Php Framework.
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
 
Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1
 
Getting to know Laravel 5
Getting to know Laravel 5Getting to know Laravel 5
Getting to know Laravel 5
 
Intro to Laravel
Intro to LaravelIntro to Laravel
Intro to Laravel
 
Rails Engine :: modularize you app
Rails Engine :: modularize you appRails Engine :: modularize you app
Rails Engine :: modularize you app
 
O que há de novo no Rails 3 - Ruby on Rails no Mundo Real - 23may2010
O que há de novo no Rails 3 - Ruby on Rails no Mundo Real - 23may2010O que há de novo no Rails 3 - Ruby on Rails no Mundo Real - 23may2010
O que há de novo no Rails 3 - Ruby on Rails no Mundo Real - 23may2010
 
Cocoa on-rails-3rd
Cocoa on-rails-3rdCocoa on-rails-3rd
Cocoa on-rails-3rd
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
Web services with laravel
Web services with laravelWeb services with laravel
Web services with laravel
 
OpenTox API introductory presentation
OpenTox API introductory presentationOpenTox API introductory presentation
OpenTox API introductory presentation
 
Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)
 
Rails Engines as a way to Micro services
Rails Engines as a way to Micro servicesRails Engines as a way to Micro services
Rails Engines as a way to Micro services
 
Laravel for Web Artisans
Laravel for Web ArtisansLaravel for Web Artisans
Laravel for Web Artisans
 
Laravel Beginners Tutorial 2
Laravel Beginners Tutorial 2Laravel Beginners Tutorial 2
Laravel Beginners Tutorial 2
 
Workshop Laravel 5.2
Workshop Laravel 5.2Workshop Laravel 5.2
Workshop Laravel 5.2
 

Viewers also liked (7)

Sinatra: the Classiest of Prototypes
Sinatra: the Classiest of PrototypesSinatra: the Classiest of Prototypes
Sinatra: the Classiest of Prototypes
 
Analysis Of Opening Techniques 1
Analysis Of Opening Techniques 1Analysis Of Opening Techniques 1
Analysis Of Opening Techniques 1
 
Triskelion and Crapshoot
Triskelion and CrapshootTriskelion and Crapshoot
Triskelion and Crapshoot
 
Test and taste your aiming
Test and taste your aimingTest and taste your aiming
Test and taste your aiming
 
Analysis Of Opening Techniques The Shining
Analysis Of Opening Techniques  The ShiningAnalysis Of Opening Techniques  The Shining
Analysis Of Opening Techniques The Shining
 
Are you paying atenttion
Are you paying atenttionAre you paying atenttion
Are you paying atenttion
 
THE 9 GOLDEN-RULES: Climbing the ladder of problem-solving mediation
THE 9 GOLDEN-RULES: Climbing the ladder of problem-solving mediation THE 9 GOLDEN-RULES: Climbing the ladder of problem-solving mediation
THE 9 GOLDEN-RULES: Climbing the ladder of problem-solving mediation
 

Similar to Rack is Spectacular

Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3
Henry S
 

Similar to Rack is Spectacular (20)

Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Rack
 
Speedy TDD with Rails
Speedy TDD with RailsSpeedy TDD with Rails
Speedy TDD with Rails
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortega
 
Intro to Rack
Intro to RackIntro to Rack
Intro to Rack
 
Wider than rails
Wider than railsWider than rails
Wider than rails
 
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
 
Sinatra for REST services
Sinatra for REST servicesSinatra for REST services
Sinatra for REST services
 
Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
Rails Performance
Rails PerformanceRails Performance
Rails Performance
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
 
Design Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonDesign Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron Patterson
 
Rails 2.3 and Rack - NHRuby Feb 2009
Rails 2.3 and Rack - NHRuby Feb 2009Rails 2.3 and Rack - NHRuby Feb 2009
Rails 2.3 and Rack - NHRuby Feb 2009
 
Using ArcGIS Server with Ruby on Rails
Using ArcGIS Server with Ruby on RailsUsing ArcGIS Server with Ruby on Rails
Using ArcGIS Server with Ruby on Rails
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the Cloud
 
Umleitung: a tiny mochiweb/CouchDB app
Umleitung: a tiny mochiweb/CouchDB appUmleitung: a tiny mochiweb/CouchDB app
Umleitung: a tiny mochiweb/CouchDB app
 
Rack
RackRack
Rack
 
Introduction To Ruby On Rails
Introduction To Ruby On RailsIntroduction To Ruby On Rails
Introduction To Ruby On Rails
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Rack is Spectacular

  • 1. Rack is Spectacular Learning Rack by making a cheap Rails knockoff
  • 2. WARNING CODE HEAVY HTTP Ruby Rails
  • 3. WARNING CODE HEAVY So get the code http://bit.ly/spectac c9c340bb
  • 4. WARNING CODE HEAVY NOT READY FOR PRODUCTION c9c340bb
  • 5. Rack • Standard Ruby-HTTP interface • Write (framework) once • Run anywhere (that is Rack-compatible) • Rails, Sinatra, Camping • Mongrel, Passenger, Unicorn
  • 6. A Rack Application config.ru use Rack::ContentLength   run(proc do   [ 200, { 'Content-type' => 'text/plain'}, 'hello world'] end) Response Code Response Headers Response Body c9c340bb
  • 7.
  • 8. What is Rails • ActionPack • Router - maps URLs to code • Controller - environment for code • View - generating response body • ActiveRecord - database access
  • 9. The Essential Rails • ActionPack • Router - maps URLs to code • Controller - environment for code • View - generating response body • ActiveRecord - database access
  • 10. config.ru require 'lib/spectacular.rb' use Rack::ContentLength   run(Spectacular::App.new) lib/spectacular/app.rb module Spectacular   class App     def call(environment)       [ 200, { 'Content-type' => 'text/plain'}, 'hello world']     end   end end 0fad6a02
  • 11.
  • 12. lib/spectacular/app.rb module Spectacular   class App …     def call(environment)       method = environment['REQUEST_METHOD']       path = environment['PATH_INFO']       @dispatcher.dispatch method, path, environment     rescue => exception       error_backtrace exception, environment     end       private     def error_backtrace(exception, environment)       exception_info = “some html”        [500, { 'Content-type' => 'text/html'}, exception_info]     end …   end end 2a0d1445
  • 13. lib/spectacular/app.rb module Spectacular   class App …     def call(environment) breaks; no dispatcher yet       method = environment['REQUEST_METHOD']       path = environment['PATH_INFO']       @dispatcher.dispatch method, path, environment     rescue => exception       error_backtrace exception, environment     end       private     def error_backtrace(exception, environment)       exception_info = “some html”        [500, { 'Content-type' => 'text/html'}, exception_info]     end …   end end 2a0d1445
  • 14.
  • 15. Dispatch & Routing are the most complicated parts of Rails [citation needed]
  • 16. But That’s Okay • We’re not building Rails • It’s okay if we Fail to Scale • Just use an array to map paths to controllers and actions
  • 17. lib/spectacular/dispatcher.rb     def get_route(path)       @routes.detect{ |r| path === r[0] }     end …     class RoutePen       def route(path, controller, method)         @routes << [path, controller, method]       end     end 9cfb9609
  • 18.
  • 19. lib/spectacular/controller.rb     def response_for(method)       @method = method       send @method       [response_code, headers, body]     end …     def template_file       File.join(APP_DIR, 'views', controller_name, "#{@method}.html.haml")     end …     def body       return @body if defined? @body       template = File.read template_file       engine = Haml::Engine.new template       @body = engine.render     end ce2f00a3
  • 20. app/controllers/hello_controller.rb class HelloController < Spectacular::Controller   def index   end end app/views/hello/index.html.haml hello world ce2f00a3
  • 21.
  • 22. Wildcards lib/spectacular/dispatcher.rb def get_route(path) @routes.detect{ |r| r[0] === path } end 48b08205
  • 23. === 0 :> /abc/ === 'abc' # => true 0 :> String === 'abc' # => true 0 :> /too+t/ === 'toooooooooot' # => true
  • 24. A Wildcard Route app/routes.rb route /too+t/, TootController, :toot 48b08205
  • 25. Using the Path app/controllers/toot_controller.rb class TootController < Spectacular::Controller def toot @toot = @path.gsub('/','') end end app/views/toot/toot.html.haml %h1=@toot 48b08205
  • 26.
  • 27.
  • 28. 404 • When we can’t figure out how to process a request, that should be a “404” • We can simply make a wildcard route
  • 29. app/routes.rb route //, ErrorController, :not_found app/controllers/error_controller.rb class ErrorController < Spectacular::Controller def not_found @response_code = 404 end end 2a2653b2
  • 30.
  • 31. What’s Left • Only renders HAML • No layouts • Can’t easily render a different view • No query parameters • No POST
  • 32. thanks • Bryce Kerley; mercenary developer • bkerley@brycekerley.net • http://twitter.com/bonzoesc • Code on Github: http://bit.ly/spectac