starcraft2 0.1.1 → 0.2.0

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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/fixtures/cassettes/ladders_999000.yml +1053 -0
  3. data/lib/starcraft2/character.rb +7 -1
  4. data/lib/starcraft2/client.rb +33 -19
  5. data/lib/starcraft2/loader.rb +4 -0
  6. data/lib/starcraft2/profile.rb +5 -1
  7. data/lib/starcraft2/profile/achievement_item.rb +3 -3
  8. data/lib/starcraft2/profile/achievements.rb +2 -2
  9. data/lib/starcraft2/profile/campaign.rb +2 -2
  10. data/lib/starcraft2/profile/career.rb +2 -2
  11. data/lib/starcraft2/profile/detailed_season.rb +17 -0
  12. data/lib/starcraft2/profile/ladder.rb +17 -0
  13. data/lib/starcraft2/profile/ladders.rb +11 -0
  14. data/lib/starcraft2/profile/match.rb +1 -1
  15. data/lib/starcraft2/profile/non_rank.rb +18 -0
  16. data/lib/starcraft2/profile/points.rb +1 -1
  17. data/lib/starcraft2/profile/season.rb +1 -1
  18. data/lib/starcraft2/profile/stats.rb +2 -2
  19. data/spec/starcraft2/achievement_spec.rb +42 -45
  20. data/spec/starcraft2/character_spec.rb +37 -19
  21. data/spec/starcraft2/client_spec.rb +2 -4
  22. data/spec/starcraft2/icon_spec.rb +7 -22
  23. data/spec/starcraft2/member_spec.rb +24 -54
  24. data/spec/starcraft2/profile/achievement_item_spec.rb +48 -0
  25. data/spec/starcraft2/profile/achievements_spec.rb +36 -27
  26. data/spec/starcraft2/profile/campaign_spec.rb +8 -7
  27. data/spec/starcraft2/profile/career_spec.rb +30 -47
  28. data/spec/starcraft2/profile/detailed_season_spec.rb +98 -0
  29. data/spec/starcraft2/profile/ladder_spec.rb +63 -0
  30. data/spec/starcraft2/profile/ladders_spec.rb +197 -0
  31. data/spec/starcraft2/profile/match_spec.rb +41 -22
  32. data/spec/starcraft2/profile/non_rank_spec.rb +48 -0
  33. data/spec/starcraft2/profile/points_spec.rb +22 -0
  34. data/spec/starcraft2/profile/rewards_spec.rb +17 -11
  35. data/spec/starcraft2/profile/season_spec.rb +33 -23
  36. data/spec/starcraft2/profile/stats_spec.rb +49 -0
  37. data/spec/starcraft2/profile/swarm_levels_spec.rb +29 -16
  38. data/spec/starcraft2/profile/swarm_race_spec.rb +25 -0
  39. data/spec/starcraft2/profile_spec.rb +80 -73
  40. data/spec/starcraft2/reward_spec.rb +22 -31
  41. data/spec/starcraft2/utils_spec.rb +30 -0
  42. data/starcraft2.gemspec +1 -1
  43. metadata +15 -2
@@ -5,5 +5,11 @@ module Starcraft2
5
5
  def initialize(options = {})
6
6
  Utils.load(self, options)
7
7
  end
8
+
9
+ def self.build(characters)
10
+ characters.map do |c|
11
+ new(c)
12
+ end
13
+ end
8
14
  end
9
- end
15
+ end
@@ -16,26 +16,22 @@ module Starcraft2
16
16
 
17
17
  def profile(options = {})
18
18
  if (args = [:character_name, :id, :realm] - options.keys).empty?
19
- Profile.build(self, profile_json(options))
19
+ Profile.build(self, profile_data(options))
20
20
  else
21
21
  raise MissingArgumentsError, "Missing Keys: #{args.map {|i| ":#{i}"}.join(', ')}"
22
22
  end
23
23
  end
24
24
 
25
- def matches(options = {})
26
- Profile::Match.build(match_json(options))
27
- end
28
-
29
25
  def achievements
30
- Achievement.build(achievements_json)
26
+ Achievement.build(achievements_data)
31
27
  end
32
28
 
33
29
  def rewards
34
- Reward.build(rewards_json)
30
+ Reward.build(rewards_data)
35
31
  end
36
32
 
37
33
  def ladder(id)
38
- Ladder.build(ladder_json(id))
34
+ Ladder.build(ladder_data(id))
39
35
  end
40
36
 
41
37
  def grandmaster_ladder
@@ -48,8 +44,16 @@ module Starcraft2
48
44
 
49
45
  private
50
46
 
