Sinatra Organization

This blog delves into the organization patterns of larger code-bases. Up until now, we’ve been working on relatively small projects that include one application file, and maybe 3-4 class and module files. To explore the file structure of larger projects, I will be looking through an open source Sinatra project called So-Nice. So-Nice is a simple web interface to control iTunes, Spotify, and other music players. The project’s code-base can be found here.

Overall structure

The top level of the application’s code-base is comprised of two folders and five main files.

The files:

  • Gemfile: The Gemfile lists all the application’s dependencies. Dependencies are all the gems that the application relies on. This file is particularly important in this application, because it loads the ‘anyplayer’ gem, which supports the various music players, including Spotify and iTunes. Other dependencies include ‘sinatra,’ ‘haml,’ ‘xml-simple,’ ‘json,’ and ‘thin.’ All of these gems are sourced from
  • Gemfile.lock: This file is a snapshot of all the gems and versions that were installed. This way you know the exact versions of all the gems that you used when your application last worked. This is especially important and useful when you are sharing your work with collaborators. Others won’t have to guess what veresion of the dependencies they should install.
  • The readme file contains a brief description of what the web application does and how to install and use it on your machine.
  • This is the rackup file that the Sinatra application needs to include in its root directory. The ‘run’ contained in this config file can be called on anything that responds to a .call method. The application itself is also ‘required’ through the config file. In this case, the application is ‘sonice.rb’
  • sonice.rb: This file is the Ruby application that is loaded by the rackup file.

The folders:

  • public: Sinatra, by default, looks for ‘assets,’ such as CSS files and images, in the ‘public’ folder. So-Nice’s public folder contains its stylesheet, a picture, and various javascript files.
  • views: The ‘views’ folder contains one file, called ‘index.haml.’ Haml is a markup language that produces clean, well-structured HTML. Haml can also handle inline code, like Ruby. Sinatra, by default, looks for webpage templates in the ‘views’ folder.

Useful resources on the subject

The following is a list of useful resources to help understand the various components of the Sinatra file structure.