Sinatra who???
As the title implies, when I first saw the word Sinatra appear on my course calendar, I did a double take. It seemed to be a bit of an odd name for a software to have, but who am I to question. Apparently the name derives from the American singer, Frank Sinatra.
<Frank Sinatra (chairman of the board) for having so much class he deserves a web-framework named after him>
Enough with the rambling! Let’s dive and learn What Sinatra is, Why and How it could be useful to building a full web-application.
WHAT IS SINATRA?
Sinatra is a DSL, a Domain Specific Language. It is a language that’s written to solve some issues or concerns with a specific domain/language. DSL is specific to a particular language. One thing to note here is that Sinatra is specific to the Ruby language and it can solve some of the issues within the language. Remember that Sinatra is not a language!
WHY do we use SINATRA?
(̵̶̵f̵̶̵o̵̶̵r̵̶̵ ̵̶̵o̵̶̵t̵̶̵h̵̶̵e̵̶̵r̵̶̵ ̵̶̵r̵̶̵e̵̶̵a̵̶̵s̵̶̵o̵̶̵n̵̶̵s̵̶̵ ̵̶̵t̵̶̵h̵̶̵a̵n̵̶̵ ̵̶̵i̵̶̵t̵̶̵’̵̶̵s̵̶̵ ̵̶̵r̵̶̵e̵̶̵q̵̶̵u̵̶̵i̵̶̵r̵̶̵e̵̶̵d̵̶̵ ̵̶̵f̵̶̵o̵̶̵r̵̶̵ ̵̶̵m̵̶̵y̵̶̵ ̵̶̵c̵̶̵o̵̶̵u̵̶̵r̵̶̵s̵̶̵e̵̶̵)̵̶̵
Sinatra is a great library to learn when you’re entering full stack web-development. In order for us to learn Rails, using Sinatra will act as training wheels for Rails. Although it is not quite popular due to its old age and simpler library, it will come in handy when the concepts Rails abstract the Sinatra concepts.It is great when you want to create a quick web application in Ruby.
How do we use SINATRA?
Sinatra can do a lot of things but it is great for learning the basics of HTTP and routing. It can create routes and render different pages based on those routes. Before I share more usages of Sinatra, let’s quickly review some of the keywords.
- Requests & Response Flows: Types of requests being made in the application and how we (developers) respond. Both Sinatra and Rails process the response.
- Client vs Server: Client is the user, the one who’s requesting and the server is the application, sending the response.
- Routing: Routes are the backbone of an application. They guide how clients will navigate the actions we define for your application. Sinatra is quite flexible with routing! Here are some
- HTTP Methods: Correspond to Create Read Update and Delete operations. (CRUD)
GET- Requesting data.
Client: “Hey get me something! Show me a lists of posts/comments!”
POST- Creating data.
Client: “Here’s my comment/photo/post!” Server: “Alrighty, I’ll take care of your added data and add it to the database.”
PATCH- Modifying a part of the data, Partially changing data.
Client: “Change my nickname to something else, but leave my bio!”
PUT- Take the entire record and update the entire record.
Client: *no example provided due to the lacking usage of this method*
DELETE- Delete data
Client: “Delete this ugly selfie! I don’t want to see it on my post anymore!”
HOW does All of this Come Along?
We create a new class Application and then inherit from Sinatra::Base. The Controllers will define our routes and behave like a middleman between models and views. We will ask the Controller, “Hey Model, You need to grab ‘this’ and store it!” One thing to note is that a View and model should never directly communicate!!! View should never directly perform data base queries!
class ApplicationController < Sinatra::Base get '/' do
"Frank Sinatra was here."
end
end
Start spreading the news of this Classy web-development dressed in a DSL, because I want to be a part of it…New York, New York!