Rack

Post on 14-Jun-2015

1.754 views 2 download

Tags:

description

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

Transcript of Rack

RackScott Leberknight

make request, get response

HTTP

Requesta hash containing the environment

(headers, params, input stream)

Responsestatus, headers, body

Simplest Rack App...

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

statusheaders

body

Middleware Order Matters !

middlewares call() each other...

and act like chain of response filters...

...so the order you use them matters!

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

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?

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?

"Off the Shelf"

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

...and lots more

Baked into Rails

$ 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

Summary

References

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

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

Image credits

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

Coolnewgeography.com

Bakinghaileythebaketress.blogspot.com

Jsonnearinfinity.com

scott.leberknight@nearinfinity.com

www.nearinfinity.com/blogs/

twitter: sleberknight