Download - Web simplicity: Ruby, Sinatra & Rack
![Page 1: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/1.jpg)
Web SimplicityRuby, Sinatra & Rack
Roland Swingler
@knaveofdiamonds
June ’09
Ruby on the web. More than rails. I’m going to talk about some of the benefits you get when you strip building things on the web down to their essentials.
![Page 2: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/2.jpg)
The time is:<?php echo date(“Y-m-d H:i:s”) ?>
I started out doing php. Php makes it really simple to start getting dynamic content out quickly. Has a low “Kathy Sierra ‘I suck’ threshold”.
![Page 3: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/3.jpg)
Generated rails project. All this just to show current time on the web?
![Page 4: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/4.jpg)
At least it isn’t Java.
![Page 5: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/5.jpg)
One file app
We really want a one file app. Sinatra is a ruby library that lets you do this.
![Page 6: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/6.jpg)
get “/now” do “The time is: #{Time.now}”end
Simplest Sinatra application. Also post, put, delete methods. You can specify parameters and so on in the path. All you have to do is return a string.
![Page 7: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/7.jpg)
$ ruby myapp.rb== Sinatra/0.9.2 has taken the stage...
$ curl http://localhost:4567/nowThe time is: Sat Jun 06 13:00:00 0100 2009
In development just run it with ruby.
![Page 8: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/8.jpg)
get “/greetings/:person” do erb :a_viewend
__END__
@@a_viewHello, <%= params[:person] %>
You can keep views in the same file (for really small apps), or you can move them out to a more standard views directory.
![Page 9: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/9.jpg)
class MyApp < Sinatra::Base get “/” do # something cool endend
You don’t have to do everything in a global namespace - you can extend Sinatra::Base
![Page 10: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/10.jpg)
HelpersLayoutsFilters
Environments
There’s more. You can build out to something very similar to rails if you want. Documentation at http://sinatrarb.com for more details.
![Page 11: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/11.jpg)
HTTPify everything
A tiny app is basically 1 line. Web app just became as easy as a shell script. Add it everywhere. Aaron Quint at GoGaRuCo talks about every library having web interface.
![Page 12: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/12.jpg)
|
So we can build small, simple tools. But there’s something missing. Pipes.
![Page 13: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/13.jpg)
We need to look at Rack. Originally designed to allow different frameworks to run on different app servers. Think WSGI if you know python.
![Page 14: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/14.jpg)
call(env)
Rack is the simplest api ever - one method: call which takes an environment Hash representing a request.
![Page 15: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/15.jpg)
[status, headers, body]
It needs to return a three part array with each of the parts of a HTTP response. Sinatra is just a thin routing layer built on top of this one method.
![Page 16: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/16.jpg)
RequestApp
+Framework
So why is this useful? Well this is the typical interaction - a request gets forwarded from a front-end web server like apache to your app, and you return a response. Everything is done in framework.
![Page 17: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/17.jpg)
RequestApp
+Framework
Middleware
But because a rack app is so simple, there’s nothing stopping you from pulling out some logic from the framework, and then forwarding on to the “real” app. Rack calls these components middleware. Think in-process http proxies.
![Page 18: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/18.jpg)
class Enterprisey < Sinatra::Base get “/annoying/*” do sleep 5 forward # we could do something here as well endend
Because Sinatra is just a rack app it can also be middleware. Saves the pain of dealing with routing etc. manually. Use the forward method to pass on to the next app.
![Page 19: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/19.jpg)
This is awesome
A lot of common cross-cutting stuff like logging, caching routing, auth can be pulled out and used across frameworks. No reason you couldn’t do views in middleware. Sandbox. Comet. Lots of ideas out there. Sinatra makes it easier.
![Page 20: Web simplicity: Ruby, Sinatra & Rack](https://reader034.vdocuments.us/reader034/viewer/2022052621/558bc420d8b42ac24b8b469f/html5/thumbnails/20.jpg)
Thanks!
@knaveofdiamonds
http://knaveofdiamonds.tumblr.com
http://rack.rubyforge.org
http://sinatrarb.com