Class: Smogon::Pokedex

Inherits:
Object
  • Object
show all
Defined in:
lib/smogon/pokedex.rb

Class Method Summary collapse

Class Method Details

.get(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/smogon/pokedex.rb', line 22

def self.get(name)    
  begin
    url       = URI::encode "http://www.smogon.com/bw/pokemon/#{name}"
    moves_url = URI::encode "http://www.smogon.com/bw/pokemon/#{name}/moves"

    smogon  = Nokogiri::HTML open url
    moves   = Nokogiri::HTML open moves_url
  rescue
    return nil
  end

  Pokemon.new.tap { |pokemon|
    pokemon.name  = smogon.xpath('//td[@class="header"]/h1').last.text
    pokemon._name = pokemon.name.downcase
    
    smogon.xpath('//table[@class="info"]/tr/td/a')[0..-2].each { |type|
      (pokemon.types      ||= []) << type.text
    }
    
    pokemon.tier = smogon.xpath('//table[@class="info"]/tr/td/a').last.text
    
    smogon.xpath('//td[@class="ability"]/dl/dt/a').each { |ability|
      (pokemon.abilities  ||= []) << ability.text
    }
    
    begin
      (pokemon.abilities  ||= []) << smogon.xpath('//td[@class="ability"]/dl/dt/em/a').first.text
    rescue
      # No dream world abilities :(

    end
    
    smogon.xpath('//td[@class="bar"]').each { |base_stat|
      (pokemon.base_stats ||= []) << base_stat.text.strip
    }
    
    moves.xpath('//table[starts-with(@id, "move_list")]/tbody/tr').each { |tr|
      (pokemon.moves      ||= []) << tr.xpath('.//td')[0].text.strip
    }
  }
end