web simplicity: ruby, sinatra & rack

Post on 25-Jun-2015

4.270 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

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

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.

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”.

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

At least it isn’t Java.

One file app

We really want a one file app. Sinatra is a ruby library that lets you do this.

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.

$ 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.

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.

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

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.

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.

|

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

We need to look at Rack. Originally designed to allow different frameworks to run on different app servers. Think WSGI if you know python.

call(env)

Rack is the simplest api ever - one method: call which takes an environment Hash representing a request.

[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.

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.

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.

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.

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.

Thanks!

@knaveofdiamonds

http://knaveofdiamonds.tumblr.com

http://rack.rubyforge.org

http://sinatrarb.com

top related