starcraft2 0.0.10 → 0.1.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.
@@ -7,13 +7,25 @@ module Starcraft2
7
7
  attr_accessor :locale, :host
8
8
 
9
9
  def initialize(options = {})
10
- options.each do |k,v|
10
+ options.each do |k, v|
11
11
  self.send(:"#{k}=", v)
12
12
  end
13
13
 
14
14
  self.host = 'us.battle.net' if self.host.nil?
15
15
  end
16
16
 
17
+ def profile(options = {})
18
+ if (args = [:character_name, :id, :realm] - options.keys).empty?
19
+ Profile.build(self, profile_json(options))
20
+ else
21
+ raise MissingArgumentsError, "Missing Keys: #{args.map {|i| ":#{i}"}.join(', ')}"
22
+ end
23
+ end
24
+
25
+ def matches(options = {})
26
+ Profile::Match.build(match_json(options))
27
+ end
28
+
17
29
  def achievements
18
30
  Achievement.build(achievements_json)
19
31
  end
@@ -36,6 +48,26 @@ module Starcraft2
36
48
 
37
49
  private
38
50
 
51
+ def profile_json(options)
52
+ HTTParty.get(profile_url(options)).body
53
+ end
54
+
55
+ def profile_url(options)
56
+ 'https://' + host + profile_path(options) + locale_param
57
+ end
58
+
59
+ def profile_path(options)
60
+ "/api/sc2/profile/#{options[:id]}/#{options[:realm]}/#{options[:character_name]}/" + locale_param
61
+ end
62
+
63
+ def match_json(options)
64
+ HTTParty.get(match_url(options)).body
65
+ end
66
+
67
+ def match_url(options)
68
+ 'https://' + host + profile_path(options) + 'matches' + locale_param
69
+ end
70
+
39
71
  def achievements_json
40
72
  HTTParty.get(achievements_url).body
41
73
  end
@@ -64,4 +96,4 @@ module Starcraft2
64
96
  locale.nil? ? '' : "?locale=#{locale}"
65
97
  end
66
98
  end
67
- end
99
+ end
@@ -0,0 +1,4 @@
1
+ module Starcraft2
2
+ class MissingArgumentsError < StandardError
3
+ end
4
+ end
@@ -2,6 +2,7 @@ require 'ostruct'
2
2
  require 'httparty'
3
3
  require 'json'
4
4
 
5
+ require File.join('starcraft2', 'errors', 'missing_arguments_error')
5
6
  require File.join('starcraft2', 'utils')
6
7
  require File.join('starcraft2', 'icon')
7
8
  require File.join('starcraft2', 'client')
@@ -9,4 +10,16 @@ require File.join('starcraft2', 'achievement')
9
10
  require File.join('starcraft2', 'reward')
10
11
  require File.join('starcraft2', 'ladder')
11
12
  require File.join('starcraft2', 'member')
12
- require File.join('starcraft2', 'character')
13
+ require File.join('starcraft2', 'character')
14
+ require File.join('starcraft2', 'profile', 'match')
15
+ require File.join('starcraft2', 'profile', 'swarm_race')
16
+ require File.join('starcraft2', 'profile', 'swarm_levels')
17
+ require File.join('starcraft2', 'profile', 'career')
18
+ require File.join('starcraft2', 'profile', 'campaign')
19
+ require File.join('starcraft2', 'profile', 'season')
20
+ require File.join('starcraft2', 'profile', 'stats')
21
+ require File.join('starcraft2', 'profile', 'rewards')
22
+ require File.join('starcraft2', 'profile', 'achievements')
23
+ require File.join('starcraft2', 'profile', 'points')
24
+ require File.join('starcraft2', 'profile', 'achievement_item')
25
+ require File.join('starcraft2', 'profile')
@@ -1,7 +1,7 @@
1
1
  module Starcraft2
2
2
  class Member
3
3
  attr_accessor :character, :join_timestamp, :points, :wins,
4
- :losses, :highest_rank, :previous_rank, :favorite_race_p1, :favorite_race_p2, :favorite_race_p3, :favorite_race_p4
4
+ :losses, :highest_rank, :previous_rank, :favorite_race_p1, :favorite_race_p2, :favorite_race_p3, :favorite_race_p4
5
5
 
6
6
  def initialize(options = {})
7
7
  Utils.load(self, options, {:character => Character})
