More Related Content Similar to Rack is Spectacular (20) Rack is Spectacular3. WARNING
CODE HEAVY
So get the code
http://bit.ly/spectac
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
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
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
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
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
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
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
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