battle_pet 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +9 -9
- data/Rakefile +7 -7
- data/lib/battle_pet.rb +69 -69
- data/lib/battle_pet.yml +1995 -1995
- data/lib/pet_ability.rb +11 -11
- data/lib/pet_type.rb +16 -16
- data/test/test_battle_pet.rb +32 -8
- metadata +1 -1
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
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'rake/testtask'
|
2
|
-
|
3
|
-
Rake::TestTask.new do |t|
|
4
|
-
t.libs << 'test'
|
5
|
-
end
|
6
|
-
|
7
|
-
desc "Run tests"
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
Rake::TestTask.new do |t|
|
4
|
+
t.libs << 'test'
|
5
|
+
end
|
6
|
+
|
7
|
+
desc "Run tests"
|
8
8
|
task :default => :test
|
data/lib/battle_pet.rb
CHANGED
@@ -1,69 +1,69 @@
|
|
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
|
-
|
21
|
-
set_attributes(
|
22
|
-
end
|
23
|
-
|
24
|
-
def can_battle?
|
25
|
-
@can_battle
|
26
|
-
end
|
27
|
-
|
28
|
-
protected
|
29
|
-
|
30
|
-
def
|
31
|
-
url = "http://#{host(locale)}/api/wow/battlePet/species/#{id.to_s}"
|
32
|
-
JSON.parse(open(url).read)
|
33
|
-
end
|
34
|
-
|
35
|
-
def set_attributes(
|
36
|
-
@id =
|
37
|
-
@
|
38
|
-
@
|
39
|
-
@source =
|
40
|
-
@can_battle =
|
41
|
-
@type = PetType.find
|
42
|
-
@creature =
|
43
|
-
@
|
44
|
-
@
|
45
|
-
end
|
46
|
-
|
47
|
-
def host(locale)
|
48
|
-
REGION_HOSTS[locale] || REGION_HOSTS[:us]
|
49
|
-
end
|
50
|
-
|
51
|
-
def find_name(locale)
|
52
|
-
PET_NAMES[id] && PET_NAMES[id]["name"][locale.to_s]
|
53
|
-
end
|
54
|
-
|
55
|
-
def acquire_abilities(abilities)
|
56
|
-
results = {}
|
57
|
-
abilities.each { |ability| results[ability['requiredLevel']] = PetAbility.new(ability) }
|
58
|
-
results
|
59
|
-
end
|
60
|
-
|
61
|
-
def check_patch
|
62
|
-
case
|
63
|
-
when 1..864 then '5.0'
|
64
|
-
when 865..1013 then '5.1'
|
65
|
-
when 1014..1213 then '5.2'
|
66
|
-
else '5.3'
|
67
|
-
end
|
68
|
-
end
|
69
|
-
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
|
+
data = BattlePet.parse_data_from_api(id, locale)
|
21
|
+
set_attributes(data, locale)
|
22
|
+
end
|
23
|
+
|
24
|
+
def can_battle?
|
25
|
+
@can_battle
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def self.parse_data_from_api(id, locale)
|
31
|
+
url = "http://#{host(locale)}/api/wow/battlePet/species/#{id.to_s}"
|
32
|
+
JSON.parse(open(url).read)
|
33
|
+
end
|
34
|
+
|
35
|
+
def set_attributes(data, locale)
|
36
|
+
@id = data["speciesId"]
|
37
|
+
@name = BattlePet.find_name(id, locale)
|
38
|
+
@description = data["description"]
|
39
|
+
@source = data["source"]
|
40
|
+
@can_battle = data["canBattle"]
|
41
|
+
@type = PetType.find data["petTypeId"]
|
42
|
+
@creature = data["creatureId"]
|
43
|
+
@added_in_patch = BattlePet.check_patch(id)
|
44
|
+
@abilities = acquire_abilities data["abilities"]
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.host(locale)
|
48
|
+
REGION_HOSTS[locale] || REGION_HOSTS[:us]
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.find_name(id, locale)
|
52
|
+
PET_NAMES[id] && PET_NAMES[id]["name"][locale.to_s]
|
53
|
+
end
|
54
|
+
|
55
|
+
def acquire_abilities(abilities)
|
56
|
+
results = {}
|
57
|
+
abilities.each { |ability| results[ability['requiredLevel']] = PetAbility.new(ability) }
|
58
|
+
results
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.check_patch(id)
|
62
|
+
case id
|
63
|
+
when 1..864 then '5.0'
|
64
|
+
when 865..1013 then '5.1'
|
65
|
+
when 1014..1213 then '5.2'
|
66
|
+
else '5.3'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|