Rack
Scott Leberknight
HTTP
make request, get response
Request
a hash containing the environment
  (headers, params, input stream)
Response
status, headers, body
Simplest Rack App..
class Hello          status
  def call(env)                 headers
    [
      200,
      { "Content-Type" => "text/plain" },
      [ "Hello!", "Having fun yet?" ]
    ]
  end
end                     body
Middleware Order Matters !
middlewares call() each other...




and act like chain of response filters...




...so the order you use them matters!
init with app
class Reverse

  def initialize(app)              first, Call the app
    @app = app
  end

  def call(env)
    status, headers, body = @app.call(env)
    [status, headers, body.map { |b| b.reverse }]
  end

end

                         then, do your stuff
use   Rack::ContentLength
use   RackExamples::Paragraphizer
use   RackExamples::Upcase
use
use
      RackExamples::Downcase
      RackExamples::Reverse
                                       Is output upper or
app = lambda { |env| [
                                           lower case?
  200,
  { 'Content-Type' => 'text/html' },
  ["Dammit, I'm mad!",
    "Was it a rat I saw?",
    "Madam, I'm Adam"]
] }
run app




                                                            11
use   Rack::ContentLength
use   RackExamples::Paragraphizer
use   RackExamples::Downcase
use   RackExamples::Upcase
use   RackExamples::Reverse            How about now?
app = lambda { |env| [
  200,
  { 'Content-Type' => 'text/html' },
  ["Dammit, I'm mad!",
    "Was it a rat I saw?",
    "Madam, I'm Adam"]
] }
run app




                                                        12
"Off t he Shelf"
Rack::ShowExceptions
Rack::MailExceptions
Rack::CommonLogger
Rack::ContentLength
Rack::JSON
ActionController::StringCoercion
Rack::ETag
Rack::Deflater

...and lots more
Baked into Ra
             ils
$ rake middleware
(in /Users/sleberkn/Projects/my-niclabs)
use Rack::Lock
use ActionController::Failsafe
use ActionController::Session::CookieStore, #<Proc:0x00000001017acf18@(eval):8>
use   ActionController::ParamsParser
use   Rack::MethodOverride
use   Rack::Head
use   ActionController::StringCoercion
use   Rack::Deflater
use   Sass::Plugin::Rack
use   ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
run ActionController::Dispatcher.new
Summary
References
http://lmgtfy.com/?q=ruby+rack

http://www.letmebingthatforyou.com/?q=ruby+rack
Image credits
Shelf
thedesignblog.org/entry/odersoding-storage-system-doubles-as-a-room-divider/


Cool
newgeography.com


Baking
haileythebaketress.blogspot.com


Json
nearinfinity.com
scott.leberknight@nearinfinity.com
    www.nearinfinity.com/blogs/
      twitter: sleberknight

Rack