Well met! Come take a seat and I will tell you how I created my CLI Gem Project and somehow got through it. Fair warning it might be very lengthy. I haven’t mastered the art of being short and concise with my words. I will now try and explain my thought process on how I created my CLI Gem. (Note: I already see that there are some issues I’m having with my gem so I will be getting help on it before my one on one on Oct 16)
The quote above has been something of my mantra with my recent injury, my change in career and other personal things. It has come in my head the entire time I was working on my CLI Gem Project. I have constantly doubted myself since the second I started my coding journey, every step of the way, and don’t foresee me changing that for a while. I tell you this with complete vulnerability and hopes that maybe someone else feels this way too, and to know, you are not alone!
To make this a little easier I’ve made a sort of outline on how I went about my code:
- Figure out what I wanted to do and created an outline
- See if my website was scrapeable
- Built the readme, gemspec, spec.md, version.rb, bin files: console and setup
- Built the three main rb files — scraper.rb, game.rb, and cli.rb
- Built the config file and the bin file run
Here we go:
First Step: I, being a video gamer since I can remember wanted to go straight to my roots and do something that I thought would be practical and useful. I realize that probably nobody is ever going to use my CLI Gem but I still wanted to make it as if people would use it. I scribbled my thoughts in my notebook for the basic outline on how I wanted my CLI Gem to work. I know for me personally, and for other fellow gamers, we always want to know what the newest games are and most importantly the review and the review score from reputable websites. This assures us that we know exactly what the game entails and if it’s worth the money and time to buy it. Most people don’t know how expensive brand-new games are! They start at $59.99 and can go up to $200.00 if you get any sort of collector’s or special edition. By reading a review, and seeing the score you can potentially save yourself money! I’m all about that. I know that I trust IGN and have from its beginning. They give fair reviews, even to games that I love but they don’t — I see what they say and understand where they are coming from and appreciate that someone can review something and not be biased. And in that moment, my CLI Gem was born! I changed the name many, many times, and finally decided on “Top Video Games Cli: PC Master Race” I could have done all games across every console, but for the sake of me being a) not super confident in what I could do and b) thinking it would be more complicated I decided to stick to strictly PC Games (although I have an Xbox 360 and a PS4, and do love console games!).
Well that is great and all, but what did I want my actual gem to do? I scribbled it down on paper and for the sake of my future employer, and anyone else that might be reading this, I do not want to show you my quick messy hand writing — I assure you I have nice hand writing when I take my time! I thought by writing it out (and getting a suggestion from someone that it would help to write it out) without any code would be a great outline for me to stick by. I have changed things in my final product, but wanted to show where it was at the beginning to now.
Copied from my notebook:
First message from computer: “Welcome fellow gamer! I see you’re curious about the top reviewed games! Well, you’ve come to the right place.” Second message from the computer “Level up! Let’s begin. Please enter a number between 1–25 to see what’s been reviewed”
User inputs: a number between 1–25, for my example I chose 2.
Computer outputs: Name: game name Genre: game genre Review: game review score (1–10)
“Would you like to see another game? If so press 1, if not press 2”
User input — either 1 or 2. If 1, the program will loop to the beginning, if 2 the program will close.
If they choose 2, the computer will say “Thank you! Come again!”
Now that’s a very, very, very rough draft. And I’ve ended up changing a few things to the final project like adding in a description of the game because that is valuable information, and that the user presses either Y or N not 1 or 2 at the end, among other things.
Second Step: Now that my outline was out of the way, I was hoping the coding God’s that I IGN would be a site that I could scrape. I read other people’s blog and have heard others in this project realizing that the website they wanted to use didn’t work so they had to change their website and even sometimes changing their game plan. I really didn’t want to do that. At all. I checked the website link, and was pretty sure (and happy) that it was able to be scraped. I asked someone who I trust to check the site and see if I was correct. And low and behold, I was! See the gif below to see an accurate view of myself when I realized I could scrape the first website I picked. (I’m not as limber as Hugh Grant, with my ankle, but you get the idea!)
Third Step: I re-read the directions and saw that I had to create some basic files on my file tree. I followed along, and looked at other people’s projects including the world’s best restaurants cli gem to see what I needed to do. It was probably the easiest part of the project — other than naming my gem!
Fourth Step: And here comes the hardest part for me in this whole project. The part where you have to test yourself and build your entire project. I began sweating immediately internally and externally! I thought “What, how am I supposed to do this without instructions, or files already created for me!?” Well, this is it I had to just jump in like when you are taught to swim by being thrown into the pool. I asked a few people to give me ideas on how they began their project and the majority said that starting in the scraper file would be the best — because you can’t do anything without scraping the data! I will spare you my step by step for each file, because that would take a whole year and I don’t have the time, nor do you! So here are the key factors of the scraper, game and cli.rb files. scraper.rb: Got Nokogiri set up so the entire website could be scraped. I needed to go deeper and find the specific game list, I found it at “.gameList-game” that tag shows all the games from 1–25 on the website.
Then I had to define a scrape_game_index method so that the computer would output the game information from the scraped data. This is where I decided that I wanted to include the game description into my CLI. My method is below with my notes. I needed the game_name, the game_genre, game description, and game_review. def self.scrape_game_index self.page.each do |game| game_name = game.css("div.game-title h3 a").text #gets the name game_genre = game.css("span.game-genre").text # gets the genre game_description = game.css(".game-details").text.split(" - ") #only outputs the description game_review = game.css("span.scoreBox-score").text #shows the score
But I wasn’t done! Now I had to create a new game so that it would output the correct information. Therefore I created Game.new — a capital G because it is a class. Inside of Game.new I needed to call the game_name, genre, description, and review. After that was done, my scraper.rb was complete. It sounds like it took a quick time for me to complete but that took me a full day on Friday. Finding out what exactly you need to find for the specific things i.e. game_name was tough. game.rb This file was a little bit easier in the way that I didn’t have to scrape, but I could use my OO Ruby skills and know from my last few labs/projects that I knew what I was doing. Telling my mind that I knew what I was doing vs actually doing it was very hard. I had to create the class Game with attr_accessors of :name, :genre, :description, :review. Then I needed to create an array of the list of every game object. And the last part was two folded — I needed to initialize my accessors so that I could call them. Then on to my cli.rb cli.rb This was challenging for me. 100%. I wanted to scream at myself yesterday and I felt like crying. However, if I did it, you can do it. I had to create my class of Cli and go from there. I defined call — so that we can call the program. Then I wrote my greeting to the CLI gem and wrote start so that the program would start. But, start hasn’t been defined yet, so of course I had to define start! That began the second output of what the computer will say to start the program. i.e. “please enter a number between 1–25 to see a game” then I needed to use user = gets.strip.to_i so that when the user inputs a number it can be turned into an integer and would work in the program. After the user enters in the number, I chose to print the games, I needed the name, genre, description and review to output but only the specific one, and not all at one time therefore I did game = Game.all[input-1] which calls the array to only get one game at a time. After that, I wanted the computer to say “Would you like to see another game? Please enter in either Y or N.” I wanted to then make it so, if the user pressed “y” then the program would start again, but if it pressed “n” to exit the program. Below is what I did — with my notes!
input = gets.strip.downcase #user types in a y or n lower case if input == "y" start #will start a new game! else puts "" puts "Thank you! Happy Gaming!" exit end end
Then everything was set. I couldn’t believe it, I asked a friend to double check to make sure I didn’t have to do anything else and they said I was okay to move forward and get to the end of it.
Fifth and LAST step: Finally — thanks for sticking with me. After all of the files were done, I had to build the config part so that it would configure and work. I then had to build the run file in my bin folder so that the program would run. This was easy and quick, I don’t think I should go into it.
But, then…my…CLI gem worked! Here is a screenshot of it in action:
All in all, this was a great experience and I can’t believe I am done. Stay tuned to see where I end up next week, or the week after.
Originally published at amandarebeccacodes.org.