CLI Data gem Project

DEVELOPMENT

My latest lab project on learn.co required writing a command-line Ruby gem that scrapes HTML to retrieve data and display it for the user. I thought it would be fun to use a scraper to retrieve a list of a strategies on how to always win at the game connect 4. I had been in limbo for a while while transferring from the full-time cohort to the part-time. Thanks to that, I had plenty of time to play around with the things I had been learning and build a connect 4 game. after that was done, I started building a scraper using Nokogiri and Open-uri to teach people how to play the game I had just built and play it very well.

After several days of research and coding the initial version of my project is finally complete! There were moments of confusion and frustration, as my instructor, zdrake, knows very well, but it was satisfying to figure out solutions and create a working CLI gem.

After going through the whole process, I am amazed to see how easy it is to publish and share working code with RubyMine on Github.

Starting the Gem

After watching Avi’s video tutorial and some experimenting, I started my project by using bundler to create a rubygem skeleton:

bundle gem Connect4

Then I had to decide on how I wanted to structure my application. I wanted my scraper to interact with the game so that you could play after reading or go back to the tips after finishing a game. to begin this task, I tested out with how I was going to scrape by getting the objective of the game from wikipedia and printing that to the CLI using this method:

def wiki
  html = open('https://en.wikipedia.org/wiki/Connect_Four')
  doc = Nokogiri::HTML(html)
  puts doc.css('.templatequote p').text.strip
  puts ' '
end

It occurred to me that instead of listing a all the tips in detail, my application needed to make a brief list to select from. Foolishly, I started scraping each tip individually without reading the full requirements of the project, and built custom scraping methods for each tip. A mistake well learned. Building the iterator to go through all the tips on the site had proven to be much more efficient than coding each tips custom method individually.

I needed a way to store the information about each individual tip, but also give the corresponding title. Since there were several different tips I wanted my application to display I decided to create the def get_info(strat_object) method in the Scraper class that would store each tip as an instance of a Strategy object in the add_step array.

Next I needed my application to actually output instructions and tips to the command-line and retrieve user input of the tip they wanted to see. My program still didn’t do anything because I needed to convert the user input into a more detailed scrape and parse the data on wikihow. After playing around in RubyMine, I knew the commands I needed to get the details, and I added the code to list the data. I continued to write code to retrieve the list and get details about the tips. I started making git commits along the way.

I ran into a good bit of trouble after trying to install my gem on my system. At first I thought it was because I was somehow not installing my gem correctly but after installing the daily_deal gem on my system I realized it was because I needed to fill out my gemspec file. I encountered my next problem when I tried to publish my gem to rubygems. I still had code in the gemspec for a push host whitelist.