Class: Smogon::Movedex

Inherits:
Object
  • Object
show all
Defined in:
lib/smogon/movedex.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
62
63
64
65
66
# File 'lib/smogon/movedex.rb', line 22

def self.get(name)
  begin
    name   = name.downcase.gsub /\s/, ?_
    url    = URI::encode "http://www.smogon.com/bw/moves/#{name}"
    smogon = Nokogiri::HTML open(url)
  rescue
    return nil
  end
  
  Move.new.tap { |move|      
    move.name  = smogon.xpath('//div[@id="content_wrapper"]/h1').first.text
    move._name = name
    
    move.description = ''.tap { |d|
      h2 = 0
      ul = 0
      dl = 0
      smogon.xpath('//div[@id="content_wrapper"]').children.each { |c|
        if c.name == 'h2'
          h2 += 1
          next
        end
        if c.name == 'ul'
          ul += 1
          next
        end
        if c.name == 'dl'
          dl += 1
          next
        end
        break if ul >= 2 || dl >= 2
        d << c.text if h2 == 1 && !c.text.strip.empty?
      }
    }
    
    info = smogon.xpath('//table[@class="info"]/tr')[1].xpath('.//td')
    move.type     = info[0].text
    move.power    = info[1].text
    move.accuracy = info[2].text
    move.pp       = info[3].text
    move.priority = info[4].text
    move.damage   = info[5].text.strip
    move.target   = info[6].text.strip
  }
end