battle_pet 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/battle_pet.rb +15 -2
  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
- JSON.parse(open(url).read)
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: battle_pet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: