web simplicity: ruby, sinatra & rack

20
Web Simplicity Ruby, 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.

Upload: roland-swingler

Post on 25-Jun-2015

4.270 views

Category:

Technology


3 download

DESCRIPTION

Lightning talk given at the Dynamic languages night at the BCS. Covers off Sinatra & Rack, and using Sinatra as Rack middleware. Focuses on the benefits you get from keeping everything simple.

TRANSCRIPT

Page 1: Web simplicity: Ruby, Sinatra & Rack

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

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

Generated rails project. All this just to show current time on the web?

Page 4: Web simplicity: Ruby, Sinatra & Rack

At least it isn’t Java.

Page 5: Web simplicity: Ruby, Sinatra & Rack

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

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

$ 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

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

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

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

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

|

So we can build small, simple tools. But there’s something missing. Pipes.

Page 13: Web simplicity: Ruby, Sinatra & Rack

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

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

[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

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

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

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

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

Thanks!

@knaveofdiamonds

http://knaveofdiamonds.tumblr.com

http://rack.rubyforge.org

http://sinatrarb.com