DEV Community

RJasonClarke
RJasonClarke

Posted on • Edited on

My Flatiron CLI Project

First of all I want to say that this project has been a huge learning experience for me. Between seeking out information that was not covered in class and problem solving on my own I can truly say that I have gained a lot of confidence in my programming and it feels AMAZING.

After watching the walkthrough provided on the CLI project page I began to ponder my own idea for a scraper. I knew what it needed to do but I had to make it my own without being to similar to the examples in class. I browsed an article or two and happen to come across a top video game list and thought to myself "PERFECT!". It had each game title listed individually and even provided an explanation of each one and Metacritic rankings right below.

Now that I had my url for the scraper I just had to do a little pseudo-coding to give myself an idea of what steps I need to take to make my idea a reality.

  1. Show user the full list of games
  2. Give user the option to see more details
  3. Loop back until user exits

After following the directions in the walkthrough I followed directions on how to generate a directory for everything I required. I started with my binary or "bin" file since that would be the first part of the app that a user would be interacting with. Upon opening the bin file I had to add a shebang (#!/usr/bin/ruby) that gives the shell the path to the ruby interpreter.

After working on my CLI file for a while I decided to save the progress to my repository. Boy was I not ready for the amount of hoops I had to jump through to get that working properly. It took me quite a while but I figured out how to update my repository and continued with the project.

Next I moved on to the star of the show, the scraper. This too caused me a bit of trouble at the start but after researching how to use open-uri I discovered that Nokogiri::HTML(open(url)) did not work the same as it was shown in the walkthrough from the project page. I simply had to add "URI" before open and it worked properly.

When I finished getting the url with open-uri and parsing it with nokogiri I got to dig around inside of the html and find the exact tags I needed for listing the games. Inside the HTML I found that the titles, ranks, and descriptions were all stored under the class ".slide" so I grabbed it and wrote an each method to store the information I required into an array I had stored in a class named Game.

doc.css(".slide").each do |site|
game = Game.new
game.title = site.css("h2").text
game.critic = site.css("p")[0].text
game.user = site.css("p")[1].text
game.description = site.css("p")[2, 1].text
end

I stored each individual part of the .slide class into a different variable so they could be be returned individually when called on. After getting the results from the array I had to use a reverse method on the array since the games were listed in a descending order on the site.

After that was finished I simply had to add the methods I had created to my CLI so they could return the proper information that the user was requesting. This was by far the easiest part of the project since its pretty much just giving the user options, taking their input, and giving results.

A few days later I was able to polish up my app and update my repository with its last commit! As I said before, this project was a real benefit to my programming and confidence. I look forward to starting the new project and seeing what I can learn from its challenges!

To add to my post, I got to learn the importance of return values and more about the each and map methods after my project review.

Return values held more value than I had previously believed. After using "puts" for nearly everything within my app I had completely forgotten the use of return values. The reason I bring up puts is because it is used only to show output within the terminal for problem solving purposes while return is primarily used to store its output within a variable for further use in an application.

Each and map have always been those two methods that trip me up at every turn so I went back to get a deeper understanding on them as well. The each method will always return the same return value.

For example, when I use this array in the method below it will always return the same values with absolutely no changes after.

array = ["burgers", "fries", "pizza"]

def food(array)
array.each do |arr|
puts "I love #{arr}."
end
end

would give the result

I love burgers.
I love fries.
I love pizza.
=> ["burgers", "fries", "pizza"]

Map on the other hand would return an entirely different return value. If I were to change the above code into a map method and remove puts my return value for the array would be

=> ["I love burgers.", "I love fries.", "I love pizza."]

In conclusion, a return value and the puts method are two entirely different things. Also, each and map serve almost the same purpose when it comes to iteration but have totally different return values. Each returns it's original values while map returns new values.

Top comments (0)