battle_pet 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -9
- data/Rakefile +8 -0
- data/lib/battle_pet.rb +61 -61
- data/lib/battle_pet.yml +1995 -1995
- data/lib/pet_ability.rb +11 -11
- data/lib/pet_type.rb +16 -16
- data/test/data/mechanical_squirrel_cn.json +1 -0
- data/test/data/mechanical_squirrel_us.json +1 -0
- data/test/test_battle_pet.rb +8 -0
- metadata +6 -2
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
# BattlePet
|
2
|
-
|
3
|
-
## Description
|
4
|
-
|
5
|
-
This is a gem to parse the info from WoW BattlePet API.
|
6
|
-
|
7
|
-
## Usage
|
8
|
-
|
9
|
-
`pet = BattlePet.new(pet_id)`
|
1
|
+
# BattlePet
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
This is a gem to parse the info from WoW BattlePet API.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
`pet = BattlePet.new(pet_id)`
|
data/Rakefile
ADDED
data/lib/battle_pet.rb
CHANGED
@@ -1,61 +1,61 @@
|
|
1
|
-
require 'open-uri'
|
2
|
-
require 'json'
|
3
|
-
require 'yaml'
|
4
|
-
|
5
|
-
require_relative 'pet_type.rb'
|
6
|
-
require_relative 'pet_ability.rb'
|
7
|
-
|
8
|
-
class BattlePet
|
9
|
-
attr_accessor :id, :name, :description, :source, :type, :creature, :abilities, :added_in_patch
|
10
|
-
|
11
|
-
REGION_HOSTS = { us: 'us.battle.net',
|
12
|
-
eu: 'eu.battle.net',
|
13
|
-
kr: 'kr.battle.net',
|
14
|
-
tw: 'tw.battle.net',
|
15
|
-
cn: 'www.battlenet.com.cn'}
|
16
|
-
|
17
|
-
PET_NAMES = YAML.load File.read(File.join(File.dirname(__FILE__), 'battle_pet.yml'))
|
18
|
-
|
19
|
-
def initialize(id, locale = :us)
|
20
|
-
url = "http://#{host(locale)}/api/wow/battlePet/species/#{id.to_s}"
|
21
|
-
info = JSON.parse(open(url).read)
|
22
|
-
@id = id
|
23
|
-
@description = info["description"]
|
24
|
-
@name = find_name(locale)
|
25
|
-
@source = info["source"]
|
26
|
-
@can_battle = info["canBattle"]
|
27
|
-
@type = PetType.find info["petTypeId"]
|
28
|
-
@creature = info["creatureId"]
|
29
|
-
@abilities = acquire_abilities info["abilities"]
|
30
|
-
@added_in_patch = check_patch
|
31
|
-
end
|
32
|
-
|
33
|
-
def can_battle?
|
34
|
-
@can_battle
|
35
|
-
end
|
36
|
-
|
37
|
-
protected
|
38
|
-
|
39
|
-
def host(locale)
|
40
|
-
REGION_HOSTS[locale] || REGION_HOSTS[:us]
|
41
|
-
end
|
42
|
-
|
43
|
-
def find_name(locale)
|
44
|
-
PET_NAMES[id] && PET_NAMES[id]["name"][locale.to_s]
|
45
|
-
end
|
46
|
-
|
47
|
-
def acquire_abilities(abilities)
|
48
|
-
results = {}
|
49
|
-
abilities.each { |ability| results[ability['requiredLevel']] = PetAbility.new(ability) }
|
50
|
-
results
|
51
|
-
end
|
52
|
-
|
53
|
-
def check_patch
|
54
|
-
case @id
|
55
|
-
when 1..864 then '5.0'
|
56
|
-
when 865..1013 then '5.1'
|
57
|
-
when 1014..1213 then '5.2'
|
58
|
-
else '5.3'
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
1
|
+
require 'open-uri'
|
2
|
+
require 'json'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
require_relative 'pet_type.rb'
|
6
|
+
require_relative 'pet_ability.rb'
|
7
|
+
|
8
|
+
class BattlePet
|
9
|
+
attr_accessor :id, :name, :description, :source, :type, :creature, :abilities, :added_in_patch
|
10
|
+
|
11
|
+
REGION_HOSTS = { us: 'us.battle.net',
|
12
|
+
eu: 'eu.battle.net',
|
13
|
+
kr: 'kr.battle.net',
|
14
|
+
tw: 'tw.battle.net',
|
15
|
+
cn: 'www.battlenet.com.cn'}
|
16
|
+
|
17
|
+
PET_NAMES = YAML.load File.read(File.join(File.dirname(__FILE__), 'battle_pet.yml'))
|
18
|
+
|
19
|
+
def initialize(id, locale = :us)
|
20
|
+
url = "http://#{host(locale)}/api/wow/battlePet/species/#{id.to_s}"
|
21
|
+
info = JSON.parse(open(url).read)
|
22
|
+
@id = id
|
23
|
+
@description = info["description"]
|
24
|
+
@name = find_name(locale)
|
25
|
+
@source = info["source"]
|
26
|
+
@can_battle = info["canBattle"]
|
27
|
+
@type = PetType.find info["petTypeId"]
|
28
|
+
@creature = info["creatureId"]
|
29
|
+
@abilities = acquire_abilities info["abilities"]
|
30
|
+
@added_in_patch = check_patch
|
31
|
+
end
|
32
|
+
|
33
|
+
def can_battle?
|
34
|
+
@can_battle
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
|
39
|
+
def host(locale)
|
40
|
+
REGION_HOSTS[locale] || REGION_HOSTS[:us]
|
41
|
+
end
|
42
|
+
|
43
|
+
def find_name(locale)
|
44
|
+
PET_NAMES[id] && PET_NAMES[id]["name"][locale.to_s]
|
45
|
+
end
|
46
|
+
|
47
|
+
def acquire_abilities(abilities)
|
48
|
+
results = {}
|
49
|
+
abilities.each { |ability| results[ability['requiredLevel']] = PetAbility.new(ability) }
|
50
|
+
results
|
51
|
+
end
|
52
|
+
|
53
|
+
def check_patch
|
54
|
+
case @id
|
55
|
+
when 1..864 then '5.0'
|
56
|
+
when 865..1013 then '5.1'
|
57
|
+
when 1014..1213 then '5.2'
|
58
|
+
else '5.3'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|