battle_pet 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
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
- info = get_data_from_api(id, locale)
21
- set_attributes(info, locale)
22
- end
23
-
24
- def can_battle?
25
- @can_battle
26
- end
27
-
28
- protected
29
-
30
- def get_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(hash, locale)
36
- @id = hash["speciesId"]
37
- @description = hash["description"]
38
- @name = find_name(locale)
39
- @source = hash["source"]
40
- @can_battle = hash["canBattle"]
41
- @type = PetType.find hash["petTypeId"]
42
- @creature = hash["creatureId"]
43
- @abilities = acquire_abilities hash["abilities"]
44
- @added_in_patch = check_patch
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 @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
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