51
- def profile_json(options)
52
- get_body do
47
+ def matches(options = {})
48
+ Profile::Match.build(match_data(options))
49
+ end
50
+
51
+ def ladders(options = {})
52
+ Profile::Ladders.new(ladders_data(options))
53
+ end
54
+
55
+ def profile_data(options)
56
+ get_json do
53
57
  HTTParty.get(profile_url(options))
54
58
  end
55
59
  end
@@ -62,18 +66,28 @@ module Starcraft2
62
66
  "/api/sc2/profile/#{options[:id]}/#{options[:realm]}/#{options[:character_name]}/" + locale_param
63
67
  end
64
68
 
65
- def match_json(options)
66
- get_body do
69
+ def match_data(options)
70
+ get_json do
67
71
  HTTParty.get(match_url(options))
68
72
  end
69
73
  end
70
74
 
75
+ def ladders_data(options)
76
+ get_json do
77
+ HTTParty.get(ladders_url(options))
78
+ end
79
+ end
80
+
71
81
  def match_url(options)
72
82
  'https://' + host + profile_path(options) + 'matches' + locale_param
73
83
  end
74
84
 
75
- def achievements_json
76
- get_body do
85
+ def ladders_url(options)
86
+ 'https://' + host + profile_path(options) + 'ladders' + locale_param
87
+ end
88
+
89
+ def achievements_data
90
+ get_json do
77
91
  HTTParty.get(achievements_url)
78
92
  end
79
93
  end
@@ -82,8 +96,8 @@ module Starcraft2
82
96
  'https://' + host + ACHIEVEMENTS_PATH + locale_param
83
97
  end
84
98
 
85
- def rewards_json
86
- get_body do
99
+ def rewards_data
100
+ get_json do
87
101
  HTTParty.get(rewards_url)
88
102
  end
89
103
  end
@@ -92,8 +106,8 @@ module Starcraft2
92
106
  'https://' + host + REWARDS_PATH + locale_param
93
107
  end
94
108
 
95
- def ladder_json(id)
96
- get_body do
109
+ def ladder_data(id)
110
+ get_json do
97
111
  HTTParty.get(ladder_url(id))
98
112
  end
99
113
  end
@@ -106,7 +120,7 @@ module Starcraft2
106
120
  locale.nil? ? '' : "?locale=#{locale}"
107
121
  end
108
122
 
109
- def get_body
123
+ def get_json
110
124
  response = yield
111
125
  case response.code
112
126
  when 200
@@ -24,4 +24,8 @@ require File.join('starcraft2', 'profile', 'rewards')
24
24
  require File.join('starcraft2', 'profile', 'achievements')
25
25
  require File.join('starcraft2', 'profile', 'points')
26
26
  require File.join('starcraft2', 'profile', 'achievement_item')
27
+ require File.join('starcraft2', 'profile', 'ladders')
28
+ require File.join('starcraft2', 'profile', 'detailed_season')
29
+ require File.join('starcraft2', 'profile', 'non_rank')
30
+ require File.join('starcraft2', 'profile', 'ladder')
27
31
  require File.join('starcraft2', 'profile')
@@ -20,7 +20,11 @@ module Starcraft2
20
20
  end
21
21
 
22
22
  def matches
23
- client.matches(:character_name => self.display_name, :id => self.id, :realm => self.realm)
23
+ client.send(:matches, {:character_name => self.display_name, :id => self.id, :realm => self.realm})
24
+ end
25
+
26
+ def ladders
27
+ client.send(:ladders, {:character_name => self.display_name, :id => self.id, :realm => self.realm})
24
28
  end
25
29
  end
26
30
  end
@@ -7,11 +7,11 @@ module Starcraft2
7
7
  Utils.load(self, options)
8
8
  end
9
9
 
10
- def self.build(achievement_item_data)
11
- achievement_item_data.map do |a|
10
+ def self.build(achievements)
11
+ achievements.map do |a|
12
12
  new(a)
13
13
  end
14
14
  end
15
15
  end
16
16
  end
17
- end
17
+ end
@@ -4,8 +4,8 @@ module Starcraft2
4
4
  attr_accessor :points, :achievements
5
5
 
6
6
  def initialize(options = {})
7
- Utils.load(self, options, { :points => Points }, {:achievements => AchievementItem})
7
+ Utils.load(self, options, {:points => Points}, {:achievements => AchievementItem})
8
8
  end
9
9
  end
10
10
  end
11
- end
11
+ end
@@ -3,9 +3,9 @@ module Starcraft2
3
3
  class Campaign
4
4
  attr_accessor :wol, :hots
5
5
 
6
- def initialize(options)
6
+ def initialize(options = {})
7
7
  Utils.load(self, options)
8
8
  end
9
9
  end
10
10
  end
11
- end
11
+ end
@@ -4,9 +4,9 @@ module Starcraft2
4
4
  attr_accessor :primary_race, :league, :terran_wins, :protoss_wins, :zerg_wins,
