My Pokemon Selector

Marlon Moreno
4 min readDec 12, 2020

So, for my first Flatiron project I had to make a CLI that would take data from a webpage and display that data when asked for it. For this occasion I ended up using an API that contained the information for all existing Pokémon. And what the CLI will do is ask the user what generation of Pokemon the user wants to see, then it will list the Pokémon in that generation, after that the user can select what specific Pokémon they want to know more about.

First I made a class that handled everything that had to do with the API, when it initialized it would initialize with the API.

Next I needed to use URI.parse, Net::HTTP.get, and JSON.parse to retrieve the info from the API, and to make the information more readable for me and as a result make it easier for me to know where specific info is located, so I made a method for that, which also would do the same with the URLs that were located within the API. Then I realized that the URL I was using was calling upon all of the Pokemon, which make it so that it would take a while for all of the info to be retrieved, so I made it so that the URL would only call the Pokemon that were asked for by changing the URL based on the request.

POKE_GENS = [[151, 0],[100, 151],[135, 251],[107, 386],[156, 493],[72, 649],[88, 721],[89, 809]]def initialize(gen)selection = POKE_GENS[gen.to_i - 1]@url = "https://pokeapi.co/api/v2/pokemon?limit=#{selection[0]}&offset=#{selection[1]}"enddef get_urlsuri = URI.parse(@url)response = Net::HTTP.get(uri)info = JSON.parse(response)pokemons = info["results"]pokemons.each do |poke_info|get_poke_info(poke_info["url"])endenddef get_poke_info(url)uri = URI.parse(url)response = Net::HTTP.get(uri)info = JSON.parse(response)Selector::Pokemon.new(info)endend

Once I finished that first class, I went ahead and made a Pokémon class, which upon initialization would need to make attribute accessors based on the keys each Pokemon URL had, and once they have their values push that into @@all. Thats when I realized a method that I previously thought had no real practical use was actually perfect for this situation, the #send method. What the send method does is it calls upon a method and it will pass in an argument into the method. One could ask, “Why don’t you just call the actual method?” Which is the exact question I made myself when I first heard about this method, but thankfully my cohort lead was able to clear that up for me.

def initialize(info)info.each do |key, value|self.class.attr_accessor keyself.send("#{key}=", value)end@@all << selfend

As we can see in the code above, in line 3, I went ahead and made attribute accessors for the keys found in #info, which just includes all of the information for the Pokemon selected. But now we need to assign each attribute accessor their corresponding value, which is where the send method comes in. Instead of looking inside of #info and looking at all the keys so that we can write down every attribute writer so that we can give them their values, we can just use #send right after we write the attribute accessors in line 4, inside the #each loop, which already calls upon the keys and values inside #info, and we can make it so that send uses the attribute writer just made so that it gives it its corresponding value.

Once this was done I just made a few more methods, #all so I could see the @@all array, #reset_all, which clears the @@all array, and #select_poke, which would take an argument which would show you a specific index of @@all.

def self.all@@allenddef self.reset_all@@all.clearenddef self.select_poke(input)Selector::Pokemon.all[input.to_i - 1]end

After I had started making some methods that would print out specific Pokémon information, but my cohort lead told me it would be better for me to put all of that in my CLI class, so then I decided to start on that.

Like I said before, wanted the CLI to ask the user for the Pokemon generation they would want to see and list all of the Pokemon in that generation, so I made a method called #start that would do this. After that I made a method called chosen info, which after #start was finished running would ask the user to pick a specific Pokemon, and then list the info of such Pokemon. After this the method #continue would ask if they wanted to look at another Pokemon or if they were done searching, and decide to continue or not based on their input. This is a summarized version of my CLI class.

I gotta say, before starting this project I was really nervous. I felt really unprepared and like I had big gaps in my knowledge. But once I got started I actually started having a lot of fun with, seeing that a was capable of actually making a CLI. I can’t wait to see what else I am able to make in the future.

--

--