Rack

2,051 views
1,951 views

Published on

A short presentation on Rack middleware I gave at the 10/21/2010 NovaRUG.

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
2,051
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Rack

  1. 1. Rack Scott Leberknight
  2. 2. HTTP make request, get response
  3. 3. Request a hash containing the environment (headers, params, input stream)
  4. 4. Response status, headers, body
  5. 5. Simplest Rack App..
  6. 6. class Hello status def call(env) headers [ 200, { "Content-Type" => "text/plain" }, [ "Hello!", "Having fun yet?" ] ] end end body
  7. 7. Middleware Order Matters !
  8. 8. middlewares call() each other... and act like chain of response filters... ...so the order you use them matters!
  9. 9. 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
  10. 10. 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
  11. 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
  12. 12. "Off t he Shelf"
  13. 13. Rack::ShowExceptions Rack::MailExceptions Rack::CommonLogger Rack::ContentLength Rack::JSON ActionController::StringCoercion Rack::ETag Rack::Deflater ...and lots more
  14. 14. Baked into Ra ils
  15. 15. $ 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
  16. 16. Summary
  17. 17. References
  18. 18. http://lmgtfy.com/?q=ruby+rack http://www.letmebingthatforyou.com/?q=ruby+rack
  19. 19. Image credits Shelf thedesignblog.org/entry/odersoding-storage-system-doubles-as-a-room-divider/ Cool newgeography.com Baking haileythebaketress.blogspot.com Json nearinfinity.com
  20. 20. scott.leberknight@nearinfinity.com www.nearinfinity.com/blogs/ twitter: sleberknight

×