Rack

23
Rack Scott Leberknight

description

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

Transcript of Rack

Page 1: Rack

RackScott Leberknight

Page 2: Rack
Page 3: Rack

make request, get response

HTTP

Page 4: Rack

Requesta hash containing the environment

(headers, params, input stream)

Page 5: Rack

Responsestatus, headers, body

Page 6: Rack

Simplest Rack App...

Page 7: Rack

class Hello def call(env) [ 200, { "Content-Type" => "text/plain" }, [ "Hello!", "Having fun yet?" ] ] endend

statusheaders

body

Page 8: Rack

Middleware Order Matters !

Page 9: Rack

middlewares call() each other...

and act like chain of response filters...

...so the order you use them matters!

Page 10: Rack

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

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

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

"Off the Shelf"

Page 14: Rack

Rack::ShowExceptionsRack::MailExceptionsRack::CommonLoggerRack::ContentLengthRack::JSONActionController::StringCoercionRack::ETagRack::Deflater

...and lots more

Page 15: Rack

Baked into Rails

Page 16: Rack

$ 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

Summary

Page 18: Rack
Page 19: Rack
Page 20: Rack

References

Page 21: Rack

http://lmgtfy.com/?q=ruby+rack

http://www.letmebingthatforyou.com/?q=ruby+rack

Page 22: Rack

Image credits

Shelfthedesignblog.org/entry/odersoding-storage-system-doubles-as-a-room-divider/

Coolnewgeography.com

Bakinghaileythebaketress.blogspot.com

Jsonnearinfinity.com

Page 23: Rack

[email protected]

www.nearinfinity.com/blogs/

twitter: sleberknight