Class: Smogon::Movesetdex

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

Class Method Summary collapse

Class Method Details

.get(name, tier, metagame) ⇒ 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/smogon/movesetdex.rb', line 22

def self.get(name, tier, metagame)
  begin
    url    = URI::encode "http://www.smogon.com/#{metagame}/pokemon/#{name}/#{tier}"
    smogon = Nokogiri::HTML(open(url))
  rescue
    return nil
  end
  
  [].tap { |movesets|
    smogon.xpath('//table[@class="info strategyheader"]').each { |s|
      moveset = Moveset.new
      
      moveset.pokemon = smogon.xpath('//tr/td[@class="header"]/h1').last.text
      moveset.name    = s.xpath('tr')[1].xpath('td[@class="name"]/h2').first.text
      moveset.tier    = smogon.xpath('//div[@id="content_wrapper"]/ul/li/strong').last.text
      
      if metagame == 'gs'
        s.xpath('.//a').each { |a|
          moveset.item    << a.text if a['href'].include? '/items/'
        }
      elsif metagame != 'rb'
        s.xpath('.//a').each { |a|
          moveset.item    << a.text if a['href'].include? '/items/'
          moveset.ability << a.text if a['href'].include? '/abilities/'
          moveset.nature  << a.text if a['href'].include? '/natures/'
        }
      end
      
      movesets << moveset
    }
    
    i = 0
    xpath = metagame == 'rb' ? '//td[@class="rbymoves"]' : '//table[@class="info moveset"]'
    smogon.xpath(xpath).each { |s|
      moveset = movesets[i]
      
      continue = false
      (metagame == 'rb' ? s : s.xpath('.//td')[0]).text.each_line { |a|
        a = a.gsub(/\n?/, '').strip
        if a == ?~
          continue = false
        elsif a == ?/
          continue = true
        elsif a.empty?
          next
        elsif a != ?~ && a != ?/
          if continue
            moveset.moves.last << a
          else
            moveset.moves << [a]
          end
          continue = false
        end
      }
      
      moveset.evs = s.xpath('.//td').last.text.strip if metagame != 'rb' && metagame != 'gs'
      
      movesets[i] = moveset
      i += 1
    }
  }
end