Sinatra Tutorial for Beginners

Sinatra Framework Tutorial

Sinatra is a small, lightweight web framework written in Ruby. Sinatra is a free and open-source web application library and domain-specific language written in Ruby, designed for the speedy creation of web applications. It allows the developer to make small web applications quickly.

Like Rails, Sinatra is built upon Rack so, you can think of it as a bare-bones version of Rails. There is no ‘magic’ to speak of as everything is very explicit. This is why learning Sinatra before learning Rails is so beneficial. It reduces the learning curve of teaching Rails quite a lot.

History of Sinatra

Sinatra Framework has adopted its name from the famous musician named Frank Sinatra. In the beginning, this software was developed to be an alternative to other web applications like Camping, Nitro, Merb, and Ruby on Rails. The software is dependent on the Rack web server interface and has gained massive popularity among users due to its small size and flexibility.

Sinatra is DSL (Domain Specific Language) for Ruby, which is small, fast, and light. It has very little MVC and no scaffolding, but it does have a lot of options for views.

Sinatra supports flexible coding. It has just under 2000 lines of code compared to Rails which has close to 100,000 lines of code, making it ideal for small applications and APIs which don’t require a full-fledged web framework. Sinatra is a trendy gem with over 10 million downloads. Sinatra is suitable for dealing with HTTP from the server side.

Features of Sinatra

Sinatra Framework has many features which ensure that the users were capable of developing web applications in Ruby efficiently.   

Some salient features of Sinatra as follows:

  1. Simplicity – A fully functional web app can be created in one single file by using Sinatra. Routes are easy to implement and have a syntax that uses all of the HTTP verbs – GET, DELETE, POST and PUT.
  2. Lightweight – Sinatra is designed to contain the bare necessities, so it is very light weighted.
  3. Flexibility – Sinatra is excellent for API implementations, Facebook, widgets, etc. This framework is built on the top of Rack, so it is easy to extend using Ruby Gems, Rack-Middleware, and its API extension.
  4. WDNNSP (We Don’t Need No Stinking Patterns) – Sinatra is very flexible as it does not support any presumptions of organizing the domain or business logic. It does not consist of any implicit folder structure or default Database Abstractor and also does not restrict how or where it will be used.
  5. Embeddable inside Rails App – Since Sinatra can be embedded in a Rails app, it is useful for a use-case making Sinatra better than Rails such as APIs.

Installation

For installing Sinatra, you must have installed RubyGemsalready in your system. The installation of Sinatra Framework is as simple as running the following code –

Extensions

Extensions provide helper or class methods for Sinatra applications. A few useful ones Sinatra Framework Extensions are as follows: –

  • Sinatra Static Assets –This extension provides helper methods to output tags for static assets.
  • Sinatra URL For – It constructs absolute paths and full URLs for different actions in Sinatra.
  • Sinatra Authorization –This extension provides HTTP authorization helpers.

Application of Sinatra

Sinatra software is distinct from various other applications because it does not use any model-view-controller pattern that was previously used in other applications unlike Ruby on Rails. The development of Sinatra ensures that users are capable of developing web applications in Ruby efficiently. 

Sinatra has been used by numerous government agencies and leading global tech companies. Some of the prominent organizations that have used or are using Sinatra Framework are Apple Inc., Engine Yard, Singbird, LinkedIn, GitHub, Heroku, US National Security Agency, and British Government’s Government Digital Service.

Advantages of Sinatra

1.    The Sinatra Framework provides an excellent platform for users who have previously worked with Rails but are looking for a more uncomplicated Single Page Application or API.

2.     For those who have worked with both Rails and Rack and need an application that is in between, Sinatra solves the issue.

3.    The Sinatra Framework can point out errors and tell you what the problem is. This makes it a better application since this is not common among many platforms.

4.    Sinatra platform supports limited barriers of entry for users who want to create a Ruby app without utilizing the extra features that come with more prominent platforms such as Rails.

Disadvantages of Sinatra

1. Lacks HTML Helper Methods

Sinatra supports only HTTP but not HTML. Ruby on Rails does not include tags for generating HTML. Sinatra sought to be a bad fit for developers who are looking for a web framework that actively assists with writing HTML.

2. Header Issues

One major problem of Sinatra Framework is that it involves cookie-setting headers (implicitly added when adding sessions to any application) and cache-control headers (which were added using Sinatra’s header helpers). These issues were the cause of a few headaches. 

3. Deployment Can Be Tricky

Sinatra provides a bit more information about deploying to other platforms but does not go into specifics about deployment automation.

4. You Determine File Structure

One could create a complete Sinatra application (including template content for views) in a single file. It is an excellent feature for small sites or APIs, but larger projects should span multiple files. Sinatra does not allow the developers to separate the parts of the app, like data-related logic and presentation.

5. Limited Applications

The application is limited in terms of the methods available to its users.

6.     Lack of Community

In Sinatra, there is a lack of community adoption, which might bother the developer if they want to use community adoption in their projects.

7.    Limited Libraries

The Sinatra application comes with only a limited number of libraries in addition to the standard Ruby libraries.

Sinatra versus Rails

Sinatra

Rails

Sinatra is a free and open-source web application library and domain-specific language written in Ruby.

Rails is a server-side web application frameworkwritten in Ruby under the MIT License. 

Sinatra is a lightweight Framework and needs fewer resources.

Rails on the other hand is packed with a lot of features, comes with a ton of code.

Sinatra is efficient for learning the basics of HTTP and routing.

Rails is better for learning how to use a database and store things.

Sinatra is great for smaller applications that need to run fast.

Rails supports numerous heavy features hence, it is not so fast as compared to Sinatra.

Templates

Sinatra is built upon an extraordinarily powerful templating engine known as Tilt which, is designed to be a “thin interface” for frameworks that want to support for multiple template engines.

Some of Tilt’s features include:

  • Custom template evaluation scopes.
  • Bindings Ability to pass locals to template evaluation.
  • Provides support for passing a block to template evaluation to “yield” backtraces with correct filenames and line numbers Template file caching and reloading.
  • It includes support for some of the best engines available, such as HAML, coffee-script and Less CSS.