bnet 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bnet.rb CHANGED
@@ -22,8 +22,12 @@ require 'bnet/diablo3/career'
22
22
  require 'bnet/diablo3/item'
23
23
  require 'bnet/diablo3/hero'
24
24
  require 'bnet/diablo3/skill'
25
- # require 'bnet/community'
25
+ require 'bnet/diablo3/follower'
26
+ # require 'bnet/community' #TODO remove?
26
27
  require 'bnet/starcraft2'
28
+ # require 'bnet/starcraft2/ladder'
29
+ require 'bnet/starcraft2/match'
30
+ require 'bnet/starcraft2/career'
27
31
  require 'bnet/starcraft2/profile'
28
32
  require 'bnet/wow'
29
33
  require 'bnet/wow/data'
@@ -2,18 +2,18 @@ class Bnet::Diablo3::Career < Bnet::BnetResource
2
2
 
3
3
  attr_accessor :heroes, :last_hero_played, :last_updated, :kills, :time_played,
4
4
  :fallen_heroes, :paragon_level, :paragon_level_hardcore, :battle_tag,
5
- :progression, :region
5
+ :progression, :region, :raw_attributes
6
6
 
7
7
  PARAMS_MAPPING = {
8
- "lastHeroPlayed" => :last_hero_played,
9
- "lastUpdated" => :last_updated,
10
- "kills" => :kills,
11
- "timePlayed" => :time_played,
12
- "fallenHeroes" => :fallen_heroes,
13
- "paragonLevel" => :paragon_level,
14
- "paragonLevelHardcore" => :paragon_level_hardcore,
15
- "battleTag" => :battle_tag,
16
- "progression" => :progression
8
+ "lastHeroPlayed" => :last_hero_played,
9
+ "lastUpdated" => :last_updated,
10
+ "kills" => :kills,
11
+ "timePlayed" => :time_played,
12
+ "fallenHeroes" => :fallen_heroes,
13
+ "paragonLevel" => :paragon_level,
14
+ "paragonLevelHardcore" => :paragon_level_hardcore,
15
+ "battleTag" => :battle_tag,
16
+ "progression" => :progression
17
17
  }
18
18
 
19
19
  def battle_tag
@@ -50,42 +50,46 @@ class Bnet::Diablo3::Career < Bnet::BnetResource
50
50
  base_api = Bnet::Diablo3.new(region: region)
51
51
  call_url = base_api.url + "profile/#{battle_tag}/?apikey=#{api_key}&locale=#{locale}"
52
52
 
53
- # NOTE common tasks below - marker for easier method extraction
54
- response = JSON.parse( URI.parse(call_url).read )
53
+ begin
54
+ data = open(call_url)
55
+ raw_response = JSON.parse(data.read)
55
56
 
56
- data = open(call_url)
57
- raw_response = JSON.parse(data.read)
57
+ if Bnet::API.valid_call?(data.status, raw_response)
58
+ career = from_api(raw_response)
59
+ career.raw_attributes = raw_response
60
+ career.region = region
58
61
 
59
- if Bnet::API.valid_call?(data.status, raw_response)
60
- career = from_api(raw_response)
61
- else
62
+ assign_heroes_from_raw_heroes(career, raw_response["heroes"]) if raw_response["heroes"]
63
+ else
64
+ career = nil
65
+ end
66
+
67
+ rescue OpenURI::HTTPError => e
62
68
  career = nil
63
69
  end
64
- # NOTE end of common tasks
65
70
 
66
- if career
67
- career.battle_tag = battle_tag
68
- career.region = region
71
+ return career
72
+ end
69
73
 
70
- # Association tasks (TODO: convert to hook)
71
- if raw_response["heroes"]
72
- heroes = response["heroes"].collect do |raw_hero_attrs|
73
- hero = Bnet::Diablo3::Hero.from_api(raw_hero_attrs)
74
- hero.career = career
75
- hero.battle_tag = career.battle_tag
76
- hero.region = career.region
77
- hero.reload
78
- hero
79
- end
74
+ def self.from_api(response)
75
+ career = super(response)
76
+ career.raw_attributes = response
77
+ return career
78
+ end
80
79
 
81
- career.heroes = heroes
82
- end
83
- # End of association tasks
80
+ private
84
81
 
82
+ def self.assign_heroes_from_raw_heroes(career, raw_heroes)
83
+ career.heroes = raw_heroes.collect do |raw_hero_attrs|
84
+ hero = Bnet::Diablo3::Hero.from_api(raw_hero_attrs)
85
+ hero.career = career
86
+ hero.battle_tag = career.battle_tag
87
+ hero.region = career.region
88
+ hero.reload
89
+ hero
85
90
  end
86
91
 
87
92
  return career
88
93
  end
89
94
 
