Professional Documents
Culture Documents
Learning Sinatra - Sample Chapter
Learning Sinatra - Sample Chapter
ee
C o m m u n i t y
Sudeep Agarwal
$ 29.99 US
19.99 UK
P U B L I S H I N G
pl
Learning Sinatra
Learning Sinatra
Sa
m
D i s t i l l e d
Learning Sinatra
Design and deploy your own web application in minutes
using Sinatra
E x p e r i e n c e
Sudeep Agarwal
Manoj Sehrawat
of experience in the industry. He was with Directi for 3 years right after finishing his
education from NIT Trichy. At Directi, he was a part of the team that wrote one of
the most widely used in-house web-based applications; this is where his interest in
Sinatra started and he used it to write some of his personal projects. Although he is
not writing a lot of web applications at Inmobi, he is still contributing to one of the
ORMsRuby Object Mapper.
Preface
This book will help you understand the basic concepts of Sinatra and build
lightweight web applications. The book follows a step-by-step course, right from
setting up Ruby and installing Sinatra to inculcating best practices for writing
beautiful code. By the end, you will have a running Sinatra app ready and the
confidence to write more apps by yourself.
Preface
Chapter 7, Handling User Data, shows you how to create a form and its necessary
attributes, and explains how it works. We will see the importance and usage of the
method and action attributes of a form. We will create a for loop to add a List
and Items of a list to a single form, where we will see how we can add an array of
values to the HTML input. You will also learn how to add items to the form through
JavaScript dynamically using a simple dummy template. We will see how to handle
data at the backend sent by a user through filling in a form, and discuss validations
and the types of validation.
Chapter 8, Connecting to a Database, covers how to connect to a database (MySQL) with
various necessary parameters. You will learn how to use the mysql gem with sequel.
We will also cover how to open and use an interactive console with Sinatra and Sequel
and the details of Sequel models with various useful class-level and instance-level
methods. We will discuss hooks and callbacks available and associations with eager
loading and joins in order to scale and optimize the application.
Chapter 9, Authentication and Authorization, shows you the basics of a session in
general and how we can configure it with Sinatra, both with simple and advanced
options. We will see the various useful methods of a session object and the meaning
of authentication and authorization individually.
Chapter 10, Deploying the App, discusses what deployment is, Heroku, and
how we can use Heroku to deploy our app. You will also learn the initial setup
steps that we need to do on Heroku, and about various Heroku commands and
their billing models.
Introduction to Sinatra
Sinatra is a Ruby-based application framework used to create web applications
quickly. It can be used to write simple single-page applications or large and complex
ones. Sinatra is very lightweight as it does not include a variety of gems, though the
user can include gems as required.
Sinatra is widely used across the globe and has gained a lot of popularity because
of its flexibility. It does not follow the Model-View-Controller (MVC) architectural
pattern completely but it is fairly simple to build one on top of Sinatra. Sinatra is a
View-Controller framework.
If you have been working with Ruby, you can go ahead and try out the codes;
otherwise, you can go ahead with this chapter as we will be covering some basic
Ruby in the next chapter. In this chapter, we will be discussing the following topics:
Model-View-Controller
What is MVC?
The MVC (Model-View-Controller) is an architectural pattern that divides the
application in three parts, namely, the model (the data), view (the user interfaces),
and controller (the interconnections between the model and view).
[1]
Introduction to Sinatra
MVC separates the code based on their responsibilities and makes it easier to
understand and maintain:
The concept of MVC will be clearer once we get into the examples using Sinatra.
Installing Sinatra
Sinatra is just another gem and there is no special way to install it. We use the gem
install command:
This will install the latest version of the Sinatra gem and all its dependencies on your
system. To see which version is installed, use the following:
$ gem list sinatra
[2]
Chapter 1
Save the file with a .rb extension (say, helloworld.rb) and execute this on the
command line:
$ ruby helloworld.rb
This will interpret the code and start the built-in server.
Now, we open the browser and point it to http://localhost:4567.
What did the code do?
1.require 'sinatra'
This block is invoked when the server receives a get request on the '/' path.
4. return 'Hello World!'
[3]
Introduction to Sinatra
Here, line 1 requires just the gem. Lines 3 to 5 define a code-block that is invoked
on receiving a get request and returns a value. So here, this code-block forms the
controller. It controls what to return when a request is received.
The view is the page rendered by the browser. We do not have a separate view here
but line 4 defines the contents of the view.
[4]
Chapter 1
HAML
HTML Abstraction Markup Language (HAML) is a templating engine that helps in
writing cleaner and simpler HTML. For example, refer to the following table:
HTML
HAML
<body>
<div id='comment'>
<form>
<input type='text'>
</form>
</div>
</body>
%body
%div#comment
%form
%input{:type => 'text'}
We can see here how HAML is simpler and neater than HTML.
Installing HAML
The following is the command to install HAML:
$ gem install haml
[5]
Introduction to Sinatra
2 require 'haml'
3
4 get '/' do
5
haml :index
6 end
index.haml
1 %body
2
%h1
3
Hello World!
haml :index
This is a ruby method call. The haml method has argument:index. The method
will look for a file named index.haml in the views folder, generate the HTML
from HAML, and return it to the client.
The following method will call the required file:
haml :<filename>
If the file does not exist in the views folder or if there are any syntax errors in
HAML, then the application will throw an error.
Let's now see index.haml:
1 %body
2
%h1
3
Hello World!
[6]
Chapter 1
We will see later that an HTML element can be defined by % in HAML and
indented blocks can be used to define child elements. So, the preceding code
will be interpreted as follows:
<body>
<h1>
Hello World!
</h1>
</body>
You want to be flexible and choose your own gems and libraries. Unlike
Rails, Sinatra lets the user choose whatever gems s/he wants to use.
You want a higher performance. Sinatra is lightweight and does only the
stuff that you want it to do.
You find that the conventions in Rails are good to work with
Other frameworks
Sinatra is not the only framework being used. Here are some more that you might
have heard of:
Introduction to Sinatra
Summary
So, now that we have discussed what MVC is and how it is useful, you can
understand how to write your application such that the responsibilities are clearly
separated in the code. Additionally, if you are familiar with Ruby, you might have
seen how easy it is to get started with Sinatra and build a web server in minutes.
Don't worry if you could not try out the codes in this chapter as we will be covering
the same in detail soon. In the next chapter, we will be discussing the basic concepts
of Ruby. We will be discussing enough for you to get started with Ruby and Sinatra,
install Ruby and gems, and understand the data structures in Ruby. There will be
a lot for you to explore that you will find really exciting when we go further with
the book.
[8]
www.PacktPub.com
Stay Connected: