CLI DATA GEM PROJECT FLATIRON

CLI Data Gem

A tale of frustration and perseverance

Andrea Jasper

--

Image of a woman standing on a mountain top.

I entered my first project like a noob gamer dropping into a professional PvP (player-vs-player) match, I got my @$$ handed to me.

I naively thought it would be easy; that being diligent in my coursework had somehow made me a master coder who could complete the project in a day, two tops.

I couldn’t have been more wrong.

That lovely thing called karma slapped me in the face with a hard dose of reality. Coding is hard. Coding is frustrating. Such was my experience with my first project: a Ruby CLI Gem.

Project Requirements

While the instructions are about as interesting as a toaster manual, it’s important to understand the requirements of the project before we dive into the code.

  1. Provide a command-line interface (CLI) for users to interact with.
  2. The CLI application must provide access to data from a webpage
  3. The data provided must go at least one level deep. A “level” is where a user can make a choice and then get detailed information about their choice.
  4. Use good object-oriented(OO) design patterns. You should be creating a collection of objects, not hashes, to store your data.

The Process

With the requirements in hand, the next logical step would be planning, right?

Wrong!

I watched Avi’s video tutorial for building a CLI gem called Daily Deal first.

You may be wondering why I chose to watch the video first instead of coding along or starting on my own. The answer is simple:

I struggle with imposter syndrome which can cause me to feel overwhelmed by even a simple project.

To mitigate this, I opted to watch the video first, take notes and document my ideas. This allowed me to see how other developers might approach their work and simplify my own project.

Here’s what I decided on:

Outline

  1. A command-line interface for latest rails posts scrapped from the Dev.to website.
  2. Users are greeted in the CLI and asked to select an article. The article list displays the following: post title and date posted
  3. Users select a number associated with the Post they’d like more information on.
  4. The desired post will display with a title, date, author, and post content.
  5. The post content is followed by a prompt to go back to the main “list” or “exit” the CLI.

Initial Setup

I took Avi’s recommendation and used bundler to create a scaffold for my gem.

bundle gem latest_rails_posts

Hint: use snake_case when naming your file

Setting up my files

/bin directory:

The first step is to call my executable file by placing it in the bin directory with the use of require_relative “../lib/latest_rails_posts”. This allows the gem to load all the files in the lib directory.

Hint: the file must begin with #!/usr/bin/env ruby so that the program knows to interpret the file as Ruby.

Image of the file structure for the bin folder.

/lib directory:

Lib contains the file “latest_rails_posts” which only requires my “config/environment” file. I opted to have a separate config directory out of habit.

Lib also holds the sub-directory called “latest_rails_posts” where we’ll find my main project files: ‘cli.rb’, ‘post.rb’ and ‘version.rb’.

Image of the file structure for the lib folder.

Setting permissions

This step allows us to set the app to go through bash (how a user would run it when they install the gem), instead of through the Ruby interpreter.

  • First, you’ll need to cd into your bin folder
  • Ls -lah will display all the files in the current directory
  • Chmod +x latest_rails_posts
  • Users can now start the gem via “./bin/latest_rails_posts

Gemspec

Bundler, you wonderful, magnificent tool! When you create your gem with bundler, it generates several files for you, like the gemspec file.

Not only does it create this file for you, but it also provides helpful prompts to help fill out the required information to ensure the gem functions properly.

Obviously, you want to go through this file and update the information, and include any necessary dependencies.

Image of the gemspec file to set dependencies for the project.

The Code

Posts.rb

#self.scrape_latest_posts and #self.scrape_post_content

This method does the heavy lifting. It scrapes the Dev.to website and creates a headline(title), author, date, url for each new instance of Post. You know, all the important stuff that will display in the CLI.

The scrape_post_content pulls the post content from the next level of the website; in this case, the next page.

Image of code that scrapes the Dev.to website.

CLI.rb

#call

Here, the call method initiates the CLI welcome message while scraping the latest posts from the Post class. It also contains the “list” and “menu” methods which will allow users to navigate through the CLI.

Image of the method code that calls the greeting and lists of posts.

#menu

The menu method is triggered once the scraper has retrieved the posts. It displays the list of posts first, then requests a number be selected before finally displaying the full contents of the post: title, author, date, and content.

This is where users will have access to the “list” and “exit” options.

Image of the menu method code that allows users to interact with the CLI.

Learnings

Difficulties

Ironically, deciding on a topic was the hardest, most frustrating part of this entire process.

My husband and I love to travel, so initially, I wanted to create a CLI to display the top 100 hotels in the world by rating. This proved to be way more difficult than I anticipated as the sites I wanted to reference didn’t have enough unique classes to call the information properly. Ugh!

After 4 days of trying to make it work, I opted to switch topics and decided to scrape the Nasa site for the latest posts on space. I meticulously updated my code only to be met with extreme hostility (at least from the site).

Several baffling hours later, I began to wonder if there was a problem with my code.

To test this, I viewed a friend’s repo (also a Flatiron student), and used the site they had scraped since I knew their gem works. More annoying updates and viola!

Success!

With the knowledge that my code is, in fact, working, I switched gears again and decided that the final version of my gem would scrape the Dev.to site. I figured a lot of developers write on it, why wouldn’t they allow you to scrape it? It was a wild theory, but it worked out in the end.

After another hour of updates and adjustments and I finally had a functioning CLI gem.

Takeaway

Sometimes, you just need to walk away. I know, it sounds counterproductive, but hear me out.

Like Stoffel the honey badger escape artist, I was determined and resourceful, just not in the right ways.

For 5 DAYS I stubbornly banged my head against the wall, unwilling to back down from the challenge.

When the weekend hit, I was so frustrated, I vowed to avoid my computer at all costs. I spent my time on favorite hobbies, giving my subconscious mind time to mull things over and make better sense of what was happening.

After distancing myself from the project, I was able to approach it with a clear head and possible solutions.

I realized that I wasn’t giving myself enough time to thoroughly process the problem. I was haphazardly bouncing from one solution to the next without any real thought behind them.

In the end, taking a break from the project was the best solution.

--

--