90
-
91
95
  end
@@ -0,0 +1,42 @@
1
+ class Bnet::Diablo3::Follower < Bnet::BnetResource
2
+ attr_accessor :follower_type, :level, :raw_attributes,
3
+ :magic_find, :gold_find, :experience_bonus,
4
+
5
+ #associations
6
+ :items, :skills
7
+
8
+ def self.from_api(follower_type, raw_response)
9
+ follower = new(follower_type: follower_type)
10
+ follower.raw_attributes = raw_response
11
+ follower.level = raw_response["level"]
12
+
13
+ assign_items_from_raw_items(follower, raw_response["items"]) if raw_response["items"]
14
+ assign_skills_from_raw_skills(follower, raw_response["skills"]) if raw_response["skills"]
15
+
16
+ follower.magic_find = raw_response["stats"]["magicFind"]
17
+ follower.gold_find = raw_response["stats"]["goldFind"]
18
+ follower.experience_bonus = raw_response["stats"]["experienceBonus"]
19
+
20
+ return follower
21
+ end
22
+
23
+ private
24
+
25
+ def self.assign_items_from_raw_items(follower, raw_items)
26
+ follower.items = raw_items.collect do |location, item_props|
27
+ Bnet::Diablo3::Item.from_api(location, item_props)
28
+ end
29
+
30
+ return follower
31
+ end
32
+
33
+ def self.assign_skills_from_raw_skills(follower, raw_skills)
34
+ follower.skills = raw_skills.collect do |raw_skill|
35
+ Bnet::Diablo3::Skill.from_api(raw_skill) unless raw_skill.empty?
36
+ end
37
+
38
+ follower.skills = follower.skills.compact
39
+
40
+ return follower
41
+ end
42
+ end
@@ -3,7 +3,7 @@ class Bnet::Diablo3::Hero < Bnet::BnetResource
3
3
  attr_accessor :paragon_level, :seasonal, :name, :hero_id,
4
4
  :level, :hardcore, :gender, :dead, :hero_class, :last_update,
5
5
  :active_skills, :passive_skills, :region, :battle_tag, :career,
6
- :items,
6
+ :items, :followers, :raw_attributes,
7
7
 
8
8
  # stats
9
9
  :life, :damage, :attack_speed, :armor, :strength, :dexterity, :vitality,
@@ -24,7 +24,7 @@ class Bnet::Diablo3::Hero < Bnet::BnetResource
24
24
  "gender" => :gender,
25
25
  "dead" => :dead,
26
26
  "class" => :hero_class,
27
- "last-updated" => :last_updated,
27
+ "last-updated" => :last_updated
28
28
  }
29
29
 
30
30
 
@@ -111,10 +111,13 @@ class Bnet::Diablo3::Hero < Bnet::BnetResource
111
111
  def self.from_api(response)
112
112
  hero = super(response)
113
113
 
114
+ hero.raw_attributes = response
115
+
114
116
  if hero
115
117
  assign_skills_from_raw_skills(hero, response["skills"]) if response["skills"]
116
118
  assign_stats_from_raw_stats(hero, response["stats"]) if response["stats"]
117
119
  assign_items_from_raw_items(hero, response["items"]) if response["items"]
120
+ assign_followers_from_raw_followers(hero, response["followers"]) if response["followers"]
118
121
  end
119
122
 
120
123
  return hero
@@ -122,37 +125,29 @@ class Bnet::Diablo3::Hero < Bnet::BnetResource
122
125
 
123
126
  private
124
127
 
128
+ def self.assign_followers_from_raw_followers(hero, raw_followers)
129
+ hero.followers = raw_followers.collect do |follower_type, follower_props|
130
+ Bnet::Diablo3::Follower.from_api(follower_type, follower_props)
131
+ end
132
+
133
+ return hero
134
+ end
135
+
125
136
  def self.assign_items_from_raw_items(hero, raw_items)
126
137
  hero.items = raw_items.collect do |location, item_props|
127
- item = Bnet::Diablo3::Item.new
128
- item.location = location
129
- item.item_id = item_props["id"]
130
- item.name = item_props["name"]
131
- item
138
+ Bnet::Diablo3::Item.from_api(location, item_props)
132
139
  end
133
140
 
134
141
  return hero
135
142
  end
136
143
 
137
144
  def self.assign_skills_from_raw_skills(hero, raw_skills)
138
- hero.active_skills = raw_skills["active"].collect do |active|
139
- skill = Bnet::Diablo3::Skill.new
140
- if active["skill"]
141
- skill.name = active["skill"]["name"]
142
- end
143
- if active["rune"]
144
- skill.rune = active["rune"]["name"]
145
- end
146
-
147
- skill
145
+ hero.active_skills = raw_skills["active"].collect do |active_skill|
146
+ Bnet::Diablo3::Skill.from_api(active_skill) unless active_skill.empty?
148
147
  end