@@ -0,0 +1,27 @@
1
+ module Starcraft2
2
+ class Profile
3
+ attr_accessor :id, :realm, :display_name, :clan_name, :clan_tag, :profile_path, :portrait,
4
+ :career, :swarm_levels, :campaign, :season, :rewards, :achievements, :client
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options, {
8
+ :portrait => Icon,
9
+ :campaign => Campaign,
10
+ :career => Career,
11
+ :swarm_levels => SwarmLevels,
12
+ :season => Season,
13
+ :rewards => Rewards,
14
+ :achievements => Achievements
15
+ })
16
+ end
17
+
18
+ def self.build(client, profile_json)
19
+ profile = JSON.parse(profile_json)
20
+ new(profile.merge!(:client => client))
21
+ end
22
+
23
+ def matches
24
+ client.matches(:character_name => self.display_name, :id => self.id, :realm => self.realm)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class AchievementItem
4
+ attr_accessor :achievement_id, :completion_date
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options)
8
+ end
9
+
10
+ def self.build(achievement_item_data)
11
+ achievement_item_data.map do |a|
12
+ new(a)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class Achievements
4
+ attr_accessor :points, :achievements
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options, { :points => Points }, {:achievements => AchievementItem})
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class Campaign
4
+ attr_accessor :wol, :hots
5
+
6
+ def initialize(options)
7
+ Utils.load(self, options)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class Career
4
+ attr_accessor :primary_race, :league, :terran_wins, :protoss_wins, :zerg_wins,
5
+ :highest1v1_rank, :highest_team_rank, :season_total_games, :career_total_games
6
+
7
+ def initialize(options)
8
+ Utils.load(self, options)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class Match
4
+ attr_accessor :map, :type, :decision, :speed, :date
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options)
8
+ end
9
+
10
+ def self.build(match_json)
11
+ data = JSON.parse(match_json)
12
+ data['matches'].map do |m|
13
+ new(m)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class Points
4
+ attr_accessor :total_points, :category_points
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options, {:category_points => Hash })
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class Rewards
4
+ attr_accessor :selected, :earned
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class Season
4
+ attr_accessor :season_id, :total_games_this_season, :stats
5
+
6
+ def initialize(options={})
7
+ Utils.load(self, options, {}, {:stats => Stats})
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class Stats
4
+ attr_accessor :type, :wins, :games
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options)
8
+ end
9
+
10
+ def self.build(stats_data)
11
+ stats_data.map do |s|
12
+ new(s)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class SwarmLevels
4
+ attr_accessor :level, :terran, :zerg, :protoss
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options, {:terran => SwarmRace, :zerg => SwarmRace, :protoss => SwarmRace})
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Starcraft2
2
+ class Profile
3
+ class SwarmRace
4
+ attr_accessor :level, :total_level_xp, :current_level_xp
5
+
6
+ def initialize(options = {})
7
+ Utils.load(self, options)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -10,12 +10,15 @@ module Starcraft2
10
10
  word
11
11
  end
12
12
 
13
- def self.load(klass, options, class_map = {})
13
+ def self.load(klass, options, class_map = {}, build_map = {})
14
14
  options.each do |k, v|
15
- v = class_map[k.to_sym].new(v) if class_map.keys.include?(k.to_sym)
15
+ k = self.underscore(k.to_s)
16
+ k = k.to_sym
17
+ v = class_map[k].new(v) if class_map.keys.include?(k)
18
+ v = build_map[k].build(v) if build_map.keys.include?(k)
16
19
 
17
- klass.send(:"#{self.underscore(k.to_s)}=", v)
20
+ klass.send(:"#{k}=", v)
18
21
  end
19
22
  end
20
23
  end
21
- end
24
+ end
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Starcraft2::Client do
4
- let(:client) { Starcraft2::Client.new(@options)}
4
+ let(:client) { Starcraft2::Client.new(@options) }
5
5
 
6
6
  before do
7
7
  @options = {:host => 'us.battle.net'}
@@ -23,16 +23,39 @@ describe Starcraft2::Client do
23
23
  end
24
24
 
25
25
  it 'should store the locale' do
26
- @options = { :locale => 'en_US' }
26
+ @options = {:locale => 'en_US'}
27
27
  client.locale.should == 'en_US'
28
28
  end
29
29
 
30
30
  it 'should store the host' do