5
5
  :highest1v1_rank, :highest_team_rank, :season_total_games, :career_total_games
6
6
 
7
- def initialize(options)
7
+ def initialize(options = {})
8
8
  Utils.load(self, options)
9
9
  end
10
10
  end
11
11
  end
12
- end
12
+ end
@@ -0,0 +1,17 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class DetailedSeason
4
+ attr_accessor :ladder, :characters, :non_ranked
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options, {}, {:ladder => Ladder, :characters => ::Starcraft2::Character, :non_ranked => NonRank})
8
+ end
9
+
10
+ def self.build(detailed_season)
11
+ detailed_season.map do |ds|
12
+ new(ds)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class Ladder
4
+ attr_accessor :ladder_name, :ladder_id, :division, :rank, :league, :match_making_queue, :wins, :losses, :showcase
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options)
8
+ end
9
+
10
+ def self.build(ladders)
11
+ ladders.map do |l|
12
+ new(l)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class Ladders
4
+ attr_accessor :current_season, :previous_season, :showcase_placement
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options, {}, {:current_season => DetailedSeason, :previous_season => DetailedSeason})
8
+ end
9
+ end
10
+ end
11
+ end
@@ -14,4 +14,4 @@ module Starcraft2
14
14
  end
15
15
  end
16
16
  end
17
- end
17
+ end
@@ -0,0 +1,18 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class NonRank
4
+ attr_accessor :mmq, :games_played
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options)
8
+ end
9
+
10
+ def self.build(non_rank_data)
11
+ non_rank_data.map do |nr|
12
+ new(nr)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -4,7 +4,7 @@ module Starcraft2
4
4
  attr_accessor :total_points, :category_points
5
5
 
6
6
  def initialize(options = {})
7
- Utils.load(self, options, {:category_points => Hash })
7
+ Utils.load(self, options)
8
8
  end
9
9
  end
10
10
  end
@@ -3,7 +3,7 @@ module Starcraft2
3
3
  class Season
4
4
  attr_accessor :season_id, :total_games_this_season, :stats
5
5
 
6
- def initialize(options={})
6
+ def initialize(options = {})
7
7
  Utils.load(self, options, {}, {:stats => Stats})
8
8
  end
9
9
  end
@@ -7,8 +7,8 @@ module Starcraft2
7
7
  Utils.load(self, options)
8
8
  end
9
9
 
10
- def self.build(stats_data)
11
- stats_data.map do |s|
10
+ def self.build(stats)
11
+ stats.map do |s|
12
12
  new(s)
13
13
  end
14
14
  end
@@ -1,13 +1,45 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Starcraft2::Achievement do
4
+ describe '.initialize' do
5
+ let(:achievement) { Starcraft2::Achievement.new(@options) }
6
+
7
+ before do
8
+ @options = {
9
+ 'title' => 'Test Title',
10
+ 'description' => 'Description',
11
+ 'achievementId' => 5,
12
+ 'categoryId' => 10,
13
+ 'points' => 5,
14
+ 'icon' => {'x' => 1, 'y' => 2, 'w' => 3, 'h' => 4, 'offset' => 0, 'url' => 'https://example.com'}
15
+ }
16
+ end
17
+
18
+ it 'should store attributes as underscored' do
19
+ achievement.title.should == 'Test Title'
20
+ achievement.description.should == 'Description'
21
+ achievement.achievement_id.should == 5
22
+ achievement.category_id.should == 10
23
+ achievement.points.should == 5
24
+ achievement.icon.x.should == 1
25
+ achievement.icon.y.should == 2
26
+ achievement.icon.w.should == 3
27
+ achievement.icon.h.should == 4
28
+ achievement.icon.offset.should == 0
29
+ achievement.icon.url.should == 'https://example.com'
30
+ end
31
+ end
32
+
4
33
  describe '#build' do
5
- let(:achievements) { Starcraft2::Achievement.build(@achievements) }
34
+ let(:achievements) { Starcraft2::Achievement.build(@items) }
6
35
 
7
36
  before do
8
- VCR.use_cassette('achievements') do
9
- @achievements = JSON.parse(HTTParty.get('https://us.battle.net/api/sc2/data/achievements').body)
10
- end
37
+ @items = {
38
+ 'achievements' => [
39
+ {'title' => 'Test Title'},
40
+ {'title' => 'Another'}
41
+ ]
42
+ }
11
43
  end
12
44
 
13
45
  it 'should build an array of achievements' do
@@ -16,16 +48,16 @@ describe Starcraft2::Achievement do
16
48
  a.class.should == Starcraft2::Achievement
17
49
  end
18
50
  end
19
- end
20
51
 
