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, C...
A Rack Application
                                  config.ru
  use Rack::ContentLength
   
  run(proc do
    [ 200, { 'Co...
What is Rails

• ActionPack
 • Router - maps URLs to code
 • Controller - environment for code
 • View - generating respon...
The Essential Rails

• ActionPack
 • Router - maps URLs to code
 • Controller - environment for code
 • View - generating ...
config.ru
              require 'lib/spectacular.rb'
              use Rack::ContentLength
               
              ru...
lib/spectacular/app.rb

module Spectacular
  class App
…
    def call(environment)
      method = environment['REQUEST_MET...
lib/spectacular/app.rb

module Spectacular
  class App
…
    def call(environment)
              breaks; no dispatcher yet...
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
  controll...
lib/spectacular/dispatcher.rb
    def get_route(path)
      @routes.detect{ |r| path === r[0] }
    end
…
    class RouteP...
lib/spectacular/controller.rb
    def response_for(method)
      @method = method
      send @method
      [response_code,...
app/controllers/hello_controller.rb
class HelloController < Spectacular::Controller
  def index
  end
end




       app/v...
Wildcards
          lib/spectacular/dispatcher.rb

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




      ...
===
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 = ...
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...
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...
Rack is Spectacular
Rack is Spectacular
Rack is Spectacular
Rack is Spectacular
Rack is Spectacular
Rack is Spectacular
Rack is Spectacular
Rack is Spectacular
Upcoming SlideShare
Loading in...5
×

Rack is Spectacular

1,179

Published on

Learning Rack by making a cheap Rails knockoff

Live at http://spectac.scul.us/
Source at http://github.com/bkerley/spectacular

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

  • Be the first to like this

No Downloads
Views
Total Views
1,179
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Rack is Spectacular

    1. 1. Rack is Spectacular Learning Rack by making a cheap Rails knockoff
    2. 2. WARNING CODE HEAVY HTTP Ruby Rails
    3. 3. WARNING CODE HEAVY So get the code http://bit.ly/spectac c9c340bb
    4. 4. WARNING CODE HEAVY NOT READY FOR PRODUCTION c9c340bb
    5. 5. Rack • Standard Ruby-HTTP interface • Write (framework) once • Run anywhere (that is Rack-compatible) • Rails, Sinatra, Camping • Mongrel, Passenger, Unicorn
    6. 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. 7. What is Rails • ActionPack • Router - maps URLs to code • Controller - environment for code • View - generating response body • ActiveRecord - database access
    8. 8. The Essential Rails • ActionPack • Router - maps URLs to code • Controller - environment for code • View - generating response body • ActiveRecord - database access
    9. 9. 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
    10. 10. 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
    11. 11. 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
    12. 12. Dispatch & Routing are the most complicated parts of Rails [citation needed]
    13. 13. 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
    14. 14. 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
    15. 15. 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
    16. 16. app/controllers/hello_controller.rb class HelloController < Spectacular::Controller   def index   end end app/views/hello/index.html.haml hello world ce2f00a3
    17. 17. Wildcards lib/spectacular/dispatcher.rb def get_route(path) @routes.detect{ |r| r[0] === path } end 48b08205
    18. 18. === 0 :> /abc/ === 'abc' # => true 0 :> String === 'abc' # => true 0 :> /too+t/ === 'toooooooooot' # => true
    19. 19. A Wildcard Route app/routes.rb route /too+t/, TootController, :toot 48b08205
    20. 20. 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
    21. 21. 404 • When we can’t figure out how to process a request, that should be a “404” • We can simply make a wildcard route
    22. 22. 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
    23. 23. What’s Left • Only renders HAML • No layouts • Can’t easily render a different view • No query parameters • No POST
    24. 24. thanks • Bryce Kerley; mercenary developer • bkerley@brycekerley.net • http://twitter.com/bonzoesc • Code on Github: http://bit.ly/spectac
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×