My Flatiron School CLI Project

Ernesto Ramirez
5 min readJan 8, 2021

The idea of creating a program by myself after only about of month of learning the basics of coding seemed like a nightmare to me. Even though I had resources to help me throughout the entire project timeline, I was still daunted by the fact that I needed to build a program from scratch that was actually functional. After a couple days of brainstorming about what I wanted my CLI to revolve around I decided to make a Pokémon research program, known as the Pokédex. I got all of my data from a public API, PokéAPI, which provided me with absolutely all known information about the world of Pokémon. There were tons of nested hashed within this free database that allowed me pick apart and return all the information that I needed. After building up enough courage I decided to finally start building my program.

Class API

In order for me to be able to get information from PokéAPI I needed to create an API class that allowed me to use the URL of the database and add endpoints in order to return the information the I wanted to.

The first line from the code above represents the base URL from PokéAPI with no endpoints. This URL led me to the front page of the database. The second line includes the endpoints to the URL. So, Pokémon, ability, and move were all endpoints the led to more specifics parts of the database. The last three lines are also endpoints that represent a full page of each specific endpoint. For example, NEXTPOKEMONPAGE leads to a full page, which is a hash, that lists every Pokémon in the Pokémon universe. If you’re wondering how many there are, the last number of the endpoint represents how many Pokémon are within that page. So, there are a total of 1,118 Pokémon listed. This is applies to the other endpoints as well for example, there are 327 Pokémon abilities and 813 Pokémon moves.

The following lines of code call the base PokéAPI URL along with their specific endpoints in order to return unique hashes of information. The first three methods are practically copy and pasted only with a few adjustments that differentiate each method. The first method, self.get_pokemon_endpoint(endpoint), sets pokemon_url equal to URL, the base PokéAPI URL, and then adds an endpoint, pokemon, along with adding a forward slash, for formatting, then finally adds the NEXTPOKEMONPAGE endpoint in order to return a hash that lists every Pokémon that has ever existed. It then calls itself along with the pokemon_url argument to return the information in my terminal. This pattern of adding endpoints on top of URL’s is constant throughout the rest of the API class since it’s a reliable way of receiving specific information. The self.search_endpoint(endpoint, search_term) method allows the user to search specific endpoints instead of only being able to use the automatically implemented methods. The clear difference between this method and all the others is that there are two arguments instead of one. The search_term argument is what allows the user to search specific endpoints and receive their own information. Finally the self.call_api(url) method uses HTTParty, a ruby gem, to call the base PokéAPI URL.

Class CLI

The CLI class is where all of the methods that I created work with each other and create the program that allows you to research about Pokémon.

The prompt method just puts all the options that are available within the Pokedex. It is also the main menu for the program. Let’s see the code that is within option ‘1’.

Option ‘1’ returns a list of all Pokémon by calling upon the get_pokemon_endpoint method from my API class along with the ‘pokemon’ argument. Then character_results gets set equal to characters which digs into the [“results”] array that has the hash of all the Pokémon names. I then iterated upon character_results in order display each Pokémon's name with the display_character(character) method which I will show below.

The display_character(character) method just puts the Pokémon's name by digging into the [“results”] array and then further digging into the hash and then extracting the [“name”] that is attached to each value within the hash. This allows me to get each name and then put it into {name.capitalize} string interpolation. the .capitalize allows the first letter of each name to be capitalized.

These methods are almost identical for the rest of the programs options, since all that I’m doing is pulling from arrays and hashes in order to extract specific information, then iterating within those hashes to extract information, then displaying that information within the program. Below will be screenshots of what the program looks like when it is being run in your terminal.

The Pokédex

Welcome Menu
Main Menu
List of Pokémon
Search For Pokémon Special Abilities
Search For Description of Special Ability
Quit Screen

Conclusion

Despite my initial fear of creating a program from scratch, I really enjoyed every step of creating this project. I received a lot of help along the way and allowed my creativity and ambition to show within the project. Thank you for reading about my journey through creating this project. I will keep working and learning about the intricacies of programming in order to create more ambitious programs in the future.

--

--