Sinatra takes the stage, Padrino the cannoli.

I’ve been recently hard at work on strenghtening my skills on backend development for iOS apps. As I said in one of my previous posts almost every mobile app nowadays requires some sort of remote database to retrieve and sync data. Often times though, what we really need is some sort of very basic API. Ruby on Rails certainly can deliver what it promises, it’s a powerhorse, but it can also become cumbersome for some simple tasks. What I’m gonna briefly review today are Sinatra and Padrino, two somehow lightweight-ish alternatives to Rails.

Easy singing with Sinatra!

Sinatra is technically a Ruby DSL, a domain specific language. It’s not a framework, it’s kind of a specific language layer – on top of Ruby and Thin – that lets you implement in an easy and fast way a working web server.

Let’s get straight to an example. The following code sets up a server and responds with a simple "Hello world!" to each request made to the base url. Create a new .rb file and copy the following snippet in it:

require 'sinatra'  
get '/' do  
  "Hello world!"
end  

Then run it from the terminal with ruby your_file_name.rb. You should see something similar to:

> ruby sinatra.rb
== Sinatra/1.3.3 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop

Now you can visit localhost:4567 and, voilà, you webpage is served!

The good thing about Sinatra is that it's lightweight, fast and very easy to setup. Plus the code it's neat and readable: all the CRUD methods are explicit in that the routes are defined with HTTP verbs (get, post, put, delete). The bad thing – which for someone might be a good thing – is that there's no magic in it: I mean no helpers, no generators, not all the little nice conventions Rails has accustomed us to and is renowned for. If you want to setup even a simple API in Sinatra you have to code almost everything by hand. That's where Padrino comes in…

Padrino

Padrino is a web framework for Sinatra. Padrino is Sinatra at the core; according to Padrino's project home page, Padrino was created to make it fun and easy to code more advanced web applications while still adhering to the spirit that makes Sinatra great!.

Padrino combines the simplicity of Sinatra with some of the more useful functions of Rails (generators, basic scaffolding, helpers, form builders…). And it's even got a nice admin panel right out of the box (Django-style if you know what I mean) which is a very useful feature if you want to run an API where only a predefined number of admins can insert and modify data.

API with Padrino

We'll now build the same API we built in a previous post so you'll see how Padrino fares against Rails.

Run the following code in you terminal:

gem install padrino  
padrino g project padrino-api -e haml -d activerecord -b  
cd padrino-api  
padrino g admin  
padrino g model product name:string -a app  
padrino rake ar:migrate seed  
padrino g admin_page product  
padrino start  

That's it! You're ready to go. What you've just done is:

  • creating an app;
  • scaffolding the admin panel;
  • creating a basic product model (with simply a name of string type);
  • creating the tables in the database and seeding it with some informations (email, password) of the admin user;
  • generating the necessary views inside the admin panel to manage your newly created model;
  • starting the app.

If you now visit localhost:3000/admin you should be able to login and see a section dedicated to your Product model. You can now start adding some objects to your DB. One last thing before being able to consume the API: inside your project directory, in you app.rb file add the following snippet.

get '/' do  
  @products = Product.all
  @products.to_json
end  

This code is basically telling your app that whenever a request hits the base url it has to retrieve all the Product objects, serialize them and respond with some JSON. That's it. If you now visit localhost:3000/ you should get something like this:

[
    {"created_at":"2013-01-05T23:23:42+01:00","id":1,"name":"Book","updated_at":"2013-01-05T23:28:07+01:00"},
    {"created_at":"2013-01-05T23:28:13+01:00","id":2,"name":"Movie","updated_at":"2013-01-05T23:28:13+01:00"},
    {"created_at":"2013-01-05T23:28:17+01:00","id":3,"name":"iPhone","updated_at":"2013-01-05T23:28:17+01:00"}
]

Cool, huh? In a couple of minutes we have defined and set up an API. Of course it's still very basic stuff, but with some polishing it shouldn't take you much effort to build a more solid API for your mobile app.