Sinatra Up and Running: A Book Review

Share this article

Sinatra: Up and Running was published at the end of last year by O’Reilly Press and is the first book to be written exclusively about Sinatra. It is written by Alan Harris and Konstantin Haase. Konstantin is the current maintainer of Sinatra and probably knows more about it than anybody else (You can find out more about him in my interview). Alan has already written two books about Python, and is also an enthusiastic Rubyist and Sinatra user. I have been waiting for a book to be written about Sinatra for a long time and was really looking forward to this … and it didn’t disappoint.

book cover

The book is very thin and weighs in at just over 100 pages. But, like Sinatra, it packs a big punch into a small, lean package. It covers the latest version of Sinatra at the time of writing (1.3.1) and gets started quickly: The first chapter introduces Sinatra along with its design goals and philosophy. There’s also an interesting section on how learning Sinatra will help to develop transferable skills that can be used in other development environments. The first chapter concludes with a quick tutorial that uses Sinatra to build a complete web app. It’s a very basic rendition of the classic kid’s game Rock, Paper Scissors that perfectly emphasizes Sinatra’s credentials for rapid development. This chapter does dive straight in to some of the more advanced aspects of Sinatra, but it is more to whet the appetite for what is to follow, rather than a tutorial of how it works (and this is made clear in the text).

The second chapter is very referencey in nature as it goes through a large number of Sinatra’s methods and how they work. It begins by going through the basics of HTTP headers and verbs such as GET, POST, PUT, DELETE and the newly added PATCH, which helps develop an understanding of how Sinatra is essentially a wrapper on top of Rack for routing HTTP requests. In fact, this chapter illustrates the fact that Sinatra is really just a collection of helper methods that provide syntactic sugar for Rack’s methods. A Highlight in this chapter was the introduction of the new streaming API that allows you to keep the connection to the server open and do things like this:

[gist id=”2464149″]

The chapter also covers useful stuff such as caching, error handling (particularly 404 and 500 Internal Server errors), before and after filters, and rendering views and sessions. A lot of this info can be found in the excellent ReadMe section of the Sinatra website, but it was nice to see it all written down in the same place.

Chapter 3 starts to get heavier and highlights the symbiotic relationship that Sinatra has with Rack – the foundation for its magic. For example, there is a nice explanation of how the ‘get’ method works. It also covers how to go about extending the functionality of Sinatra using extension methods and helpers. Generally, extensions are used configuration or routing, whereas helpers are used in view blocks and templates. The chapter then goes quite deep into Rack and has a great example of a mini-implementation of Sinatra using Rack, which shows a little of how Sinatra and Rack work together. The chapter finishes by discussing how to use Sinatra to create Rack middleware applications.

Chapter 4 shows how you can subclass Sinatra in order to build modular applications. This builds the foundations of the good practice of creating your own structured framework. This is done by subclassing Sinatra itself like so:

[gist id=”2464157″]

It then goes on to explain how to use the run! command to get the server started. Settings are discussed next and, it turns out, they are actually just methods of the application class. It also covers subclassing these classes further and how the routes are inherited. The next section is really cool – it shows how you can very easily mimic Rails’ controller architecture by subclassing Sinatra::Base with an ApplicationController class and then subclassing your controllers from this, like so:

[gist id=”2464167″]

Things get even cooler as the authors demonstrate how to dynamically create applications on the fly using Sinatra.new. The chapter finishes off by showing how these subclasses fit into the Rack ecosystem as middleware and also how to use Extensions and Helpers in modular applications.

The last chapter in the book takes a more tutorial-led approach, going through the steps required to build a blog application that uses Git for versioning. There are some great nuggets in this chapter, including using the ostruct library to store the blog posts, using a hook to reload the app whenever there are any updates, and parsing the data for each article on the pages. It was also nice to see how the project folder was structured and some examples of views using erb, markdown, JavaScript and CSS. As a tutorial, it moves along at a fair pace – a lot of ground is covered in only a few pages – but it is easy to follow.

I was really impressed with this book. There isn’t much there, but what is there is worth it’s weight in gold and you can fly through it in a weekend (a plus point in my view). A word of warning though – it’s quite theoretical in places. Although you don’t need to know anything about Sinatra as a prerequisite, you will need a reasonable level of Ruby knowledge in order to follow along. You also need to be fairly confident at using the command line and installing gems as well as firing up a local server.

This shouldn’t put anybody off though – nothing is particularly difficult and since everything in Sinatra is just Ruby there are lots of places you can go to find out more information. I also found that the book really helped to improve my Ruby skills – it’s choc full of well-written snippets of ruby. It also encourages you to fire up an irb session and experiment with Sinatra in order to help further your understanding of how it works.

As the first book about Sinatra, I feel that this book is a success. The authors have done a great job of introducing Sinatra as well as giving the reader a thorough understanding of its inner-workings. Although the first and last chapter both contain example apps, they don’t really cover many use-cases. I feel that there is still a space for a more practical book that goes through building apps in more detail and design patterns that can be used for different types of applications. For example, database-driven apps are not featured at all in the book. If you want more practical advice then I can recommend Cloning Internet Applications in Ruby by Shau Seung.

The good news is that I hear there’s a follow up planned. Some things I’d like to see covered are:

  • More examples of using the streaming API
  • Testing … it’s not covered at all in the book
  • How to structure a modular application (file structure etc)

In the meantime, I would recommend anybody with an interest in improving their Sinatra or Ruby skills, or finding out what all the Sinatra fuss is about to buy this book.

Darren JonesDarren Jones
View Author

Darren loves building web apps and coding in JavaScript, Haskell and Ruby. He is the author of Learn to Code using JavaScript, JavaScript: Novice to Ninja and Jump Start Sinatra.He is also the creator of Nanny State, a tiny alternative to React. He can be found on Twitter @daz4126.

books
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week