Rack
-
Upload
scott-leberknight -
Category
Technology
-
view
1.754 -
download
2
description
Transcript of Rack
![Page 1: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/1.jpg)
RackScott Leberknight
![Page 2: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/2.jpg)
![Page 3: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/3.jpg)
make request, get response
HTTP
![Page 4: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/4.jpg)
Requesta hash containing the environment
(headers, params, input stream)
![Page 5: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/5.jpg)
Responsestatus, headers, body
![Page 6: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/6.jpg)
Simplest Rack App...
![Page 7: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/7.jpg)
class Hello def call(env) [ 200, { "Content-Type" => "text/plain" }, [ "Hello!", "Having fun yet?" ] ] endend
statusheaders
body
![Page 8: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/8.jpg)
Middleware Order Matters !
![Page 9: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/9.jpg)
middlewares call() each other...
and act like chain of response filters...
...so the order you use them matters!
![Page 10: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/10.jpg)
class Reverse
def initialize(app) @app = app end
def call(env) status, headers, body = @app.call(env) [status, headers, body.map { |b| b.reverse }] end
end
first, Call the app
then, do your stuff
init with app
![Page 11: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/11.jpg)
use Rack::ContentLengthuse RackExamples::Paragraphizeruse RackExamples::Upcaseuse RackExamples::Downcaseuse RackExamples::Reverse
app = lambda { |env| [ 200, { 'Content-Type' => 'text/html' }, ["Dammit, I'm mad!", "Was it a rat I saw?", "Madam, I'm Adam"]] }run app
11
Is output upper or
lower case?
![Page 12: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/12.jpg)
use Rack::ContentLengthuse RackExamples::Paragraphizeruse RackExamples::Downcaseuse RackExamples::Upcaseuse RackExamples::Reverse
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
How about now?
![Page 13: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/13.jpg)
"Off the Shelf"
![Page 14: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/14.jpg)
Rack::ShowExceptionsRack::MailExceptionsRack::CommonLoggerRack::ContentLengthRack::JSONActionController::StringCoercionRack::ETagRack::Deflater
...and lots more
![Page 15: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/15.jpg)
Baked into Rails
![Page 16: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/16.jpg)
$ rake middleware(in /Users/sleberkn/Projects/my-niclabs)use Rack::Lockuse ActionController::Failsafeuse ActionController::Session::CookieStore, #<Proc:0x00000001017acf18@(eval):8>use ActionController::ParamsParseruse Rack::MethodOverrideuse Rack::Headuse ActionController::StringCoercionuse Rack::Deflateruse Sass::Plugin::Rackuse ActiveRecord::ConnectionAdapters::ConnectionManagementuse ActiveRecord::QueryCacherun ActionController::Dispatcher.new
![Page 17: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/17.jpg)
Summary
![Page 18: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/18.jpg)
![Page 19: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/19.jpg)
![Page 20: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/20.jpg)
References
![Page 21: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/21.jpg)
http://lmgtfy.com/?q=ruby+rack
http://www.letmebingthatforyou.com/?q=ruby+rack
![Page 22: Rack](https://reader034.fdocuments.net/reader034/viewer/2022042815/557d9cd8d8b42a3a7c8b4c62/html5/thumbnails/22.jpg)
Image credits
Shelfthedesignblog.org/entry/odersoding-storage-system-doubles-as-a-room-divider/
Coolnewgeography.com
Bakinghaileythebaketress.blogspot.com
Jsonnearinfinity.com