Throughout the last month, I have been attending the Flatiron Schools Software Engineering course. We have been learning Ruby and to finish off phase 1, we had to build a CLI using an API. Originally I thought about creating a crypto currency tracker which showed the highest and lowest prices of the day as well as the current value of a selected currency but ran into an issue of it not being upto date with many APIs being hours behind. Eventually I chose to use the Poke API to create a pokedex of the original 151 pokemon from the first generation. Though the project took a while, it wasn't due to it being an overall hard project, rather most of the time spent was finding an API that would work well along with working around ways to get the proper information output through trial and error.
def self.fetch_pokemon
url = "https://pokeapi.co/api/v2/pokemon?limit=151"
response = HTTParty.get(url)
fetch_abilities(response["results"])
end
HTTParty
is a ruby gem which allows you to gain access to outside information and automatically parse it into an array or hash. In this example I took the URL for the pokeapi and parsed it into the response
variable. From there I was able to dig deeper into the hash, removing the unnecessary data that wasn't apart of the array I wanted to gain access to, stored in the results
array.
pry(POKE::API)> response
=> {"count"=>1118,
"next"=>"https://pokeapi.co/api/v2/pokemon?offset=10&limit=10",
"previous"=>nil,
"results"=>
[{"name"=>"bulbasaur", "url"=>"https://pokeapi.co/api/v2/pokemon/1/"},
{"name"=>"ivysaur", "url"=>"https://pokeapi.co/api/v2/pokemon/2/"},
{"name"=>"venusaur", "url"=>"https://pokeapi.co/api/v2/pokemon/3/"},
{"name"=>"charmander", "url"=>"https://pokeapi.co/api/v2/pokemon/4/"},
{"name"=>"charmeleon", "url"=>"https://pokeapi.co/api/v2/pokemon/5/"},
{"name"=>"charizard", "url"=>"https://pokeapi.co/api/v2/pokemon/6/"},
{"name"=>"squirtle", "url"=>"https://pokeapi.co/api/v2/pokemon/7/"},
{"name"=>"wartortle", }
I found it interesting trying to manipulate the data you see in the above clip. Each pokemon has its name which is fine, and then a URL along with it to access any moves, abilities, types, game appearances, etc. This was a common occurrence with this API of creating a constant loop of parsing the information every time you wanted to get a level deeper into information.
In order to do this, I created a new method to clean up the code so it could be edited easier as well as more visually appealing. We can now start creating Pokemon objects. First, we iterate through the array and parse each url of the individual pokemon hashes and finally create new Pokemon objects with new
.
def self.fetch_abilities(results)
results.each do |x|
response = HTTParty.get(x["url"])
POKE::Pokemon.new(response)
end
end
This is the end of the API class. From here we can take the information and manipulate it further through the Pokemon class and finally create our CLI class to display options and information to the user. While creating the Pokemon class I had ran into issues using send
. At this point, completing the project was simply trying to gather a deeper understanding of why my variables had been overwritten through this method which I have previously covered in another post. If you'd like, you can read more about it here.
puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
puts "⣿⣿⣿⣿⣿⡏⠉⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣿
⣿⣿⣿⣿⣿⣿⠀⠀⠀⠈⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⠉⠁⠀⣿
⣿⣿⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠙⠿⠿⠿⠻⠿⠿⠟⠿⠛⠉⠀⠀⠀⠀⠀⣸⣿
⣿⣿⣿⣿⣿⣿⣿⣷⣄⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⣴⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⠀⢰⣹⡆⠀⠀⠀⠀⠀⠀⣭⣷⠀⠀⠀⠸⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠈⠉⠀⠀⠤⠄⠀⠀⠀⠉⠁⠀⠀⠀⠀⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⢾⣿⣷⠀⠀⠀⠀⡠⠤⢄⠀⠀⠀⠠⣿⣿⣷⠀⢸⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡀⠉⠀⠀⠀⠀⠀⢄⠀⢀⠀⠀⠀⠀⠉⠉⠁⠀⠀⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿"
puts "~~~~~~~~~~~~~GOOD BYE~~~~~~~~~~~~~"
Top comments (0)