21
- describe '.initialize' do
22
- let(:achievement) { Starcraft2::Achievement.new(@options) }
52
+ it 'should build items using new' do
53
+ @items['achievements'].each do |i|
54
+ Starcraft2::Achievement.should_receive(:new).with(i)
55
+ end
23
56
 
24
- before do
25
- @options = {}
57
+ achievements
26
58
  end
27
59
 
28
- it 'should import the first achievement' do
60
+ it 'should import the achievements from blizzard' do
29
61
  VCR.use_cassette('achievements') do
30
62
  @achievement = Starcraft2::Achievement.build(JSON.parse(HTTParty.get('https://us.battle.net/api/sc2/data/achievements').body)).first
31
63
  end
@@ -42,40 +74,5 @@ describe Starcraft2::Achievement do
42
74
  @achievement.icon.offset.should == 45
43
75
  @achievement.icon.url.should == 'http://media.blizzard.com/sc2/achievements/5-75.jpg'
44
76
  end
45
-
46
- it 'should store the title' do
47
- @options = {:title => 'Test Title'}
48
- achievement.title.should == 'Test Title'
49
- end
50
-
51
- it 'should store the description' do
52
- @options = {:description => 'Description'}
53
- achievement.description.should == 'Description'
54
- end
55
-
56
- it 'should store the achievement id' do
57
- @options = {:achievement_id => 5}
58
- achievement.achievement_id.should == 5
59
- end
60
-
61
- it 'should store the category id' do
62
- @options = {:category_id => 10}
63
- achievement.category_id.should == 10
64
- end
65
-
66
- it 'should store the points' do
67
- @options = {:points => 5}
68
- achievement.points.should == 5
69
- end
70
-
71
- it 'should store the icon data' do
72
- @options = {:icon => {'x' => 1, 'y' => 2, 'w' => 3, 'h' => 4, 'offset' => 0, 'url' => 'https://example.com'}}
73
- achievement.icon.x.should == 1
74
- achievement.icon.y.should == 2
75
- achievement.icon.w.should == 3
76
- achievement.icon.h.should == 4
77
- achievement.icon.offset.should == 0
78
- achievement.icon.url.should == 'https://example.com'
79
- end
80
77
  end
81
78
  end
@@ -5,37 +5,55 @@ describe Starcraft2::Character do
5
5
  let(:character) { Starcraft2::Character.new(@options) }
6
6
 
7
7
  before do
8
- @options = {}
8
+ @options = {
9
+ 'id' => 5,
10
+ 'realm' => 'somewhere',
11
+ 'displayName' => 'display me',
12
+ 'clanName' => 'clan name',
13
+ 'clanTag' => 'clan tag',
14
+ 'profilePath' => '/some/path'
15
+ }
9
16
  end
10
17
 
11
- it 'should store the id' do
12
- @options = {:id => 5}
18
+ it 'should store attributes as underscored' do
13
19
  character.id.should == 5
14
- end
15
-
16
- it 'should store the realm' do
17
- @options = {:realm => 'somewhere'}
18
20
  character.realm.should == 'somewhere'
21
+ character.display_name.should == 'display me'
22
+ character.clan_name.should == 'clan name'
23
+ character.clan_tag.should == 'clan tag'
24
+ character.profile_path.should == '/some/path'
19
25
  end
20
26
 
21
- it 'should store the display_name' do
22
- @options = {:display_name => 'display me'}
23
- character.display_name.should == 'display me'
27
+ it 'should use Stracraft2::Utils.load to populate the model' do
28
+ Starcraft2::Utils.should_receive(:load).with(anything, @options)
29
+
30
+ character
24
31
  end
32
+ end
25
33
 
26
- it 'should store the clan_name' do
27
- @options = {:clan_name => 'clan name'}
28
- character.clan_name.should == 'clan name'
34
+ describe '#build' do
35
+ let(:characters) { Starcraft2::Character.build(@items) }
36
+
37
+ before do
38
+ @items = [
39
+ {'id' => 1},
40
+ {'id' => 2}
41
+ ]
29
42
  end
30
43
 
31
- it 'should store the clan_tag' do
32
- @options = {:clan_tag => 'clan tag'}
33
- character.clan_tag.should == 'clan tag'
44
+ it 'should build multiple character objects' do
45
+ characters.class.should == Array
46
+ characters.each do |s|
47
+ s.class.should == Starcraft2::Character
48
+ end
34
49
  end
35
50
 
36
- it 'should store the profile_path' do
37
- @options = {:profile_path => '/some/path'}
38
- character.profile_path.should == '/some/path'
51
+ it 'should build items using new' do
52
+ @items.each do |i|
53
+ Starcraft2::Character.should_receive(:new).with(i)
54
+ end
55
+
56
+ characters
39
57
  end
40
58
  end
41
59
  end