In the last article in the series - Micro Frameworks for Rapid Web Development Series, we had talked about Flask, which is a microframework for Python. To recap, Web Microframeworks are small and focused frameworks which make very few assumptions or decisions about how it will be used in building websites, APIs or web services, all the while providing granular control of the codebase. Web Microframeworks provide a lot of freedom in terms of how they can be used and are not restrictive with respect to libraries and extensions.
Popular web frameworks such as Ruby on Rails tend to be resource heavy and include lots of boilerplate code which may be unnecessary for smaller web applications. Microframeworks avoid this by providing only the bare essentials and leaving the rest to the web developer to decide.
Some of the microframeworks available for Ruby are Sinatra, Padrino and Cramp. We will discuss Sinatra in this part.
About Sinata, a Ruby Microframework
Sinatra is DSL(domain specific language) for Ruby which is small, fast and light. It has has very little MVC and no scaffolding but it does have a lot of options for views.
For comparision, Sinatra 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 very popular gem with over 10 million downloads today.
Sinatra is ideal for dealing with HTTP from the server side. If you think in terms of HTTP requests/responses, Sinatra is the ideal tool. If full integration and as much boilerplate as possible is needed, Rails is the way to go.
Where Sinatra Shines
Simplicity - A fully functional web app using Sinatra can be created in one single file. Routes are simple to implement and have a syntax that uses all of the HTTP verbs - GET, POST, PUT and DELETE.
Lightweight - Since Sinatra is designed to contain the bare necessities, it is very lightweight.
Flexibility - Sinatra is great for API implementations, widgets, Facebook apps etc. Since it is built on top of Rack, it is easy to extend using Ruby Gems, Rack-Middleware and its own extension API.
WDNNSP(We Don't Need No Stinking Patterns) - Sinatra is very flexible with no preconceptions of how the domain or business logic is organizes. It has no implicit folder structure, no default Database Abstractor, and no restrictions about how or where it will be used.
Embeddable inside Rails App - Since Sinatra can be embedded in a Rails app, it can be useful for a specific use-case where Sinatra maybe better than Rails such as APIs.
Built using Sinatra
- TheMovieDB - A popular API for movies
- Github - Github uses Sinatra for post-receive hooks, calling user specified services/URLs, whenever someone pushes to their repository.
- Nesta CMS - Describes itself as a Ruby CMS for developers and designers. Good example of how Sinatra can be used to build comparitively complex applications.
If you have RubyGems installed, installation Sinatra is a as simple as running -
gem install sinatra
If you don't have RubyGems installed, you can install it from their website.
Simple Web App
Let us write a simple web applications using Sinatra to demonstrate how it works.
First, create a file called hello_sinatra.rb in a new folder -
mkdir simple_sinatra && cd simple_sinatra touch hello_sinatra.rb
Add the following code snippet to the hello_sinatra.rb file -
require 'sinatra' get '/' do 'Hello Sinatra!' end
We will discuss how it works in the next section, for the moment run the app to see the results.
Running the app is as simple as this -
$ ruby hello_sinatra.rb [2013-11-27 18:36:03] INFO WEBrick 1.3.1 [2013-11-27 18:36:03] INFO ruby 2.0.0 (2013-06-27) [x86_64-linux] == Sinatra/1.4.4 has taken the stage on 4567 for development with backup from WEBrick [2013-11-27 18:36:03] INFO WEBrick::HTTPServer#start: pid=25283 port=4567
Sinatra uses WEBrick to start a server locally and it can be accessed by navigating to localhost:port where port number will be the number mentioned after port= , in our case http://localhost:4567
Once you navigate to the page, you should be able to see the message -
That's it! 4 lines of code in a single file and we have a simple web app up and running.
What We Just Did
First things first, we require Sinatra -
Next comes the interesting part of Sinatra -
get '/' do
For the root directory to say 'Hello, Sinatra!', we need to define the route '/'. In Sinatra, a HTTP method followed by the URL path defines a route. What happens in that route is defined in the following block.
We come to the next part, which is the simplest, we just return 'Hello Sinatra!'
'Hello Sinatra!' end
That wasn't too hard. To learn more follow the guides from the next section -
Some other useful tutorials are -
Extensions provide helper or class methods for Sinatra applications. A few useful ones are -
- Sinatra Static Assets - Provides helper methods to output tags for static assets.
- Sinatra URL For - Constructs absolute paths and full URLs for actions in Sinatra.
- Sinatra Authorization - Provides HTTP authorization helpers.
If you are already familiar with Sinatra and are looking for some guides on advanced topics, check these -
- There are a lot of web frameworks available each with their own set of advantages and disadvantages. To decide which web framework to use is a key step and depends on various factors. It is necessary to identify these factors and compare each framework thoroughly before picking one. Sinatra is ideal for small web applications and APIs, it maybe extended to use in large applications.
Micro Frameworks for Rapid Web Development Series
- Micro Frameworks for Rapid Web Development - Flask
- Micro Frameworks for Rapid Web Development - Sinatra