smogon 0.5.2 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b4c544fa43ec9787e19713b37837aed880f5558
4
- data.tar.gz: 9631e14e37748d286c838dbaf81b37090403bc9e
3
+ metadata.gz: c3754f037f47ef782ff02f217455d16921b0de26
4
+ data.tar.gz: 03fdf4ba26140f321937bb4885292ef256f16273
5
5
  SHA512:
6
- metadata.gz: 1948345687b6bc977edab359e3c4dd354a9b57fb5e0cba1740d03f5d8c6069e66bfedca0c0c81133c81a914696daab3e43e4cf94d5323d076b90a543fe761873
7
- data.tar.gz: 45f665797cabd6a038fdea25d064f9b46520e510cb38e7c3d67344cbed65eb83b6a339d327e24db4010b5191bec96995e4cdb15cecb44597070f8ac292bae057
6
+ metadata.gz: bbf87bf7c1aa0be1729cf392d2f3786d01ea585a70ab61d84430e0b9487acc5f007e82b2f0859ac536f370804d07533644e9d893fc24417abd35d207daa2e167
7
+ data.tar.gz: fc7aa690a33d408ceffd08c1c07cf01b776a18741673da6346b9a37ea084f9ba30f9085bbb1c8a4989fbea3f9a1a084540fd5179c3aceb9fbaa6e118cdc14834
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
3
  #
4
4
  # This file is part of Smogon-API.
5
5
  #
@@ -18,7 +18,10 @@
18
18
  #++
19
19
 
20
20
  require 'open-uri'
21
- require 'nokogiri'
21
+ require 'json'
22
+
23
+ require 'smogon/api'
24
+ require 'smogon/naturedex'
22
25
 
23
26
  require 'smogon/types/pokemon'
24
27
  require 'smogon/types/ability'
@@ -32,4 +35,4 @@ require 'smogon/itemdex'
32
35
  require 'smogon/movedex'
33
36
  require 'smogon/movesetdex'
34
37
 
35
- require 'smogon/version'
38
+ require 'smogon/version'
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
3
  #
4
4
  # This file is part of Smogon-API.
5
5
  #
@@ -18,38 +18,27 @@
18
18
  #++
19
19
 
20
20
  module Smogon
21
- class Abilitydex
22
- def self.get(name)
23
- begin
24
- name = name.downcase.gsub /\s/, ?_
25
- url = URI::encode "http://www.smogon.com/bw/abilities/#{name}"
26
- smogon = Nokogiri::HTML open(url)
27
- rescue
28
- return nil
21
+ class Abilitydex
22
+ def self.get(name, fields = nil)
23
+ incapsulate = fields == nil
24
+
25
+ fields ||= [
26
+ 'name',
27
+ 'alias',
28
+ 'description'
29
+ ]
30
+
31
+ response = API.request 'ability', name, fields
32
+ return nil if response.is_a?(String) || response.empty? || response.first.empty?
33
+ return response if not incapsulate
34
+
35
+ response = response.first
36
+
37
+ Ability.new.tap do |ability|
38
+ ability.name = response['name' ]
39
+ ability._name = response['alias' ]
40
+ ability.description = response['description']
29
41
  end
30
-
31
- Ability.new.tap { |ability|
32
- s = smogon.xpath('//div[@id="content_wrapper"]')[0]
33
- ability.name = s.xpath('.//h1').first.text
34
- ability._name = name
35
-
36
- ability.description = ''.tap { |d|
37
- h2 = 0
38
- ul = 0
39
- s.children.each { |c|
40
- if c.name == 'h2'
41
- h2 += 1
42
- next
43
- end
44
- if c.name == 'ul'
45
- ul += 1
46
- next
47
- end
48
- break if ul >= 2
49
- d << c.text if h2 == 1 && !c.text.strip.empty?
50
- }
51
- }
52
- }
53
42
  end
54
43
  end
55
44
  end
