DEV Community

Cover image for Sinatra - Movies catalog (Part-1)
Merdan Durdyyev
Merdan Durdyyev

Posted on • Edited on

Sinatra - Movies catalog (Part-1)

WELCOME

Good day, dear coders, enthusiasts, and lifetime learners!
Today I decided to post about a simple project I developed recently. This project was both just for fun and a learning step for me into the 'sinatra' ruby web application framework.

Bits and pieces

Let me tell you what this project will be composed of.

Alt Text

Ass you see from the picture above, we have three classes for manipulation of the whole project. They are:

  • app.rb - the main file where we accept requests and redirect HTML content to users
  • movie.rb - Movie class to manipulate the movie object.
  • movie_store.rb - A class where we will manipulate storage and retrieval of movies from the storage file.

Besides that, we have 3 HTML files that we will serve as a response to the client when they request certain data about a movie. Here they are:

  • index.html - Main view file where we will list all movies
  • show.html - A file where we will show info about a single movie
  • new.html - A file that has a form to submit new movies

The last interesting file you might see is the 'movies.yml' file that stores the movies list in Yaml format. YAML is just another simple to write and easy to read data serialization format.

Let's Start

First of all, let me acknowledge you that this is firstly the demonstration of developing a simple web application using 'sinatra' framework and the latter decoration and improvement of the project is up to you.
You will have to install 'sinatra' gem from the ruby gems repository. And yes, it is. Sinatra framework is just a gem.
If you already have ruby installed on your computer, just run the installation of 'sinatra' gem.

gem install sinatra
Enter fullscreen mode Exit fullscreen mode

This will get you the gem and all the dependencies it has.

Movies

What we need first is of course obvious as a sun. We have to create a class that will manage our movies. For this, we are going to write a simple 'Movie' class to store its properties. And the properties will be :

  • id
  • title
  • director
  • year

We actually do not need much of this class. Just, required fields and methods to read them and to assign values to them. All that can be done with a single line like in the screen below.

Alt Text

Storage file

To get a grasp of what a YAML format is and how we store movies in that format, let's take a look at the file content itself. It saves the list of movies one after the other in a key-value format. In other words, we can call it a Hash format. The movie objects 'id' field is used as a key. So ids of movies must be unique and every next movie must have an incremented value of id.
Here is how the yml format file looks like.

Alt Text

Movie storage class

To implement the last part of the project that works purely on ruby, let's start writing the 'MovieStore' class. This class will handle all the operations to store and retrieve the required movie from the storage file. Here are our methods and what they are meant for:

  • find - a method to find the required movie with the given id
  • all - a method to get the list of all the movies
  • save - a method to store the movie object given as a parameter

Here we initialize the class with its constructor and give a name to the file where we will store the movies list in YAML format.

Alt Text

As you have already noticed, it uses 'YAML::Store' class that makes it all available for us. It serializes the given object and converts to YAML format to store in the 'movies.yml' file. And it reads the movie object from the file and retrieves it for us into the application.

MOVIE SEARCH
Alt Text

Take a look at these 3 lines of simple code within the 'find' function. As we have said before, the storage of movies is performed in a Hash format. So we just give the key to search for and it gets us the movie object with that key. The 'id' of the movie is used as a key here.
One more thing to note is that we use 'transaction' method of 'YAML::Store' class. It blocks access to the file when we start using it and releases it only when we have finished working with it. It resembles a process like the locking mechanism in database management systems.

MOVIES LIST

Alt Text

The retrieval of all the movies is accomplished in the 'all' method. It also uses the locking mechanism with 'transaction' method. With the method 'roots', it just gets all the keys, that is all the ids. And inside the block, it returns the movie with the corresponding id. Thus, we get all the movie objects as a list at the output.

SAVE MOVIES

Alt Text

And finally, you can see the method where we save the movie and place it into the 'movies.yml' file serialized in a YAML format.
It also uses the 'transaction' mechanism. Within the block, it tries to get the maximum id within the storage file and increments it to assign it to the next movie to be stored. If it is not available it just assigns 0 as it comes out to be the first movie on the list.
The last thing it does is inserts the movie, given as a parameter, into the storage file with the newly assigned id. And that id is going to be used as a key in the next file access.

Be back soon

Here we come to the end of the first part of our project blog. We went through the main stuff that does all the storage and retrieval procedure.
In the next part, we will look at how we get responses and redirect them to the corresponding view files.
If you want, you can look at the project repository on Github : Sinatra-movies-demo

Do not go too far, dear friends and readers. I will be back soon with the second part of this article.
The second part of the article is here: Part-2
Stay healthy and good luck!

Top comments (0)