Class: Smogon::Abilitydex

Inherits:
Object
  • Object
show all
Defined in:
lib/smogon/abilitydex.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
# File 'lib/smogon/abilitydex.rb', line 22

def self.get(name)
  begin
    name = name.downcase.gsub /\s/, ?_
    url = URI::encode "http://www.smogon.com/bw/abilities/#{name}"
    
    smogon = Nokogiri::HTML(open(url))
  rescue
    return nil
  end
  
  ability = Ability.new
  
  s = smogon.xpath('//div[@id="content_wrapper"]')[0]
  ability.name        = s.xpath('.//h1').first.text
  ability._name       = name
  
  ability.description = ''.tap { |d|
    h2 = 0
    s.children.each { |c|
      if c.name == 'h2'          
        h2 += 1
        next
      end
      d << c.text if h2 == 1 && !c.text.strip.empty?
    }
  }
  
  return ability
end