149
148
 
150
- hero.passive_skills = raw_skills["passive"].collect do |passive|
151
- skill = Bnet::Diablo3::Skill.new
152
- if passive["skill"]
153
- skill.name = passive["skill"]["name"]
154
- end
155
- skill
149
+ hero.passive_skills = raw_skills["passive"].collect do |passive_skill|
150
+ Bnet::Diablo3::Skill.from_api(passive_skill) unless passive_skill.empty?
156
151
  end
157
152
 
158
153
  return hero
@@ -1,3 +1,11 @@
1
- class Bnet::Diablo3::Item
1
+ class Bnet::Diablo3::Item < Bnet::BnetResource
2
2
  attr_accessor :item_id, :location, :name
3
+
4
+ def self.from_api(location, raw_response)
5
+ item = new(location: location)
6
+ item.item_id = raw_response["id"]
7
+ item.name = raw_response["name"]
8
+
9
+ return item
10
+ end
3
11
  end
@@ -11,9 +11,13 @@ class Bnet::Diablo3::Skill < Bnet::BnetResource
11
11
  #
12
12
  # Returns:
13
13
  #
14
- # #<Bnet::Diablo3::Skill:0x007fd111396360 @name: "", @rune=>
14
+ # #<Bnet::Diablo3::Skill:0x007fd111396360 @name: "", @rune: "">
15
15
  def self.from_api(response)
16
- new(name: response['skill']['name'], rune: response['rune']['name'])
16
+ skill = new
17
+ skill.name = response['skill']['name']
18
+ skill.rune = response['rune']['name'] if response["rune"]
19
+
20
+ return skill
17
21
  end
18
22
 
19
23
  end
@@ -0,0 +1,21 @@
1
+ class Bnet::Starcraft2::Career < Bnet::BnetResource
2
+ attr_accessor :primary_race, :terran_wins, :protoss_wins, :zerg_wins,
3
+ :highest_1v1_rank, :highest_team_rank, :season_total_games,
4
+ :career_total_games
5
+
6
+ PARAMS_MAPPING = {
7
+ 'primaryRace' => :primary_race,
8
+ 'terranWins' => :terran_wins,
9
+ 'protosswins' => :protoss_wins,
10
+ 'zergWins' => :zerg_wins,
11
+ 'highest1v1Rank' => :highest_1v1_rank,
12
+ 'highestTeamRank' => :highest_team_rank,
13
+ "seasonTotalGames" => :season_total_games,
14
+ "careerTotalGames" => :career_total_games
15
+ }
16
+
17
+ def self.from_api(raw_response)
18
+ career = super(raw_response)
19
+ return career
20
+ end
21
+ end
@@ -0,0 +1,48 @@
1
+ class Bnet::Starcraft2::Match < Bnet::BnetResource
2
+ attr_accessor :map, :match_type, :decision, :speed, :date, :raw_attributes
3
+
4
+ PARAMS_MAPPING = {
5
+ "map" => :map,
6
+ "type" => :match_type,
7
+ "decision" => :decision,
8
+ "speed" => :speed,
9
+ "date" => :date
10
+ }
11
+
12
+ def self.all(profile, args = {})
13
+ profile_id = profile.profile_id
14
+ name = profile.name
15
+ realm = profile.realm || '1'
16
+ locale = args[:locale] || 'en_US'
17
+ api_key = args[:api_key] || Bnet.configuration.api_key
18
+
19
+ client = Bnet::Starcraft2.new(region: profile.region)
20
+ call_url = client.url + "profile/#{profile_id}/#{realm}/#{name}/matches?apikey=#{api_key}&locale=#{locale}"
21
+
22
+ begin
23
+ data = open(call_url)
24
+ raw_collection_response = JSON.parse(data.read)
25
+
26
+ if Bnet::API.valid_call?(data.status, raw_collection_response)
27
+ matches = raw_collection_response["matches"].collect do |raw_response|
28
+ match = from_api(raw_response)
29
+ match
30
+ end
31
+ else
32
+ matches = []
33
+ end
34
+
35
+ rescue OpenURI::HTTPError => e
36
+ matches = []
37
+ end
38
+
39
+ return matches
40
+
41
+ end
42
+
43
+ def self.from_api(raw_response)
44
+ match = super(raw_response)
45
+ match.raw_attributes = raw_response
46
+ match
47
+ end
48
+ end
@@ -1,17 +1,17 @@
1
1
  # TODO: Associations for career, current_season
2
2
  class Bnet::Starcraft2::Profile < Bnet::BnetResource
3
3
 
