Surviving Your First CLI Project
By: Alicia Fasciocco
Tags: Coding, Flatiron School, Ruby, Women In Tech, Seattle

For my first project at Flatiron School, we were asked to build a CLI gem by scraping two levels of data while creating objects (not hashes). On day one, I got everything in my first level scrape working. By day three, I realized I had to refactor all of the code I wrote to store it into an object. After many temper tantrums, two study groups saved my sanity and my project. Here is a how-to list that will hopefully save you from many of the frustrations I experienced.

Brainstorm! A writing teacher once told me to write what I know and love. The same goes for coding. In this case, I went with ice cream. The idea was to scrape the top ice cream shops in NYC and return reviews about each of them. However, I couldn’t find a site that had reviews linked on the same website to qualify as a second level scrape. My teacher, Lantz, suggested The Infatuation, and I “settled” on a list of Seattle’s Top 18 ice cream shops (Salt & Straw is my favorite anyway)!

Watch videos, but know they are not perfect. I watched videos from a few different teachers at Flatiron. The way my chosen website is structured meant I couldn’t live and die by the way they scraped data. This led to a lot of writing and deleting code. Don’t do what I did. Learn from them, but don’t assume you’ll be able to extract your data the same exact way. Avi’s set up video is a good one, but know you probably won’t be able to scrape as quickly as he will.

Local environment >>>. Setting up a local environment felt overwhelming…until I did it. Doing so a lesson or two before the project helped tremendously. It is pretty easy to get the hang of — just use this set up guide. Those on the Learn IDE had a few errors you can avoid by taking the plunge.

Install Bundler Gem. No need to start from zero. The Bundler Gem will get most of your skeleton files together. Use this guide https://bundler.io/v1.12/ and type the following into your terminal:

bundle gem name_of_your_gem

For nice-looking filenames, I suggest using underscores in your gem name.

Shebang time. Create an executable file in your bin with the name of your gem. This file will help the program figure out it needs to run in Ruby.

Chmod. You may see a few “permission denied” occurrences in your terminal from here. Typing ls-lah will list all files in your directory, then you will type chmod +x file_name to “unlock” the gem and see it just as a user would!

Create an environment folder in your lib. This is where you require all of the things. See above where we added the environment file that didn’t exist yet to our bin/ice_cream executable? Now, your entire directory will have what it needs.

Time to scrape. Once everything is set up, it’s time to work on scraping your data. This is the fun part (read: the part where all of the errors you couldn’t predict will pop up). I ran into a few issues. Historically, I am not very good at asking for help, but this experience has taught me to abandon that mentality. Don’t wait too long to ask for help or you will lose hours and hours to an error that just needs a second set of eyes.

· Don’t Fear Dupes! One of my ice cream shops, Matcha Man Ice Cream, was returning twice. After looking through my notes, I remembered the .uniq method. Initially, that code looked pretty awful, but it worked. After some refactoring, things started making a lot more sense.

What it used to look like:

What it looks like now:

· Uh oh, a unique parent class. I knew this problem would creep up again and it did. During my second scrape, Matcha Man began to return an empty review. After some digging, I realized it was because the class in which the p tag was nested was different from all of the other shops. In addition, it wasn’t returning ‘nil,’ but an empty string. After some input at a study session, I was able to write the following line of code to get everything in working order.

A lot of unexpected errors will come your way, but you’ve got this! After you’re finished, you might even challenge yourself further by using fun add-ons like the colorize gem like I did. It makes things just a little bit prettier!

Take a look at my GitHub repository here!