starcraft2 0.2.0 → 0.2.1
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.
- checksums.yaml +4 -4
- data/README.md +43 -2
- data/fixtures/cassettes/last_grandmaster.yml +3237 -0
- data/lib/starcraft2/client.rb +37 -33
- data/lib/starcraft2/loader.rb +11 -28
- data/lib/starcraft2/utils.rb +20 -0
- data/spec/spec_helper.rb +5 -3
- data/spec/starcraft2/client_spec.rb +56 -20
- data/spec/starcraft2/profile_spec.rb +2 -1
- data/spec/starcraft2/reward_spec.rb +28 -29
- data/spec/starcraft2/utils_spec.rb +78 -0
- data/starcraft2.gemspec +1 -1
- metadata +2 -3
- data/fixtures/vcr_cassettes/grandmaster.yml +0 -3221
- data/fixtures/vcr_cassettes/previous_grandmaster.yml +0 -39
data/lib/starcraft2/client.rb
CHANGED
@@ -18,7 +18,7 @@ module Starcraft2
|
|
18
18
|
if (args = [:character_name, :id, :realm] - options.keys).empty?
|
19
19
|
Profile.build(self, profile_data(options))
|
20
20
|
else
|
21
|
-
raise MissingArgumentsError, "Missing Keys: #{args.map {|i| ":#{i}"}.join(', ')}"
|
21
|
+
raise MissingArgumentsError, "Missing Keys: #{args.map { |i| ":#{i}" }.join(', ')}"
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -42,6 +42,10 @@ module Starcraft2
|
|
42
42
|
ladder('grandmaster/last')
|
43
43
|
end
|
44
44
|
|
45
|
+
def flush_cache
|
46
|
+
@memoizations = {}
|
47
|
+
end
|
48
|
+
|
45
49
|
private
|
46
50
|
|
47
51
|
def matches(options = {})
|
@@ -53,8 +57,10 @@ module Starcraft2
|
|
53
57
|
end
|
54
58
|
|
55
59
|
def profile_data(options)
|
56
|
-
|
57
|
-
|
60
|
+
memoize(:profile_data, options) do
|
61
|
+
Utils.get_json do
|
62
|
+
HTTParty.get(profile_url(options))
|
63
|
+
end
|
58
64
|
end
|
59
65
|
end
|
60
66
|
|
@@ -67,14 +73,18 @@ module Starcraft2
|
|
67
73
|
end
|
68
74
|
|
69
75
|
def match_data(options)
|
70
|
-
|
71
|
-
|
76
|
+
memoize(:match_data, options) do
|
77
|
+
Utils.get_json do
|
78
|
+
HTTParty.get(match_url(options))
|
79
|
+
end
|
72
80
|
end
|
73
81
|
end
|
74
82
|
|
75
83
|
def ladders_data(options)
|
76
|
-
|
77
|
-
|
84
|
+
memoize(:ladders_data, options) do
|
85
|
+
Utils.get_json do
|
86
|
+
HTTParty.get(ladders_url(options))
|
87
|
+
end
|
78
88
|
end
|
79
89
|
end
|
80
90
|
|
@@ -87,8 +97,10 @@ module Starcraft2
|
|
87
97
|
end
|
88
98
|
|
89
99
|
def achievements_data
|
90
|
-
|
91
|
-
|
100
|
+
memoize(:achievements_data) do
|
101
|
+
Utils.get_json do
|
102
|
+
HTTParty.get(achievements_url)
|
103
|
+
end
|
92
104
|
end
|
93
105
|
end
|
94
106
|
|
@@ -97,8 +109,10 @@ module Starcraft2
|
|
97
109
|
end
|
98
110
|
|
99
111
|
def rewards_data
|
100
|
-
|
101
|
-
|
112
|
+
memoize(:rewards_data) do
|
113
|
+
Utils.get_json do
|
114
|
+
HTTParty.get(rewards_url)
|
115
|
+
end
|
102
116
|
end
|
103
117
|
end
|
104
118
|
|
@@ -107,8 +121,10 @@ module Starcraft2
|
|
107
121
|
end
|
108
122
|
|
109
123
|
def ladder_data(id)
|
110
|
-
|
111
|
-
|
124
|
+
memoize(:ladder_data, id) do
|
125
|
+
Utils.get_json do
|
126
|
+
HTTParty.get(ladder_url(id))
|
127
|
+
end
|
112
128
|
end
|
113
129
|
end
|
114
130
|
|
@@ -120,26 +136,14 @@ module Starcraft2
|
|
120
136
|
locale.nil? ? '' : "?locale=#{locale}"
|
121
137
|
end
|
122
138
|
|
123
|
-
def
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
body
|
131
|
-
when 404
|
132
|
-
raise Starcraft2::NotFoundError, body['message'] || body['reason']
|
133
|
-
when 500
|
134
|
-
raise Starcraft2::ApiError, body['message'] || body['reason']
|
135
|
-
else
|
136
|
-
body
|
137
|
-
end
|
138
|
-
when 404
|
139
|
-
raise Starcraft2::NotFoundError, 'Record not found.'
|
140
|
-
when 500
|
141
|
-
raise Starcraft2::ApiError, 'An API error occurred.'
|
139
|
+
def memoize(name, *args)
|
140
|
+
@memoizations ||= {}
|
141
|
+
@memoizations[name] ||= {}
|
142
|
+
if @memoizations[name].has_key?(args)
|
143
|
+
@memoizations[name][args]
|
144
|
+
else
|
145
|
+
@memoizations[name][args] = yield
|
142
146
|
end
|
143
147
|
end
|
144
148
|
end
|
145
|
-
end
|
149
|
+
end
|
data/lib/starcraft2/loader.rb
CHANGED
@@ -1,31 +1,14 @@
|
|
1
|
-
require 'ostruct'
|
2
1
|
require 'httparty'
|
3
2
|
require 'json'
|
4
3
|
|
5
|
-
|
6
|
-
require
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
require
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
require
|
15
|
-
|
16
|
-
require File.join('starcraft2', 'profile', 'match')
|
17
|
-
require File.join('starcraft2', 'profile', 'swarm_race')
|
18
|
-
require File.join('starcraft2', 'profile', 'swarm_levels')
|
19
|
-
require File.join('starcraft2', 'profile', 'career')
|
20
|
-
require File.join('starcraft2', 'profile', 'campaign')
|
21
|
-
require File.join('starcraft2', 'profile', 'season')
|
22
|
-
require File.join('starcraft2', 'profile', 'stats')
|
23
|
-
require File.join('starcraft2', 'profile', 'rewards')
|
24
|
-
require File.join('starcraft2', 'profile', 'achievements')
|
25
|
-
require File.join('starcraft2', 'profile', 'points')
|
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')
|
31
|
-
require File.join('starcraft2', 'profile')
|
4
|
+
Dir[File.join("#{File.expand_path File.dirname(__FILE__)}", 'errors', '*.rb')].each do |file|
|
5
|
+
require file
|
6
|
+
end
|
7
|
+
|
8
|
+
Dir[File.join("#{File.expand_path File.dirname(__FILE__)}", 'profile', '*.rb')].each do |file|
|
9
|
+
require file
|
10
|
+
end
|
11
|
+
|
12
|
+
Dir[File.join("#{File.expand_path File.dirname(__FILE__)}", '*.rb')].each do |file|
|
13
|
+
require file
|
14
|
+
end
|
data/lib/starcraft2/utils.rb
CHANGED
@@ -20,5 +20,25 @@ module Starcraft2
|
|
20
20
|
klass.send(:"#{k}=", v)
|
21
21
|
end
|
22
22
|
end
|
23
|
+
|
24
|
+
def self.get_json
|
25
|
+
response = yield
|
26
|
+
case response.code
|
27
|
+
when 200
|
28
|
+
body = JSON.parse(response.body)
|
29
|
+
case body['code']
|
30
|
+
when 404
|
31
|
+
raise Starcraft2::NotFoundError, body['message'] || body['reason']
|
32
|
+
when 500
|
33
|
+
raise Starcraft2::ApiError, body['message'] || body['reason']
|
34
|
+
else
|
35
|
+
body
|
36
|
+
end
|
37
|
+
when 404
|
38
|
+
raise Starcraft2::NotFoundError, 'Record not found.'
|
39
|
+
when 500
|
40
|
+
raise Starcraft2::ApiError, 'An API error occurred.'
|
41
|
+
end
|
42
|
+
end
|
23
43
|
end
|
24
44
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start do
|
3
|
+
add_filter 'spec'
|
4
|
+
end
|
5
|
+
|
1
6
|
require File.join('bundler', 'setup')
|
2
7
|
require 'rspec'
|
3
8
|
require 'vcr'
|
4
9
|
require 'starcraft2'
|
5
|
-
require 'simplecov'
|
6
|
-
|
7
|
-
SimpleCov.start
|
8
10
|
|
9
11
|
VCR.configure do |c|
|
10
12
|
c.cassette_library_dir = 'fixtures/cassettes'
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Starcraft2::Client do
|
4
4
|
let(:client) { Starcraft2::Client.new(@options) }
|
@@ -33,9 +33,12 @@ describe Starcraft2::Client do
|
|
33
33
|
|
34
34
|
describe '.profile' do
|
35
35
|
it 'should return an sc2 profile' do
|
36
|
-
VCR.use_cassette(
|
36
|
+
VCR.use_cassette('profile_999000') do
|
37
37
|
options = {:character_name => 'DayNine', :id => 999000, :realm => 1}
|
38
|
-
client.profile(options)
|
38
|
+
profile = client.profile(options)
|
39
|
+
profile.class.should == Starcraft2::Profile
|
40
|
+
profile.display_name.should == 'DayNine'
|
41
|
+
profile.id.should == 999000
|
39
42
|
end
|
40
43
|
end
|
41
44
|
|
@@ -53,13 +56,11 @@ describe Starcraft2::Client do
|
|
53
56
|
describe '.achievements' do
|
54
57
|
it 'should return an array of achievements' do
|
55
58
|
VCR.use_cassette('achievements') do
|
56
|
-
client.achievements
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
VCR.use_cassette('achievements') do
|
62
|
-
client.achievements.first.class.should == Starcraft2::Achievement
|
59
|
+
achievements = client.achievements
|
60
|
+
achievements.class.should == Array
|
61
|
+
achievements.each do |a|
|
62
|
+
a.class.should == Starcraft2::Achievement
|
63
|
+
end
|
63
64
|
end
|
64
65
|
end
|
65
66
|
|
@@ -75,13 +76,11 @@ describe Starcraft2::Client do
|
|
75
76
|
describe '.rewards' do
|
76
77
|
it 'should return an array of rewards' do
|
77
78
|
VCR.use_cassette('rewards') do
|
78
|
-
client.rewards
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
VCR.use_cassette('rewards') do
|
84
|
-
client.rewards.first.class.should == Starcraft2::Reward
|
79
|
+
rewards = client.rewards
|
80
|
+
rewards.class.should == Array
|
81
|
+
rewards.each do |r|
|
82
|
+
r.class.should == Starcraft2::Reward
|
83
|
+
end
|
85
84
|
end
|
86
85
|
end
|
87
86
|
|
@@ -102,15 +101,44 @@ describe Starcraft2::Client do
|
|
102
101
|
end
|
103
102
|
let(:member) { ladder.first }
|
104
103
|
|
105
|
-
it 'should return an array of members' do
|
104
|
+
it 'should return an array of members with characters' do
|
106
105
|
ladder.class.should == Array
|
107
106
|
ladder.each do |m|
|
108
107
|
m.class.should == Starcraft2::Member
|
108
|
+
m.character.class.should == Starcraft2::Character
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
112
|
-
it 'should
|
112
|
+
it 'should return the first grandmaster' do
|
113
|
+
member.character.id == 2778732
|
114
|
+
member.character.realm == 1
|
115
|
+
member.character.display_name == 'lIlIlIlIlIlI'
|
116
|
+
member.character.clan_name == ''
|
117
|
+
member.character.clan_tag == ''
|
118
|
+
member.character.profile_path == '/profile/2778732/1/lIlIlIlIlIlI/'
|
119
|
+
|
120
|
+
member.points.should == 2374.0
|
121
|
+
member.wins.should == 253
|
122
|
+
member.losses.should == 102
|
123
|
+
member.join_timestamp.should == 1373859935
|
124
|
+
member.highest_rank.should == 1
|
125
|
+
member.previous_rank.should == 1
|
126
|
+
member.favorite_race_p1.should == 'PROTOSS'
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe '.last_grandmaster_ladder' do
|
131
|
+
let(:ladder) do
|
132
|
+
VCR.use_cassette('last_grandmaster') do
|
133
|
+
client.last_grandmaster_ladder
|
134
|
+
end
|
135
|
+
end
|
136
|
+
let(:member) { ladder.first }
|
137
|
+
|
138
|
+
it 'should return an array of members with characters' do
|
139
|
+
ladder.class.should == Array
|
113
140
|
ladder.each do |m|
|
141
|
+
m.class.should == Starcraft2::Member
|
114
142
|
m.character.class.should == Starcraft2::Character
|
115
143
|
end
|
116
144
|
end
|
@@ -132,4 +160,12 @@ describe Starcraft2::Client do
|
|
132
160
|
member.favorite_race_p1.should == 'PROTOSS'
|
133
161
|
end
|
134
162
|
end
|
135
|
-
|
163
|
+
|
164
|
+
describe '.flush_cache' do
|
165
|
+
it 'should clear out all memoizations' do
|
166
|
+
client.instance_variable_set('@memoizations', {:foo => 'bar'})
|
167
|
+
client.flush_cache
|
168
|
+
client.instance_variable_get('@memoizations').should == {}
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
@@ -8,13 +8,14 @@ describe Starcraft2::Profile do
|
|
8
8
|
let(:profile) { Starcraft2::Profile.build(client, @profile) }
|
9
9
|
|
10
10
|
before do
|
11
|
-
VCR.use_cassette(
|
11
|
+
VCR.use_cassette('profile_999000') do
|
12
12
|
@profile = JSON.parse(HTTParty.get('https://us.battle.net/api/sc2/profile/999000/1/DayNine/').body)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'should build a profile' do
|
17
17
|
profile.class.should == Starcraft2::Profile
|
18
|
+
profile.display_name.should == 'DayNine'
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
@@ -1,12 +1,38 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Starcraft2::Reward do
|
4
|
+
describe '.initialize' do
|
5
|
+
let(:reward) { Starcraft2::Reward.new(@options) }
|
6
|
+
|
7
|
+
before do
|
8
|
+
@options = {
|
9
|
+
'title' => 'Test Title',
|
10
|
+
'id' => 3,
|
11
|
+
'icon' => {'x' => 1, 'y' => 2, 'w' => 3, 'h' => 4, 'offset' => 0, 'url' => 'https://example.com'},
|
12
|
+
'achievement_id' => 5
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should store attributes as underscored' do
|
17
|
+
reward.title.should == 'Test Title'
|
18
|
+
reward.id.should == 3
|
19
|
+
reward.icon.class.should == Starcraft2::Icon
|
20
|
+
reward.icon.x.should == 1
|
21
|
+
reward.icon.y.should == 2
|
22
|
+
reward.icon.w.should == 3
|
23
|
+
reward.icon.h.should == 4
|
24
|
+
reward.icon.offset.should == 0
|
25
|
+
reward.icon.url.should == 'https://example.com'
|
26
|
+
reward.achievement_id.should == 5
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
4
30
|
describe '#build' do
|
5
|
-
let(:rewards) { Starcraft2::Reward.build(@
|
31
|
+
let(:rewards) { Starcraft2::Reward.build(@rewards) }
|
6
32
|
|
7
33
|
before do
|
8
34
|
VCR.use_cassette('rewards') do
|
9
|
-
@
|
35
|
+
@rewards = JSON.parse(HTTParty.get('https://us.battle.net/api/sc2/data/rewards').body)
|
10
36
|
end
|
11
37
|
end
|
12
38
|
|
@@ -19,7 +45,6 @@ describe Starcraft2::Reward do
|
|
19
45
|
|
20
46
|
it 'should import the first reward' do
|
21
47
|
reward = rewards.first
|
22
|
-
|
23
48
|
reward.title.should == 'Kachinsky'
|
24
49
|
reward.id.should == 2951153716
|
25
50
|
reward.icon.x.should == 0
|
@@ -31,30 +56,4 @@ describe Starcraft2::Reward do
|
|
31
56
|
reward.achievement_id.should == 0
|
32
57
|
end
|
33
58
|
end
|
34
|
-
|
35
|
-
describe '.initialize' do
|
36
|
-
let(:reward) { Starcraft2::Reward.new(@options) }
|
37
|
-
|
38
|
-
before do
|
39
|
-
@options = {
|
40
|
-
'title' => 'Test Title',
|
41
|
-
'id' => 3,
|
42
|
-
'icon' => {'x' => 1, 'y' => 2, 'w' => 3, 'h' => 4, 'offset' => 0, 'url' => 'https://example.com'},
|
43
|
-
'achievement_id' => 5
|
44
|
-
}
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'should store attributes as underscored' do
|
48
|
-
reward.title.should == 'Test Title'
|
49
|
-
reward.id.should == 3
|
50
|
-
reward.icon.class.should == Starcraft2::Icon
|
51
|
-
reward.icon.x.should == 1
|
52
|
-
reward.icon.y.should == 2
|
53
|
-
reward.icon.w.should == 3
|
54
|
-
reward.icon.h.should == 4
|
55
|
-
reward.icon.offset.should == 0
|
56
|
-
reward.icon.url.should == 'https://example.com'
|
57
|
-
reward.achievement_id.should == 5
|
58
|
-
end
|
59
|
-
end
|
60
59
|
end
|
@@ -55,4 +55,82 @@ describe Starcraft2::Utils do
|
|
55
55
|
ladders.current_season.last.ladder.first.ladder_name.should == 'Torrasque Upsilon'
|
56
56
|
end
|
57
57
|
end
|
58
|
+
|
59
|
+
describe '#get_json' do
|
60
|
+
let(:success) { {'code' => 200, 'message' => 'success', 'status' => 'nok'} }
|
61
|
+
let(:error) { {'code' => 500, 'message' => 'error', 'status' => 'nok'} }
|
62
|
+
let(:not_found) { {'code' => 404, 'message' => 'not found', 'status' => 'nok'} }
|
63
|
+
|
64
|
+
describe 'HTTP 200' do
|
65
|
+
let(:request) { HTTParty::Request.new Net::HTTP::Get, '/' }
|
66
|
+
let(:response) { Net::HTTPOK.new('1.1', 200, 'OK') }
|
67
|
+
|
68
|
+
it 'should return the parsed json body' do
|
69
|
+
response.stub(:body).and_return(success.to_json)
|
70
|
+
response_lambda = lambda { success.to_json }
|
71
|
+
|
72
|
+
Starcraft2::Utils.get_json do
|
73
|
+
HTTParty::Response.new(request, response, response_lambda)
|
74
|
+
end.should == success
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'json contains code 404' do
|
78
|
+
it 'should throw a NotFoundError' do
|
79
|
+
response.stub(:body).and_return(not_found.to_json)
|
80
|
+
response_lambda = lambda { not_found.to_json }
|
81
|
+
|
82
|
+
expect {
|
83
|
+
Starcraft2::Utils.get_json do
|
84
|
+
HTTParty::Response.new(request, response, response_lambda)
|
85
|
+
end
|
86
|
+
}.to raise_error(Starcraft2::NotFoundError, anything)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'json contains code 500' do
|
91
|
+
it 'should throw an ApiError' do
|
92
|
+
response.stub(:body).and_return(error.to_json)
|
93
|
+
response_lambda = lambda { error.to_json }
|
94
|
+
|
95
|
+
expect {
|
96
|
+
Starcraft2::Utils.get_json do
|
97
|
+
HTTParty::Response.new(request, response, response_lambda)
|
98
|
+
end
|
99
|
+
}.to raise_error(Starcraft2::ApiError, anything)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe 'HTTP 404' do
|
105
|
+
let(:request) { HTTParty::Request.new Net::HTTP::Get, '/' }
|
106
|
+
let(:response) { Net::HTTPNotFound.new('1.1', 404, 'OK') }
|
107
|
+
|
108
|
+
it 'should throw a NotFoundError' do
|
109
|
+
response.stub(:body).and_return(not_found.to_json)
|
110
|
+
response_lambda = lambda { not_found.to_json }
|
111
|
+
|
112
|
+
expect {
|
113
|
+
Starcraft2::Utils.get_json do
|
114
|
+
HTTParty::Response.new(request, response, response_lambda)
|
115
|
+
end
|
116
|
+
}.to raise_error(Starcraft2::NotFoundError, anything)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe 'HTTP 500' do
|
121
|
+
let(:request) { HTTParty::Request.new Net::HTTP::Get, '/' }
|
122
|
+
let(:response) { Net::HTTPInternalServerError.new('1.1', 500, 'OK') }
|
123
|
+
|
124
|
+
it 'should throw an ApiError' do
|
125
|
+
response.stub(:body).and_return(error.to_json)
|
126
|
+
response_lambda = lambda { error.to_json }
|
127
|
+
|
128
|
+
expect {
|
129
|
+
Starcraft2::Utils.get_json do
|
130
|
+
HTTParty::Response.new(request, response, response_lambda)
|
131
|
+
end
|
132
|
+
}.to raise_error(Starcraft2::ApiError, anything)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
58
136
|
end
|