• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Rack is Spectacular
 

Rack is Spectacular

on

  • 1,640 views

Learning Rack by making a cheap Rails knockoff

Learning Rack by making a cheap Rails knockoff

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

Statistics

Views

Total Views
1,640
Views on SlideShare
1,632
Embed Views
8

Actions

Likes
0
Downloads
16
Comments
0

1 Embed 8

http://www.slideshare.net 8

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Rack is Spectacular Rack is Spectacular Presentation Transcript

  • 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