DEV Community

Cover image for CLI For Gamers
Alexia
Alexia

Posted on

CLI For Gamers

Phase one is coming to a close and we were tasked with creating our own CLI by using and API or through scraping. I was excited I knew exactly whet I wanted to do once my cohort lead sent out a list of API's we could use. I was a little stumped on how to get started but after watching some videos and messing around a bit first I figured out what all I had to do. Though my original plan never came into fruition.

MY API

The first thing I did was figure out what API I wanted to use. The moment I looked at the list my cohort lead sent out I knew what I wanted to do. I choose Pokemon! I was super excited I had started planning out what I was going to do. I started coding up some basic functions just to test the capabilities of the API. This is where I run into my first problem. The original API I chose was Pokemon TCG and it just was to much data that I didn't understand how to manipulate so I moved on. I was still in the realm of Pokemon so I tried to use PokeAPI but it wasn't giving me the data I wanted. I wanted and API that could give me stats on Pokemon cards. When using PokeAPI it gave me the Pokemon but it was also giving me their URl and I dont know how to pull that data out of that URL.

So I threw in the towel and started over. It was hard I was set on having a Pokemon CLI so it was difficult to find a new API. Finally I settled Free to Game I played around for a bit figuring out what data I wanted to present to the user. Did I want to show PC games? Did I want to show MMORPG games? Did I want to show MMOPRG games for PC? I spent hours trying different combinations before I settled on just MMORPG. So I chose to have the user select a game and have a selection of data be returned for that game. Next was creating my files.

Creating My Files

This part thankfully wasn't to hard for me. I had attended a lecture where we went over manually creating the files and linking them up. The only trouble I had was when going over some of the other videos there was a bunch of extra files I had only seen in labs. I panicked! I didn't know how to fill these files? Why were they there? I reached out to my cohort lead and she told me it was okay to not have all those extra files. I breathed a sigh of relief. In the same lecture that I attended it was also explained what exactly each file did so after that little panic I was set. Time to get down and dirty typing out my code!

Actually Coding

I had a few things set up while I was playing around and figuring out my API so it was just branching off of that. I jumped right into coding for my CLI. I was hard coding everything to make sure it ran smoothly as I followed along with the videos. So I already had the bare bones of my code before I added in my API and personalized the code towards the API. When I got to adding in my API and calling upon that data that's when I had all my issues.
Originally while I was hard coding I had initialized like this

def initialize
@title = title
@short_description = short_description
@release_date = release_date
@publisher = publisher
@platform = platform
end

It didn't work when I plugged in my API so I had so scrap it and ponder. Thankfully my friend was willing to help me out and she explained Metaprogramming to me. It is essentially code that writes other code, it pulls key/value pairs and allows a more effective code.
So my code ended up changeing into

def initialize(game_hash)
game_hash.each do |key, value|
self.send("#{key}=", value) if self.respond_to?("#{key}=")
end
save
end

This ended up making my life so much easier.

When I though I had finished, besides making it look nicer I had my sister test it out and she gave me some pretty good feedback. I had written my code to only accept first letter capitalized input because I was always capitalized everything. She didn't so she was constantly getting errors. So I decided to alter my code so that it didn't matter, you could type in all caps or lower case and it would still respond. I did this by adding .downcase to the users selection.

After that it was just adding a bit more to make the user side present better. I ended up adding on more data that the API provided for each game. It went from five pieces of data to eight. I added in the colorize gem to add color and I even added in some ASCII art to make it more user friendly. Soon I finally wiped my brow and decide that my CLI was done.

If you would like to check out go right ahead at Phase One Project

Top comments (0)