module PokemonGenerator

Constants

VERSION

Public Class Methods

evolve(pokemon_to_evolve) click to toggle source
# File lib/PokemonGenerator.rb, line 51
def self.evolve(pokemon_to_evolve)
  evolutions = []
  href = link_html_stripper(pokemon_to_evolve)
  html = Nokogiri::HTML(open("https://pokemondb.net/#{href}"))
  html.css('.infocard-tall').each do |pokmn|
    if pokmn.css('.ent-name').text != ''
      evolutions << pokmn.css('.ent-name').text
    end
  end
  if !evolutions.empty?
    PokemonGenerator.pokemon({ name: evolutions[evolutions.index(pokemon_to_evolve) + 1] })
  else
    "Their is no evolution for that pokemon."
  end
end
image(pokmn_name) click to toggle source
# File lib/PokemonGenerator.rb, line 67
def self.image(pokmn_name)
  pokmn_link = PokemonGenerator.link_html_stripper(pokmn_name.capitalize)
  html = Nokogiri::HTML(open("https://pokemondb.net/#{pokmn_link}"))
  html.css('.figure img').attr('src').text == "https://img.pokemondb.net/s.png" ? 
  'Sorry no image found by that name' : html.css('.figure img').attr('src').text
end
moves(pokmn_name) click to toggle source
# File lib/PokemonGenerator.rb, line 40
def self.moves(pokmn_name)
  moves_list = []
  pokmn_link = PokemonGenerator.link_html_stripper(pokmn_name)
  html = Nokogiri::HTML(open("https://pokemondb.net/#{pokmn_link}"))
  html.css('td').each do |pokmn_txt|
    moves = pokmn_txt.css('.ent-name').text
    moves_list << moves if !moves_list.include?(moves) && moves != ''
  end
  moves_list
end
name() click to toggle source
# File lib/PokemonGenerator.rb, line 29
def self.name
  nokogiri = PokemonGenerator.nokogiri_obj()
  pokmn_name = nokogiri.css('tr')[rand(0..909)].css('.ent-name').text
  pokmn_name if pokmn_name != ''
end
nokogiri_obj() click to toggle source
# File lib/PokemonGenerator.rb, line 10
def self.nokogiri_obj
  Nokogiri::HTML(open("https://pokemondb.net/pokedex/all"))
end
pokemon(args={}) click to toggle source
# File lib/PokemonGenerator.rb, line 105
def self.pokemon(args={})
  nokogiri = PokemonGenerator.nokogiri_obj()
  pokemon_list = nokogiri.css('tr')
  pokemon_obj = {}
  if args[:name]
    pokemon_obj = PokemonGenerator.pokemon_name_specific(pokemon_list, args[:name])
  elsif args[:type]
    pokemon_obj = PokemonGenerator.pokemon_type_specific(pokemon_list, args[:type])
  else
      pokemon = pokemon_list[rand(0..909)]
      pokemon_obj[:name] = pokemon.css('.ent-name').text
      pokemon_obj[:type] = PokemonGenerator.type_stripper(pokemon.css('a'))
      pokemon_obj[:image] = PokemonGenerator.image(pokemon.css('.ent-name').text)
      pokemon_obj[:moves] = PokemonGenerator.moves(pokemon.css('.ent-name').text)
  end
  pokemon_obj
end
pokemon_name_specific(pokemon_list, name) click to toggle source
# File lib/PokemonGenerator.rb, line 74
def self.pokemon_name_specific(pokemon_list, name)
  if pokemon_list != ''
    pokemon_list.find do |pokmn|
      if pokmn.css('.ent-name').text == name.capitalize
        return {
          name: name,
          type: PokemonGenerator.type_stripper(pokmn.css('a')),
          image: PokemonGenerator.image(pokmn.css('.ent-name').text),
          moves: PokemonGenerator.moves(name.capitalize)
        }
      end
    end
  end
  'Sorry no pokemon found by that name.'
end
pokemon_type_specific(pokemon_list, type) click to toggle source
# File lib/PokemonGenerator.rb, line 90
def self.pokemon_type_specific(pokemon_list, type)
  pokemon_list.find do |pokmn|
    if PokemonGenerator.type_stripper(pokmn.css('a')).include?(type)
      return {
        name: pokmn.css('.ent-name').text,
        type: PokemonGenerator.type_stripper(pokmn.css('a')),
        image: PokemonGenerator.image(pokmn.css('.ent-name').text),
        moves: PokemonGenerator.moves(pokmn.css('.ent-name').text)
      }
    end
  end
  'Sorry no pokemon found by that type.'
end
type() click to toggle source
# File lib/PokemonGenerator.rb, line 35
def self.type
  nokogiri = PokemonGenerator.nokogiri_obj()
  PokemonGenerator.type_stripper(nokogiri.css('tr')[rand(0..909)].css('a'))
end
type_stripper(a_tags) click to toggle source
# File lib/PokemonGenerator.rb, line 14
def self.type_stripper(a_tags)
  types_list = []
  a_tags.each do |link|
    types_list << link.text
  end
  types_list.length == 2 ? types_list[1] : [types_list[1], types_list[2]]
end