4
- attr_accessor :profile_id, :realm, :display_name, :clan_name, :clan_tag,
4
+ attr_accessor :profile_id, :realm, :name, :clan_name, :clan_tag,
5
5
  :achievement_points, :swarm_level, :terran_level, :zerg_level,
6
- :protoss_level, :acievement_points
6
+ :protoss_level, :acievement_points, :career, :region,
7
+ :raw_attributes
7
8
 
8
9
  PARAMS_MAPPING = {
9
10
  "id" => :profile_id,
10
11
  "realm" => :realm,
11
- "displayName" => :display_name,
12
+ "displayName" => :name,
12
13
  "clanName" => :clan_name,
13
- "clanTag" => :clan_tag,
14
- "career" => :career
14
+ "clanTag" => :clan_tag
15
15
  }
16
16
 
17
17
  def initialize args
@@ -25,13 +25,14 @@ class Bnet::Starcraft2::Profile < Bnet::BnetResource
25
25
  #
26
26
  # Hash Params:
27
27
  # Required
28
- # :realm - (required but defaults to '1')
29
- # :profile_id - ID (Honestly i do not know why Blizzard still needs this if
30
- # localized Battletag is unique enough)
31
- # :name - Just the name string in the Battle tag.
28
+ # :realm - (required but defaults to '1')
29
+ # :profile_id - ID (Honestly i do not know why Blizzard still needs this if
30
+ # localized Battletag is unique enough)
31
+ # :name - Just the name string in the Battle tag.
32
+ #
32
33
  # Optional
33
- # :locale - (defaults to 'en_US')
34
- # :api_key - the api key
34
+ # :locale - (defaults to 'en_US')
35
+ # :api_key - the api key
35
36
  #
36
37
  # Example: If US account 'Playerone#1309' the profile can be accessible via
37
38
  # web from 'http://us.battle.net/sc2/en/profile/2143215/1/PlayerOne/'
@@ -40,16 +41,16 @@ class Bnet::Starcraft2::Profile < Bnet::BnetResource
40
41
  #
41
42
  # Returns a Profile object with the following attributes
42
43
  #
43
- # :profile_id, :realm, :display_name, :clan_name, :clan_tag,
44
+ # :profile_id, :realm, :name, :clan_name, :clan_tag,
44
45
  # :achievement_points, :swarm_level, :terran_level, :zerg_level,
45
46
  # :protoss_level, :acievement_points
46
47
  def self.find args
47
- region = args.delete(:region)
48
- profile_id = args.delete(:profile_id)
49
- name = args.delete(:name)
50
- realm = args.delete(:realm) || '1'
51
- locale = args.delete(:locale) || 'en_US'
52
- api_key = args.delete(:api_key) || Bnet.configuration.api_key
48
+ region = args[:region]
49
+ profile_id = args[:profile_id]
50
+ name = args[:name]
51
+ realm = args[:realm] || '1'
52
+ locale = args[:locale] || 'en_US'
53
+ api_key = args[:api_key] || Bnet.configuration.api_key
53
54
 
54
55
  base_api = Bnet::Starcraft2.new(region: region)
55
56
  call_url = base_api.url + "profile/#{profile_id}/#{realm}/#{name}/?locale=#{locale}&apikey=#{api_key}"
@@ -60,6 +61,8 @@ class Bnet::Starcraft2::Profile < Bnet::BnetResource
60
61
 
61
62
  if Bnet::API.valid_call?(data.status, raw_response)
62
63
  bnet_object = from_api(raw_response)
64
+ bnet_object.raw_attributes = raw_response
65
+ bnet_object.region = region
63
66
  else
64
67
  bnet_object = nil
65
68
  end
@@ -76,6 +79,14 @@ class Bnet::Starcraft2::Profile < Bnet::BnetResource
76
79
  @career ||= []
77
80
  end
78
81
 
82
+ def matches
83
+ @matches ||= Bnet::Starcraft2::Match.all(self)
84
+ end
85
+
86
+ def ladders
87
+ # @ladders ||=
88
+ end
89
+
79
90
  def self.from_api(response)
80
91
  bnet_resource = super(response)
81
92
  if bnet_resource && response["achievements"]
@@ -89,6 +100,16 @@ class Bnet::Starcraft2::Profile < Bnet::BnetResource
89
100
  bnet_resource.zerg_level = response["swarmLevels"]["zerg"]["level"]
90
101
  end
91
102
 
103
+ assign_career_from_raw_career(bnet_resource, response["career"]) if response["career"]
104
+
92
105
  bnet_resource
93
106
  end
107
+
108
+ private
109
+
110
+ def self.assign_career_from_raw_career(profile, raw_career)
111
+ profile.career = Bnet::Starcraft2::Career.from_api(raw_career)
112
+ return profile
113
+ end
114
+
94
115
  end