battle_pet 0.2.7 → 0.2.8
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/lib/battle_pet.rb +15 -2
- metadata +1 -1
data/lib/battle_pet.rb
CHANGED
|
@@ -13,7 +13,7 @@ class BattlePet
|
|
|
13
13
|
|
|
14
14
|
def initialize(id, locale = :us)
|
|
15
15
|
data = BattlePet.parse_data_from_api(id, locale)
|
|
16
|
-
set_attributes(data, locale)
|
|
16
|
+
set_attributes(data, locale) if data
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def can_battle?
|
|
@@ -28,7 +28,20 @@ class BattlePet
|
|
|
28
28
|
|
|
29
29
|
def self.parse_data_from_api(id, locale)
|
|
30
30
|
url = "http://#{host(locale)}/api/wow/battlePet/species/#{id.to_s}"
|
|
31
|
-
|
|
31
|
+
tried_times = 0
|
|
32
|
+
begin
|
|
33
|
+
response = open(url)
|
|
34
|
+
rescue OpenURI::HTTPError => e
|
|
35
|
+
if e.message =~ /404/
|
|
36
|
+
warn '[WARNING] Pet ID Incorrect'
|
|
37
|
+
elsif e.message =~ /500/
|
|
38
|
+
warn '[WARNING] Access denied.'
|
|
39
|
+
elsif tried_times < 3
|
|
40
|
+
tried_times += 1
|
|
41
|
+
retry
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
response ? JSON.parse(response.read) : nil
|
|
32
45
|
end
|
|
33
46
|
|
|
34
47
|
def set_attributes(data, locale)
|