What is Sinatra

Building Web Services with Sinatra

Sinatra is a framework written in the Ruby language. It’s designed as a simple, but very powerful and flexible way to build web-delivered application without a lot of setup, configuration or effort. Basically it’s quick and easy to get a web service up and running with Sinatra.

“DSL (domain specific language) for quickly creating web applications in Ruby with minimal effort.” 1

It can be used to quickly make anything from your own dynamic site to an API to open up data to other services. You can find out more on the project site: http://sinatrarb.com/

What is Sinatra

Sinatra is a Gem or library that extends the core functionality of the Ruby programming language and allows you to map incoming web requests to blocks of Ruby code.To do this, it builds on a Rack. Rack is a ‘webserver interface’ that basically allows a structured way of allowing Ruby frameworks to interact with HTTP or Web communications. It helps you handle any HTTP request and the pipeline of activities that you might need to address, such as authentication, caching and more, to deliver a great web-based software experience.

An Illustration of Sinatra - Incoming Web Requests Are Routed to Scripts on a server

Why Sinatra

There are lots of other web-application frameworks out there. For example, theres:

  • Ruby on Rails - another Ruby-based framework for web software development
  • Django - a Python based web-development frameowrk
  • Express - a Node.js based web-dev framework…
  • etc. etc. etc

All that is to say, is that there are a lot of options out there for you to choose. I recommend Sinatra because:

  • Ruby is easy to learn and great for beginners;
  • Sinatra is a minimal framework. It’s barebones and that means there’s not a lot of conventions, configurations or complexity when you start your first application.;
  • It’s widely used, has a great developer community, it’s quick and it scales to lots of different sizes of applications and scenarios. Bascially, it’s a great tool for lots of projects.
  • and it’s trusted. It’s used by lots of big companies to help deliver their software. If they’re using it, you know it’s a good one to put time into learning.

To illustrate this further, compare Sinatra to a basic Express script (example taken from: http://www.mervine.net/notes/sinatra-vs-express):

Sinatra

require 'sinatra'
get '/' do
  "Hello World"
end

Express

var app = require('express');
app.get('/', function(req, res) {
    res.send("Hello World!");
});
app.listen(8000);

Doesn’t Sinatra look much easier to write? It’s clearer, cleaner and simpler to read and understand.

Here’s another reason. In the image below you’ll see the default files included in Sinatra versus Ruby on Rails. Ruby on Rails has a lot more. Because it’s designed to encourage the use of web-development conventions. These conventions mean you have a LOT more to learn and a lot more to setup when you want to work with Ruby on Rails.

SINATRA VERSUS RUBY

The good news is, you can easily adapt what you learn from working with Sinatra to Ruby on Rails. This means that if your web project grows, you can easily scale to Ruby on Rail’s more complex framework later.

×

Subscribe

The latest tutorials sent straight to your inbox.