@@ -0,0 +1,53 @@
1
+ #--
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ #
4
+ # This file is part of Smogon-API.
5
+ #
6
+ # Smogon-API is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Smogon-API is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Smogon-API. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Smogon
21
+ class API
22
+ METAGAME = 'xy'
23
+ ENDPOINT = 'http://www.smogon.com/dex/api/query?q='
24
+
25
+ class << self
26
+ def request(what, name, fields)
27
+ query = {
28
+ what => { 'gen' => METAGAME, 'alias' => aliasize(name) },
29
+ '$' => fields
30
+ }
31
+
32
+ query = JSON.generate(query)
33
+ query = URI.escape(query, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
34
+ response = JSON.parse open("#{ENDPOINT}#{query}").read
35
+ response['status'] == 'success' ? response['result'] : response['message']
36
+ end
37
+
38
+ def using_metagame(metagame, &block)
39
+ default_metagame = METAGAME.dup
40
+ METAGAME.replace aliasize(metagame)
41
+ instance_eval(&block).tap do
42
+ METAGAME.replace default_metagame
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def aliasize(string)
49
+ string.downcase.gsub(' ', '_').gsub(/[^a-z_]/i, '')
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
3
  #
4
4
  # This file is part of Smogon-API.
5
5
  #
@@ -18,38 +18,27 @@
18
18
  #++
19
19
 
20
20
  module Smogon
21
- class Itemdex
22
- def self.get(name)
23
- begin
24
- name = name.downcase.gsub /\s/, ?_
25
- url = URI::encode "http://www.smogon.com/bw/items/#{name}"
26
- smogon = Nokogiri::HTML open(url)
27
- rescue
28
- return nil
21
+ class Itemdex
22
+ def self.get(name, fields = nil)
23
+ incapsulate = fields == nil
24
+
25
+ fields ||= [
26
+ 'name',
27
+ 'alias',
28
+ 'description'
29
+ ]
30
+
31
+ response = API.request 'item', name, fields
32
+ return nil if response.is_a?(String) || response.empty? || response.first.empty?
33
+ return response if not incapsulate
34
+
35
+ response = response.first
36
+
37
+ Item.new.tap do |item|
38
+ item.name = response['name' ]
39
+ item._name = response['alias' ]
40
+ item.description = response['description']
29
41
  end
30
-
31
- Item.new.tap { |item|
32
- s = smogon.xpath('//div[@id="content_wrapper"]')[0]
33
- item.name = s.xpath('.//h1').first.text
34
- item._name = name
35
-
36
- item.description = ''.tap { |d|
37
- h2 = 0
38
- ul = 0
39
- s.children.each { |c|
40
- if c.name == 'h2'
41
- h2 += 1
42
- next
43
- end
44
- if c.name == 'ul'
45
- ul += 1
46
- next
47
- end
48
- break if ul >= 2
49
- d << c.text if h2 == 1 && !c.text.strip.empty?
50
- }
51
- }
52
- }
53
42
  end
54
43
  end
55
44
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
3
  #
4
4
  # This file is part of Smogon-API.
5
5
  #
@@ -18,51 +18,35 @@
18
18
  #++
19
19
 
20
20
  module Smogon
21
- class Movedex
22
- def self.get(name)
23
- begin
24
- name = name.downcase.gsub /\s/, ?_
25
- url = URI::encode "http://www.smogon.com/bw/moves/#{name}"
26
- smogon = Nokogiri::HTML open(url)
27
- rescue
28
- return nil
21
+ class Movedex
22
+ def self.get(name, fields = nil)
23
+ incapsulate = fields == nil
24
+
25
+ fields ||= [
26
+ 'name',
27
+ 'alias',
28
+ 'description',
29
+ 'power',
30
+ 'accuracy',
31
+ 'pp',
32
+ 'type' => ['name'],
33
+ ]
34
+
35
+ response = API.request 'move', name, fields
36
+ return nil if response.is_a?(String) || response.empty? || response.first.empty?
37
+ return response if not incapsulate
38
+
39
+ response = response.first
40
+
41
+ Move.new.tap do |move|
42
+ move.name = response['name' ]
43
+ move._name = response['alias' ]
44
+ move.description = response['description']
45
+ move.type = response['type' ].values.join(' / ')
46
+ move.power = response['power' ]
47
+ move.accuracy = response['accuracy' ]
48
+ move.pp = response['pp' ]
29
49
  end
30
-
31
- Move.new.tap { |move|
32
- move.name = smogon.xpath('//div[@id="content_wrapper"]/h1').first.text
33
- move._name = name
34
-
35
- move.description = ''.tap { |d|
36
- h2 = 0
37
- ul = 0
38
- dl = 0
39
- smogon.xpath('//div[@id="content_wrapper"]').children.each { |c|
40
- if c.name == 'h2'
41
- h2 += 1
42
- next
43
- end
44
- if c.name == 'ul'
45
- ul += 1
46
- next
47
- end
48
- if c.name == 'dl'
49
- dl += 1
50
- next
51
- end
52
- break if ul >= 2 || dl >= 2
53
- d << c.text if h2 == 1 && !c.text.strip.empty?
54
- }
55
- }
56
-
57
- info = smogon.xpath('//table[@class="info"]/tr')[1].xpath('.//td')
58
- move.type = info[0].text
59
- move.power = info[1].text
60
- move.accuracy = info[2].text
61
- move.pp = info[3].text
62
- move.priority = info[4].text
63
- move.damage = info[5].text.strip
64
- move.target = info[6].text.strip
65
- }
66
50
  end
67
51
  end
68
52
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
3
  #
4
4
  # This file is part of Smogon-API.
5
5
  #
@@ -18,68 +18,66 @@
18
18
  #++
19
19
 
20
20
  module Smogon
21
- class Movesetdex
22
- def self.get(name, tier, metagame)
23
- begin
24
- url = URI::encode "http://www.smogon.com/#{metagame}/pokemon/#{name}/#{tier}"
25
- smogon = Nokogiri::HTML(open(url))
26
- rescue
27
- return nil
21
+ class Movesetdex
22
+ def self.get(name, tier = nil, metagame = nil, fields = nil)
23
+ incapsulate = fields == nil
24
+
25
+ fields ||= [
26
+ 'name',
27
+ 'movesets' => [
28
+ 'name',
29
+ { 'tags' => %w(shorthand) },
30
+ { 'items' => %w(name) },
31
+ { 'abilities' => %w(name) },
32
+ { 'natures' => %w(hp patk pdef spatk spdef spe) },
33
+ { 'moveslots' => [ 'slot', { 'move' => %(name) } ] },
34
+ { 'evconfigs' => %w(hp patk pdef spatk spdef spe) }
35
+ ]
36
+ ]
37
+
38
+ response = if metagame
39
+ API.using_metagame(metagame) do
40
+ API.request 'pokemon', name, fields
41
+ end
42
+ else
43
+ API.request 'pokemon', name, fields
28
44
  end
29
-
30
- [].tap { |movesets|
31
- smogon.xpath('//table[@class="info strategyheader"]').each { |s|
32
- moveset = Moveset.new
33
-
34
- moveset.pokemon = smogon.xpath('//tr/td[@class="header"]/h1').last.text
35
- moveset.name = s.xpath('tr')[1].xpath('td[@class="name"]/h2').first.text
36
- moveset.tier = smogon.xpath('//div[@id="content_wrapper"]/ul/li/strong').last.text
37
-
38
- if metagame == 'gs'
39
- s.xpath('.//a').each { |a|
40
- moveset.item << a.text if a['href'].include? '/items/'
41
- }
42
- elsif metagame != 'rb'
43
- s.xpath('.//a').each { |a|
44
- moveset.item << a.text if a['href'].include? '/items/'
45
- moveset.ability << a.text if a['href'].include? '/abilities/'
46
- moveset.nature << a.text if a['href'].include? '/natures/'
47
- }
48
- end
49
-
50
- movesets << moveset
51
- }
52
-
53
- i = 0
54
- xpath = metagame == 'rb' ? '//td[@class="rbymoves"]' : '//table[@class="info moveset"]'
55
- smogon.xpath(xpath).each { |s|
56
- moveset = movesets[i]
57
-
58
- continue = false
59
- (metagame == 'rb' ? s : s.xpath('.//td')[0]).text.each_line { |a|
60
- a = a.gsub(/\n?/, '').strip
61
- if a == ?~
62
- continue = false
63
- elsif a == ?/
64
- continue = true
65
- elsif a.empty?
66
- next
67
- elsif a != ?~ && a != ?/
68
- if continue
69
- moveset.moves.last << a
45
+ return nil if response.is_a?(String) || response.empty? || response.first.empty?
46
+ return response if not incapsulate
47
+
48
+ response = response.first
49
+
50
+ results = [].tap do |movesets|
51
+ response['movesets'].each do |movesetdex|
52
+ movesets << Moveset.new.tap do |moveset|
53
+ moveset.pokemon = response['name']
54
+ moveset.name = movesetdex['name']
55
+ moveset.tier = movesetdex['tags'][0]['shorthand']
56
+ moveset.item = movesetdex['items'].collect(&:values).flatten
57
+ moveset.ability = movesetdex['abilities'].collect(&:values).flatten
58
+ moveset.nature = movesetdex['natures'].map { |nature| Naturedex.get(nature) }
59
+
60
+ moveset.moves = []
61
+ movesetdex['moveslots'].each do |moveslot|
62
+ slot = moveslot['slot'] - 1
63
+
64
+ if moveset.moves[slot]
65
+ moveset.moves[slot] << moveslot['move_name']
70
66
  else
71
- moveset.moves << [a]
67
+ moveset.moves << [ moveslot['move_name'] ]
72
68
  end
73
- continue = false
74
69
  end
75
- }
76
-
77
- moveset.evs = s.xpath('.//td').last.text.strip if metagame != 'rb' && metagame != 'gs'
78
-
79
- movesets[i] = moveset
80
- i += 1
81
- }
82
- }
70
+
71
+ moveset.evs = [].tap do |evs|
72
+ ['hp', 'patk', 'pdef', 'spatk', 'spdef', 'spe'].each do |stat|
73
+ evs << movesetdex['evconfigs'].first[stat]
74
+ end
75
+ end.join ' / '
76
+ end
77
+ end
78
+ end
79
+
80
+ tier ? results.reject { |moveset| moveset.tier.downcase != tier.downcase } : results
83
81
  end
84
82
  end
85
83
  end
@@ -0,0 +1,260 @@
1
+ #--
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ #
4
+ # This file is part of Smogon-API.
5
+ #
6
+ # Smogon-API is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Smogon-API is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Smogon-API. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Smogon
21
+ class Naturedex
22
+ NATURES = [
23
+ {
24
+ hp: 1.0,
25
+ patk: 1.1,
26
+ pdef: 1.0,
27
+ spatk: 0.9,
28
+ spdef: 1.0,
29
+ spe: 1.0,
30
+ name: 'Adamant'
31
+ },
32
+ {
33
+ hp: 1.0,
34
+ patk: 1.0,
35
+ pdef: 1.0,
36
+ spatk: 1.0,
37
+ spdef: 1.0,
38
+ spe: 1.0,
39
+ name: 'Bashful'
40
+ },
41
+ {
42
+ hp: 1.0,
43
+ patk: 0.9,
44
+ pdef: 1.1,
45
+ spatk: 1.0,
46
+ spdef: 1.0,
47
+ spe: 1.0,
48
+ name: 'Bold'
49
+ },
50
+ {
51
+ hp: 1.0,
52
+ patk: 1.1,
53
+ pdef: 1.0,
54
+ spatk: 1.0,
55
+ spdef: 1.0,
56
+ spe: 0.9,
57
+ name: 'Brave'
58
+ },
59
+ {
60
+ hp: 1.0,
61
+ patk: 0.9,
62
+ pdef: 1.0,
63
+ spatk: 1.0,
64
+ spdef: 1.1,
65
+ spe: 1.0,
66
+ name: 'Calm'
67
+ },
68
+ {
69
+ hp: 1.0,
70
+ patk: 1.0,
71
+ pdef: 1.0,
72
+ spatk: 0.9,
73
+ spdef: 1.1,
74
+ spe: 1.0,
75
+ name: 'Careful'
76
+ },
77
+ {
78
+ hp: 1.0,
79
+ patk: 1.0,
80
+ pdef: 1.0,
81
+ spatk: 1.0,
82
+ spdef: 1.0,
83
+ spe: 1.0,
84
+ name: 'Docile'
85
+ },
86
+ {
87
+ hp: 1.0,
88
+ patk: 1.0,
89
+ pdef: 0.9,
90
+ spatk: 1.0,
91
+ spdef: 1.1,
92
+ spe: 1.0,
93
+ name: 'Gentle'
94
+ },
95
+ {
96
+ hp: 1.0,
97
+ patk: 1.0,
98
+ pdef: 1.0,
99
+ spatk: 1.0,
100
+ spdef: 1.0,
101
+ spe: 1.0,
102
+ name: 'Hardy'
103
+ },
104
+ {
105
+ hp: 1.0,
106
+ patk: 1.0,
107
+ pdef: 0.9,
108
+ spatk: 1.0,
109
+ spdef: 1.0,
110
+ spe: 1.1,
111
+ name: 'Hasty'
112
+ },
113
+ {
114
+ hp: 1.0,
115
+ patk: 1.0,
116
+ pdef: 1.1,
117
+ spatk: 0.9,
118
+ spdef: 1.0,
119
+ spe: 1.0,
120
+ name: 'Impish'
121
+ },
122
+ {
123
+ hp: 1,
124
+ patk: 1,
125
+ pdef: 1,
126
+ spatk: 0.9,
127
+ spdef: 1,
128
+ spe: 1.1,
129
+ name: 'Jolly'
130
+ },
131
+ {
132
+ hp: 1.0,
133
+ patk: 1.0,
134
+ pdef: 1.1,
135
+ spatk: 1.0,
136
+ spdef: 0.9,
137
+ spe: 1.0,
138
+ name: 'Lax'
139
+ },
140
+ {
141
+ hp: 1.0,
142
+ patk: 1.1,
143
+ pdef: 0.9,
144
+ spatk: 1.0,
145
+ spdef: 1.0,
146
+ spe: 1.0,
147
+ name: 'Lonely'
148
+ },
149
+ {
150
+ hp: 1.0,
151
+ patk: 1.0,
152
+ pdef: 0.9,
153
+ spatk: 1.1,
154
+ spdef: 1.0,
155
+ spe: 1.0,
156
+ name: 'Mild'
157
+ },
158
+ {
159
+ hp: 1.0,
160
+ patk: 0.9,
161
+ pdef: 1.0,
162
+ spatk: 1.1,
163
+ spdef: 1.0,
164
+ spe: 1.0,
165
+ name: 'Modest'
166
+ },
167
+ {
168
+ hp: 1.0,
169
+ patk: 1.0,
170
+ pdef: 1.0,
171
+ spatk: 1.0,
172
+ spdef: 0.9,
173
+ spe: 1.1,
174
+ name: 'Naive'
175
+ },
176
+ {
177
+ hp: 1.0,
178
+ patk: 1.1,
179
+ pdef: 1.0,
180
+ spatk: 1.0,
181
+ spdef: 0.9,
182
+ spe: 1.0,
183
+ name: 'Naughty'
184
+ },
185
+ {
186
+ hp: 1.0,
187
+ patk: 1.0,
188
+ pdef: 1.0,
189
+ spatk: 1.1,
190
+ spdef: 1.0,
191
+ spe: 0.9,
192
+ name: 'Quiet'
193
+ },
194
+ {
195
+ hp: 1.0,
196
+ patk: 1.0,
197
+ pdef: 1.0,
198
+ spatk: 1.0,
199
+ spdef: 1.0,
200
+ spe: 1.0,
201
+ name: 'Quirky'
202
+ },
203
+ {
204
+ hp: 1.0,
205
+ patk: 1.0,
206
+ pdef: 1.0,
207
+ spatk: 1.1,
208
+ spdef: 0.9,
209
+ spe: 1.0,
210
+ name: 'Rash'
211
+ },
212
+ {
213
+ hp: 1.0,
214
+ patk: 1.0,
215
+ pdef: 1.1,
216
+ spatk: 1.0,
217
+ spdef: 1.0,
218
+ spe: 0.9,
219
+ name: 'Relaxed'
220
+ },
221
+ {
222
+ hp: 1.0,
223
+ patk: 1.0,
224
+ pdef: 1.0,
225
+ spatk: 1.0,
226
+ spdef: 1.1,
227
+ spe: 0.9,
228
+ name: 'Sassy'
229
+ },
230
+ {
231
+ hp: 1.0,
232
+ patk: 1.0,
233
+ pdef: 1.0,
234
+ spatk: 1.0,
235
+ spdef: 1.0,
236
+ spe: 1.0,
237
+ name: 'Serious'
238
+ },
239
+ {
240
+ hp: 1.0,
241
+ patk: 0.9,
242
+ pdef: 1.0,
243
+ spatk: 1.0,
244
+ spdef: 1.0,
245
+ spe: 1.1,
246
+ name: 'Timid'
247
+ }
248
+ ]
249
+
250
+ def self.get(hash)
251
+ NATURES.each do |nature|
252
+ boost = nature.dup
253
+ boost.delete :name
254
+ return nature[:name] if hash == boost || hash == Hash[boost.map { |(k, v)| [k.to_s, v] }]
255
+ end
256
+
257
+ nil
258
+ end
259
+ end
260
+ end
@@ -1,63 +1,62 @@
1
- #--
2
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
3
- #
4
- # This file is part of Smogon-API.
5
- #
6
- # Smogon-API is free software: you can redistribute it and/or modify
7
- # it under the terms of the GNU General Public License as published by
8
- # the Free Software Foundation, either version 3 of the License, or
9
- # (at your option) any later version.
10
- #
11
- # Smogon-API is distributed in the hope that it will be useful,
12
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- # GNU General Public License for more details.
15
- #
16
- # You should have received a copy of the GNU General Public License
17
- # along with Smogon-API. If not, see <http://www.gnu.org/licenses/>.
18
- #++
19
-
20
- module Smogon
21
- class Pokedex
22
- def self.get(name)
23
- begin
24
- url = URI::encode "http://www.smogon.com/bw/pokemon/#{name}"
25
- moves_url = URI::encode "http://www.smogon.com/bw/pokemon/#{name}/moves"
26
-
27
- smogon = Nokogiri::HTML open url
28
- moves = Nokogiri::HTML open moves_url
29
- rescue
30
- return nil
31
- end
32
-
33
- Pokemon.new.tap { |pokemon|
34
- pokemon.name = smogon.xpath('//td[@class="header"]/h1').last.text
35
- pokemon._name = pokemon.name.downcase
36
-
37
- smogon.xpath('//table[@class="info"]/tr/td/a')[0..-2].each { |type|
38
- (pokemon.types ||= []) << type.text
39
- }
40
-
41
- pokemon.tier = smogon.xpath('//table[@class="info"]/tr/td/a').last.text
42
-
43
- smogon.xpath('//td[@class="ability"]/dl/dt/a').each { |ability|
44
- (pokemon.abilities ||= []) << ability.text
45
- }
46
-
47
- begin
48
- (pokemon.abilities ||= []) << smogon.xpath('//td[@class="ability"]/dl/dt/em/a').first.text
49
- rescue
50
- # No dream world abilities :(
51
- end
52
-
53
- smogon.xpath('//td[@class="bar"]').each { |base_stat|
54
- (pokemon.base_stats ||= []) << base_stat.text.strip
55
- }
56
-
57
- moves.xpath('//table[starts-with(@id, "move_list")]/tbody/tr').each { |tr|
58
- (pokemon.moves ||= []) << tr.xpath('.//td')[0].text.strip
59
- }
60
- }
61
- end
62
- end
63
- end
1
+ #--
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ #
4
+ # This file is part of Smogon-API.
5
+ #
6
+ # Smogon-API is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Smogon-API is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Smogon-API. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Smogon
21
+ class Pokedex
22
+ def self.get(name, fields = nil)
23
+ incapsulate = fields == nil
24
+
25
+ fields ||= [
26
+ 'name',
27
+ 'alias',
28
+ 'alts' => [
29
+ { 'types' => %w(name) },
30
+ { 'tags' => %w(shorthand) },
31
+ { 'abilities' => %w(name) },
32
+ 'hp', 'patk', 'pdef', 'spatk', 'spdef', 'spe'
33
+ ],
34
+ 'moves' => ['name']
35
+ ]
36
+
37
+ response = API.request 'pokemon', name, fields
38
+ return nil if response.is_a?(String) || response.empty? || response.first.empty?
39
+ return response if not incapsulate
40
+
41
+ response = response.first
42
+ pokedex = response['alts'][0]
43
+
44
+ Pokemon.new.tap do |pokemon|
45
+ pokemon.name = response['name']
46
+ pokemon._name = response['alias']
47
+
48
+ pokemon.types = pokedex['types'].collect(&:values).flatten
49
+ pokemon.tier = pokedex['tags'][0]['shorthand']
50
+ pokemon.abilities = pokedex['abilities'].collect(&:values).flatten
51
+
52
+ pokemon.base_stats = [].tap do |base_stats|
53
+ ['hp', 'patk', 'pdef', 'spatk', 'spdef', 'spe'].each do |stat|
54
+ base_stats << pokedex[stat]
55
+ end
56
+ end
57
+
58
+ pokemon.moves = response['moves'].collect(&:values).flatten
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
3
  #
4
4
  # This file is part of Smogon-API.
5
5
  #
@@ -20,13 +20,13 @@
20
20
  module Smogon
21
21
  class Ability
22
22
  attr_accessor :name, :_name, :description
23
-
23
+
24
24
  def to_s
25
25
  "Name: #{@name}\nDescription: #{@description}"
26
26
  end
27
-
27
+
28
28
  def url
29
- "http://www.smogon.com/bw/abilities/#{@_name}"
29
+ "http://www.smogon.com/dex/#{API::METAGAME}/abilities/#{@_name}"
30
30
  end
31
31
  end
32
- end
32
+ end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
3
  #
4
4
  # This file is part of Smogon-API.
5
5
  #
@@ -20,13 +20,13 @@
20
20
  module Smogon
21
21
  class Item
22
22
  attr_accessor :name, :_name, :description
23
-
23
+
24
24
  def to_s
25
25
  "Name: #{@name}\nDescription: #{@description}"
26
26
  end
27
-
27
+
28
28
  def url
29
- "http://www.smogon.com/bw/items/#{@_name}"
29
+ "http://www.smogon.com/dex/#{API::METAGAME}/items/#{@_name}"
30
30
  end
31
31
  end
32
- end
32
+ end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
3
  #
4
4
  # This file is part of Smogon-API.
5
5
  #
@@ -19,14 +19,18 @@
19
19
 
20
20
  module Smogon
21
21
  class Move
22
- attr_accessor :name, :_name, :description, :type, :power, :accuracy, :pp, :priority, :damage, :target
23
-
22
+ attr_accessor :name, :_name, :description, :type, :power, :accuracy, :pp
23
+
24
24
  def to_s
25
- "Name: #{@name}\nDescription: #{@description}\nType: #{@type}\nPower: #{@power}\nAccuracy: #{@accuracy}\nPP: #{@pp}\nPriority: #{@priority}\nDamage: #{@damage}\nTarget: #{@target}"
25
+ "Name: #{@name}\nDescription: #{@description}\nType: #{@type}\nPower: #{@power}\nAccuracy: #{@accuracy}\nPP: #{@pp}"
26
26
  end
27
-
27
+
28
28
  def url
29
- "http://www.smogon.com/bw/moves/#{@_name}"
29
+ "http://www.smogon.com/dex/#{API::METAGAME}/moves/#{@_name}"
30
+ end
31
+
32
+ %i(priority damage target).each do |m|
33
+ define_method(m) { '' }
30
34
  end
31
35
  end
32
- end
36
+ end
@@ -1,6 +1,6 @@
1
1
  #encoding: utf-8
2
2
  #--
3
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
4
4
  #
5
5
  # This file is part of Smogon-API.
6
6
  #
@@ -21,7 +21,7 @@
21
21
  module Smogon
22
22
  class Moveset
23
23
  attr_accessor :pokemon, :name, :tier, :item, :ability, :nature, :moves, :evs
24
-
24
+
25
25
  def initialize
26
26
  @item = []
27
27
  @ability = []
@@ -33,4 +33,4 @@ module Smogon
33
33
  "Pokémon: #{@pokemon}\nSet: #{@name}\nItem: #{@item.join(' / ')}\nAbility: #{@ability.join(' / ')}\nNature: #{@nature.join(' / ')}\nMoves: #{''.tap { |s| @moves.each { |move| s << move.join(' / ') + ', '}}[0..-3]}\nEVs: #{@evs}"
34
34
  end
35
35
  end
36
- end
36
+ end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
3
  #
4
4
  # This file is part of Smogon-API.
5
5
  #
@@ -26,7 +26,7 @@ module Smogon
26
26
  end
27
27
 
28
28
  def url
29
- "http://www.smogon.com/bw/pokemon/#{@_name}"
29
+ "http://www.smogon.com/dex/#{API::METAGAME}/pokemon/#{@_name}"
30
30
  end
31
31
 
32
32
  def self.id2name(id)
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright(C) 2013 Giovanni Capuano <webmaster@giovannicapuano.net>
2
+ # Copyright(C) 2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
3
  #
4
4
  # This file is part of Smogon-API.
5
5
  #
@@ -18,7 +18,5 @@
18
18
  #++
19
19
 
20
20
  module Smogon
21
- def self.version
22
- '0.5.2'
23
- end
21
+ VERSION = '0.6'
24
22
  end
@@ -0,0 +1,12 @@
1
+ #! /usr/bin/env ruby
2
+ require 'smogon'
3
+
4
+ describe 'Abilitydex' do
5
+ it 'returns infos about the given ability' do
6
+ ability = Smogon::Abilitydex.get 'Synchronize'
7
+
8
+ expect(ability.name).to eq('Synchronize')
9
+ expect(ability.description.length).to be > 10
10
+ expect(ability.to_s.length).to be > 10
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ #! /usr/bin/env ruby
2
+ require 'smogon'
3
+
4
+ describe 'Itemdex' do
5
+ it 'returns infos about the given item' do
6
+ item = Smogon::Itemdex.get 'Leftovers'
7
+
8
+ expect(item.name).to eq('Leftovers')
9
+ expect(item.description.length).to be > 10
10
+ expect(item.to_s.length).to be > 10
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ #! /usr/bin/env ruby
2
+ require 'smogon'
3
+
4
+ describe 'Movedex' do
5
+ it 'returns infos about the given move' do
6
+ move = Smogon::Movedex.get 'Extreme Speed'
7
+
8
+ expect(move.name).to eq('Extreme Speed')
9
+ expect(move.type).to eq('Normal')
10
+ expect(move.pp).to be 5
11
+ expect(move.to_s.length).to be > 10
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ #! /usr/bin/env ruby
2
+ require 'smogon'
3
+
4
+ describe 'Movesetdex' do
5
+ it 'returns the moveset of given Pokémon and metagame' do
6
+ movesets = Smogon::Movesetdex.get 'Abomasnow', 'UU', 'bw'
7
+
8
+ expect(movesets.length).to be > 0
9
+ expect(movesets.to_s.length).to be > 10
10
+ expect(movesets.first.nature.include?('Lonely')).to be_truthy
11
+ expect(movesets.select { |moveset| moveset.tier != 'UU' }.length).to be 0
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ #! /usr/bin/env ruby
2
+ require 'smogon'
3
+
4
+ describe 'Pokedex' do
5
+ it 'returns the requested Pokémon' do
6
+ pokemon = Smogon::Pokedex.get 'Blaziken'
7
+
8
+ expect(pokemon.name).to eq('Blaziken')
9
+ expect(pokemon.base_stats.last).to be 80
10
+ expect(pokemon.moves.length).to be > 10
11
+ expect(pokemon.to_s.length).to be > 10
12
+ end
13
+ end
metadata CHANGED
@@ -1,67 +1,70 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smogon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: '0.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giovanni Capuano
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-10 00:00:00.000000000 Z
11
+ date: 2015-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: nokogiri
14
+ name: json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1.8'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '1.8'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '10.4'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '10.4'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '3.1'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
55
- description: API to get (Poké|Ability|Item|Move|Moveset)dex from Smogon
54
+ version: '3.1'
55
+ description: APIs wrapper to get (Poké|Ability|Item|Move|Moveset)dex data from Smogon
56
56
  email: webmaster@giovannicapuano.net
57
57
  executables: []
58
58
  extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
+ - lib/smogon.rb
61
62
  - lib/smogon/abilitydex.rb
63
+ - lib/smogon/api.rb
62
64
  - lib/smogon/itemdex.rb
63
65
  - lib/smogon/movedex.rb
64
66
  - lib/smogon/movesetdex.rb
67
+ - lib/smogon/naturedex.rb
65
68
  - lib/smogon/pokedex.rb
66
69
  - lib/smogon/types/ability.rb
67
70
  - lib/smogon/types/item.rb
@@ -69,8 +72,11 @@ files:
69
72
  - lib/smogon/types/moveset.rb
70
73
  - lib/smogon/types/pokemon.rb
71
74
  - lib/smogon/version.rb
72
- - lib/smogon.rb
73
- - spec/smogon_spec.rb
75
+ - spec/abilitydex.rb
76
+ - spec/itemdex.rb
77
+ - spec/movedex.rb
78
+ - spec/movesetdex.rb
79
+ - spec/pokedex.rb
74
80
  homepage: http://www.giovannicapuano.net
75
81
  licenses:
76
82
  - GPL-3
@@ -91,9 +97,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
97
  version: '0'
92
98
  requirements: []
93
99
  rubyforge_project:
94
- rubygems_version: 2.0.3
100
+ rubygems_version: 2.2.2
95
101
  signing_key:
96
102
  specification_version: 4
97
- summary: API for Smogon.
103
+ summary: APIs wrapper to get data from Smogon.
98
104
  test_files:
99
- - spec/smogon_spec.rb
105
+ - spec/abilitydex.rb
106
+ - spec/itemdex.rb
107
+ - spec/movedex.rb
108
+ - spec/movesetdex.rb
109
+ - spec/pokedex.rb
@@ -1,41 +0,0 @@
1
- #! /usr/bin/env ruby
2
- require 'smogon'
3
-
4
- describe 'Smogon' do
5
- it 'returns the requested Pokémon' do
6
- pokemon = Smogon::Pokedex.get 'Blaziken'
7
- pokemon.name.should eql('Blaziken')
8
- pokemon.base_stats.last.should eql('80')
9
- pokemon.moves.any?.should be_true
10
- end
11
-
12
- it 'search the given ability' do
13
- ability = Smogon::Abilitydex.get 'Synchronize'
14
- ability.name.should eql('Synchronize')
15
- end
16
-
17
- it 'returns the given item' do
18
- item = Smogon::Itemdex.get 'Leftovers'
19
- item.name.should eql('Leftovers')
20
- end
21
-
22
- it 'returns the given move' do
23
- move = Smogon::Movedex.get 'Reflect'
24
- move.name.should eql('Reflect')
25
- move.type.should eql('Psychic')
26
- move.pp.should eql('20')
27
- end
28
-
29
- it 'returns the moveset of the given Pokémon and metagame' do
30
- moveset = Smogon::Movesetdex.get 'Blaziken', 'uber', 'bw'
31
- moveset.any?.should be_true
32
- moveset.first.nature.include?('Jolly').should be_true
33
- end
34
-
35
- it 'returns the name of the Pokémon relative to the given ID' do
36
- Smogon::Pokemon.id2name(111).should eql('Rhyhorn')
37
- moveset = Smogon::Movesetdex.get 'Blaziken', 'uber', 'bw'
38
- moveset.any?.should be_true
39
- moveset.first.nature.include?('Jolly').should be_true
40
- end
41
- end