MoDay App

Murat Ogulcan Sahin
3 min readFeb 7, 2021

MoDay is a command line application that was built in Ruby language using object oriented programming (OOP) model. This app’s main purpose is to help you to pick a movie when you feel lost in movie streaming apps with endless content. MoDay simply gives you a genre list and then narrows the movie options based on your selections and entries throughout the app.

All of the objects in the app (genre, movie star, director, movie) abstracted with creating separate classes, and their attributes for each one of them. In order to link these separate classes, envrionment.rb file is created. This file creates an object orientation environment for our app by requiring all libraries and other files that were created within the directory. This will prevent us to require all of these files on each other and avoid uninitialized constant NameError. However, the order of requiring files in environment file is important. For example:

MoDay environment.rb file

As you can see below, Director and Star classes inherit from Person class. Therefore we should require Person class file before requiring Star and Director class files in our environment, otherwise we would get an uninitialized constant Person (NameError) error.

Person class is superclass of Director and Star classes.

While we’re using the app, web scraping and API calls are happening under the hood. In order to make these possible, ‘open-uri’ and ‘nokogiri’ gems are used for scraping, ‘httparty’ gem is used for API requests and parsing the response.

Let’s have a closer look to these gems and their roles.

Scraping Gems

#scrape_genre_names method is scraping imdb top rated movies web page and returns array of genre names.

Open-Uri is a ruby gem that opens http, https or ftp URL as if it is a file. We can make this possible with its #open method. This method takes an URL as an argument. The opened file will be useful for the next step of scraping which involves our next gem, nokogiri.

Nokogiri gem parses opened file and make capturing the data easier for us. Its #css method simply targets the webpage’s HTML methods using CSS selectors by passing those selectors as a string argument and returns the targeted data in a well structured nokogiri array. That will allow us to spot and access the data we are after via array methods.

API Gems

#get_movie_by_id class method is taking a movie imdbID as an argument, checking if it is stored. If not, making an API call and returns a movie that is instantiated with the API data.

HTTParty gem is one of the API gems that lessens the steps of obtaining data procedure. This gem sends API request, gets a response and parses results. Its #get method takes an URL string argument, and returns the API data.

Getting API response might take some time based on various reasons. If the user is not notified about API progress, they might think our app is not functioning. Therefore you would like to inform the user by displaying a progress bar that indicates our app is working. Thankfully, there are progress bar gems. In this app, ‘tty-progressbar’ gem is used. Basically, after setting the bar variable, use its methods (advance, stop etc.) where your API operates.

bar variable is set and #advance method is called in an iteration where API method functions.

As a conclusion, important point of scraping and making API calls is storing the obtained data. This will make our code working faster and keeps our data providers less busy and also the API we are using might not be free. Therefore less request will return less cost for our app. OOP makes our code more eloquent, dynamic and flexible for future improvements also makes fixing bugs easier.

--

--