Building My First CLI Ruby Program
By: Robinson Regalado
Tags: Programming, Flatiron School

Over the past week and a half, after catching up on a few lessons I had backlogged, I was able to start my very first CLI Ruby program from start to finish and I feel very proud and accomplished! In the beginning of 2019, I tried to self teach myself how to code and after 3 months I couldn’t get anything to work. After a little over a month, I am very happy with the progress I made and having built my own program from everything I’ve learned.

Since I wanted my first program building experience to be fun, I chose a topic I truly love, food! I built a Top 100 NYC Restaurant CLI program that shows the top 100 restaurant recommendations from Timeout New York and, when you input the number next to the restaurant you are interested in, it returns the food type and a review by someone about their experience at the restaurant that comes from the Timeout webpage about the restaurant.

It didn’t take long for me to get stuck and confused about how to start my program and it wasn’t reassuring that I had issues simply developing my environment and dependencies. When I finally understood the purpose of having a centralized environment that loads all my dependencies and calling them during each class that I built did it click that developing one environment that you then call on makes life a lot easier. I was confused as to whether I would need to require nokogiri or pry in certain files since I would be calling scraper methods but having everything loaded before hand just made life simpler.

I then began building my command line since I had a general idea of the layout I wanted my program to follow. My #call method was going to go down a list of methods where the user will navigate from displaying the restaurants to exiting the program. I first defined my #scrape_data method which was simply going to cal on another class that would be responsible for the scraping. My #menu method is where the fun starts. After scraping the data I needed, I needed to receive an input and I need to turn that input into an integer and return the corresponding integer with the information about the restaurant. This would be fulfilled by a second level scraper so I just inserted my “puts” for the information I wanted returned. The logic it took in left the user with the option of entering a number from 1–100, see the list of restaurants again or exit the program. So far so good!

Keeping up with the positive vibes and good track work, I then moved on to the restaurant class which would be responsible for defining the information I would get from the scrape. The information it would return will be the name, type, location, description and URL. It would initialize with a name and have a method to save all that it scraped. Simple and clean.

My scraper is where the bulk of my time went to and not only trying to find the right parts of data to strip but just trying to present and display the information in a way that looked good to the end user made everything harder. Since the top 100 Timeout list was a list, I knew I would need to look for some sort of table like contain that held all this information. Unfortunately, I had a lot of problems stripping only the data I wanted because there were a lot of shared class names that were responsible for other displaying other data. There were links to other lists that you could click on and the formatting were the same and kept pulling that data in an array. When I was finally able to narrow that information down to just restaurants, it returned the data in a weird slanted formatting and I had no way of centralizing to. I asked Lantz (my cohort lead) for some guidance and pointed me to the &:text operator that helped clean up the formatting.

After that headache, I was able to scrape the information I wanted much easier and didn’t run into the formatting issue I had before. I made sure to return all my data into an array and once I had all my scrapes in, I iterated my data .with_index which allowed me to iterate over all the data I had over the other arrays of data. I updated my command line file and after a few tweaks I was able to scrape and present all the information I needed!

My last step was being able to access data from the individual restaurant’s website and pull the descriptions and type of food. Using the url scraper I got from the top 100 list, I was able to scrape the code that governed all the individual restaurant pages so I just needed to put those together with the main timeout URL and create new scrapers for the new data. I would then call on this new scraper every time a valid input was entered and send back the corresponding information. The descriptions followed a similar formatting issue but I then found another handy operator which deleted all white spaces (gsub(/\s+/, “ “) but looked much cleaner. And after a lot of caffeine, tylenol and grit, my first CLI program came to life!

Although I do think the website I chose was on an easier side since I didn’t need to install any APIs to scrape data, I am happy with the progress I made. There