Let’s talk CLI Project.

Cissica Gonzalez Martinez
5 min readNov 21, 2020

ft. Ruby

A walk through of my planning for a recent project for The Flatiron School. Recently I was assigned a project that required me to showcase my understanding of the Command Line Interface with Ruby. In the beginning it was intimidating and a bit overwhelming, I suddenly forgot everything and nothing made sense anymore. So when thinking about where to start, I decided to create a visual.

My classes and their responsibilities.

This visual helped me determine how to start my build for this project as well as remember the individual responsibilities that belong to each component of this project, therefore making it easier to maintain separation of concerns.

SoC or Modular Program

Separation of concerns is a software architecture design pattern/principle for separating an application into distinct sections, so each section addresses a separate concern. At its essence, Separation of concerns is about order

I started by creating 3 classes. A scraper class, a cli class and a blogs class. My scraper class was responsible for scrapping my chosen webpage using the gems ‘Open-uri’ and ‘Nokogiri’.

Scrapping

Ruby has an amazing web scraping gem called Nokogiri. Among other features, it allows you to search HTML documents by CSS selectors. That means if we know the ids, classes, or even types of elements where the data is stored in the DOM, we’re able to pluck it out.

Then the information is used to create and store objects. Which brings me to my blogs class, and it’s responsibility: creating the objects, storing the objects, and making them accessible.

And finally, the cli class is responsible for talking to the user and returning information according to the user’s input which is where the methods in our other classes become very helpful. After creating the skeletons for these classes, came all the important questions to fill in the blank between my class and end.

For this I needed a quick recap on a couple of things, so let’s recap.

What is self? A keyword that gives access to the current object depending on the context.

Class method vs instance methods? When to use what?

A class method allows the class itself to have functionality, while an instance method allows functionality in one instance of a class at a time. I learned through lots of examples and reading that instance methods are best when wanting to do something in particular pertaining to a specific object at a time, while class methods are useful when wanting to access the entire class itself at once. The use of each really depends on what you need. Do I need to access every object in my class? If so I need a class method. Do I need to print the details of a particular object? If so an instance method is the way to go.

Recap ☑️

So now that i’ve cleared up my doubts, I have created my classes and they seem to be communicating effectively. Let’s walk through it.

To start I run bin/executable file and once I press enter my first method will be called to output a greeting.

Beautiful! Our greeting, now you may not be able to see it but something else is happening besides the printing of these words. Our initial method also invoked a method from the scraper class, which is responsible for scraping the webpage we will be getting our next set of information from. You see a greeting but the magic of scraping is also occurring.

After entering ‘yum’ I am now given this beautiful list of recipe titles. This is the result of the scraper, cli, and blogs class communicating effectively. How you may ask? Well, first off remember how I mentioned before the magic of scraping? The scraper class worked its magic and scraped the information I asked for and then called on the blogs class to create objects with that information and store it. And finally our cli class called on the blogs class to give it the titles of each object created, assign each title to a number and output a message prompting for a number in order to output more details on a specific recipe.

We chose a number and ta-da! We are now presented with this object’s particular attribute details. The title, a summary, the ingredients and the recipe. The magic behind this is happening because our cli is calling on our blogs class to give it that particular information and if the attribute information has already been scraped then our cli will simply print it from the blogs class, however if the information cannot be found in our database then the cli will call on the scraper class to scrape that objects’ corresponding page for the information needed.

And finally we simply exit the program by entering ‘exit’ and we are sent on our way, which our cli takes care of by simply printing out a message and executing exit.

Well that is it folks. If you made it till the end with me, thanks for reading!

--

--