31
- @options = { :host => 'someserver.com'}
31
+ @options = {:host => 'someserver.com'}
32
32
  client.host.should == 'someserver.com'
33
33
  end
34
34
  end
35
35
 
36
+ describe '.profile' do
37
+ it 'should return an sc2 profile' do
38
+ VCR.use_cassette("profile_#{999000}") do
39
+ options = {:character_name => 'DayNine', :id => 999000, :realm => 1}
40
+ client.profile(options).class.should == Starcraft2::Profile
41
+ end
42
+ end
43
+
44
+ it 'should error with invalid arguments' do
45
+ expect {
46
+ client.profile({})
47
+ }.to raise_error(Starcraft2::MissingArgumentsError, 'Missing Keys: :character_name, :id, :realm')
48
+
49
+ expect {
50
+ client.profile({:character_name => 'DayNine'})
51
+ }.to raise_error(Starcraft2::MissingArgumentsError, 'Missing Keys: :id, :realm')
52
+ end
53
+ end
54
+
55
+ VCR.use_cassette("profile_#{999000}") do
56
+ @profile_json = HTTParty.get('https://us.battle.net/api/sc2/profile/999000/1/DayNine/').body
57
+ end
58
+
36
59
  describe '.achievements' do
37
60
  it 'should return an array of achievements' do
38
61
  VCR.use_cassette('achievements') do
@@ -4,12 +4,12 @@ describe Starcraft2::Member do
4
4
  describe '.initialize' do
5
5
  let(:member) { Starcraft2::Member.new(@options) }
6
6
  let(:character) { {
7
- 'id' => 333319,
8
- 'realm' => 1,
9
- 'displayName' => 'NajM',
10
- 'clanName' => '',
11
- 'clanTag' => '',
12
- 'profilePath' => '/profile/333319/1/NajM/'
7
+ 'id' => 333319,
8
+ 'realm' => 1,
9
+ 'displayName' => 'NajM',
10
+ 'clanName' => '',
11
+ 'clanTag' => '',
12
+ 'profilePath' => '/profile/333319/1/NajM/'
13
13
  } }
14
14
 
15
15
  before do
@@ -20,7 +20,7 @@ describe Starcraft2::Member do
20
20
  @options = {:character => character}
21
21
  member.character.class.should == Starcraft2::Character
22
22
 
23
- member.character.id .should == 333319
23
+ member.character.id.should == 333319
24
24
  member.character.realm.should == 1
25
25
  member.character.display_name.should == 'NajM'
26
26
  member.character.clan_name.should == ''
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Starcraft2::Profile::Achievements do
4
+ let(:achievements) { Starcraft2::Profile::Achievements.new(@options) }
5
+
6
+ before do
7
+ @options = {}
8
+ end
9
+
10
+ it 'should store points' do
11
+ @options = {:points => {
12
+ :total_points => 3420,
13
+ :categoryPoints => {
14
+ "4325382" => 0,
15
+ "4325380" => 80,
16
+ "4325408" => 0,
17
+ "4325379" => 1560,
18
+ "4325410" => 1280,
19
+ "4325377" => 500
20
+ }
21
+ }}
22
+ achievements.points.total_points.should == 3420
23
+ end
24
+
25
+ it 'should store achievement_items' do
26
+ @options = {:achievements => [{ :achievement_id => 91475035553845, :completion_date => 1365790265},
27
+ { :achievement_id => 91475320768475, :completion_date => 1365392587},
28
+ { :achievement_id => 91475320768585, :completion_date => 1364634012}]}
29
+
30
+ achievements.achievements.class.should == Array
31
+ achievements.achievements.first.class.should == Starcraft2::Profile::AchievementItem
32
+ achievements.achievements.first.achievement_id.should == 91475035553845
33
+ achievements.achievements.first.completion_date.should == 1365790265
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Starcraft2::Profile::Campaign do
4
+ let(:campaign) { Starcraft2::Profile::Campaign.new(@options) }
5
+
6
+ before do
7
+ @options = {}
8
+ end
9
+
10
+ describe '.initialize' do
11
+ it 'should store wol' do
12
+ @options = {:wol => 'BRUTAL'}
13
+ campaign.wol.should == 'BRUTAL'
14
+ end
15
+
16
+ it 'should store hots' do
17
+ @options = {:hots => 'BRUTAL'}
18
+ campaign.hots.should == 'BRUTAL'
19
+ end
20
+ end
21
+ end