Created
February 12, 2016 16:02
-
-
Save biglovisa/436df6d27c14631cd89b to your computer and use it in GitHub Desktop.
pokemon-cli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "net/http" | |
| require "json" | |
| require "pry" | |
| class PokemonService | |
| def pokemon_information(info) | |
| path = "pokemon/#{info}" | |
| send_request(path: path) | |
| end | |
| def next_evolution(info) | |
| pokemon_information(info)["evolutions"] | |
| end | |
| def types(info) | |
| pokemon_information(info)["types"].map { |type| type["name"] }.join(", ") | |
| end | |
| def evolutions(info, collector = []) | |
| response = pokemon_information(info)["evolutions"] | |
| collector << response | |
| response.empty? ? collector.flatten : evolutions(response.first["to"].downcase, collector) | |
| end | |
| def pokemon_for_type(type, type_2 = nil) | |
| # iterate 718 times over the pokemon and select the pokemon with matching type | |
| collector = [] | |
| [*1..718].each do |i| | |
| pokemon = pokemon_information(i) | |
| collector << pokemon["name"] if is_of_type(pokemon, type, type_2) | |
| end | |
| collector.join(", ") | |
| end | |
| def send_request(url: url = "http://pokeapi.co/api/v1/", path: path) | |
| uri = URI(url + path + "/") | |
| response = Net::HTTP.get_response(uri) | |
| JSON.parse(response.body) | |
| end | |
| private | |
| def is_of_type(pokemon, type_name, type_name_2) | |
| if type_name_2 | |
| pokemon["types"].any? { |type| type["name"] == type_name } && | |
| pokemon["types"].any? { |type| type["name"] == type_name_2 } | |
| else | |
| pokemon["types"].any? { |type| type["name"] == type_name } | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment