starcraft2 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 08741dd794e9bd5b5ac4e64c4de0c8fd4263e31f
4
- data.tar.gz: 5954f75fcb88aa8a941ab80a8915632b4c7655c0
3
+ metadata.gz: ae4ebd851c581a60a04334ee02445b8f53d0f8a6
4
+ data.tar.gz: 86579b56eedd10f2ba72309c6b52d13dc9aa0a9d
5
5
  SHA512:
6
- metadata.gz: aee87b02d3d1234b818007df6f15161ab3af7cf5542258adf9896f3c5cf34051c284ae58071ed5e74fec3f5cf1a4c458b52aff19bbbbf58586d9a51d87a46b91
7
- data.tar.gz: 65b2b8b08b4539b5c6a7cdac0b92dab59c280129e84913536ed27a7471840f71a1b6234e539c02d3a696c0fff5a2d5571b4521f15fa95ca620ff445d4732f5e1
6
+ metadata.gz: eed2a2067825ae0df56f0029512cff259839b9e941520d6ceeede9af6c412d036c0203f3a656bf296a539d39e61a8e521f353ec078a2e939163d72ef4548d30c
7
+ data.tar.gz: 9ddac4a3333d6bd492555122c064b33b3afe1c50d7f1baa3ec9bd56f10fc16541c113bf064317cf1da7098cca087ff0142b97fdba4994cefc669bee5b48cad05
@@ -6,8 +6,7 @@ module Starcraft2
6
6
  Utils.load(self, options, {:icon => Icon})
7
7
  end
8
8
 
9
- def self.build(achievements_json)
10
- data = JSON.parse(achievements_json)
9
+ def self.build(data)
11
10
  data['achievements'].map do |achievement|
12
11
  new(achievement)
13
12
  end
@@ -42,14 +42,16 @@ module Starcraft2
42
42
  ladder('grandmaster')
43
43
  end
44
44
 
45
- def previous_grandmaster_ladder
45
+ def last_grandmaster_ladder
46
46
  ladder('grandmaster/last')
47
47
  end
48
48
 
49
49
  private
50
50
 
51
51
  def profile_json(options)
52
- HTTParty.get(profile_url(options)).body
52
+ get_body do
53
+ HTTParty.get(profile_url(options))
54
+ end
53
55
  end
54
56
 
55
57
  def profile_url(options)
@@ -61,7 +63,9 @@ module Starcraft2
61
63
  end
62
64
 
63
65
  def match_json(options)
64
- HTTParty.get(match_url(options)).body
66
+ get_body do
67
+ HTTParty.get(match_url(options))
68
+ end
65
69
  end
66
70
 
67
71
  def match_url(options)
@@ -69,7 +73,9 @@ module Starcraft2
69
73
  end
70
74
 
71
75
  def achievements_json
72
- HTTParty.get(achievements_url).body
76
+ get_body do
77
+ HTTParty.get(achievements_url)
78
+ end
73
79
  end
74
80
 
75
81
  def achievements_url
@@ -77,7 +83,9 @@ module Starcraft2
77
83
  end
78
84
 
79
85
  def rewards_json
80
- HTTParty.get(rewards_url).body
86
+ get_body do
87
+ HTTParty.get(rewards_url)
88
+ end
81
89
  end
82
90
 
83
91
  def rewards_url
@@ -85,7 +93,9 @@ module Starcraft2
85
93
  end
86
94
 
87
95
  def ladder_json(id)
88
- HTTParty.get(ladder_url(id)).body
96
+ get_body do
97
+ HTTParty.get(ladder_url(id))
98
+ end
89
99
  end
90
100
 
91
101
  def ladder_url(id)
@@ -95,5 +105,27 @@ module Starcraft2
95
105
  def locale_param
96
106
  locale.nil? ? '' : "?locale=#{locale}"
97
107
  end
108
+
109
+ def get_body
110
+ response = yield
111
+ case response.code
112
+ when 200
113
+ body = JSON.parse(response.body)
114
+ case body['code']
115
+ when 200
116
+ body
117
+ when 404
118
+ raise Starcraft2::NotFoundError, body['message'] || body['reason']
119
+ when 500
120
+ raise Starcraft2::ApiError, body['message'] || body['reason']
121
+ else
122
+ body
123
+ end
124
+ when 404
125
+ raise Starcraft2::NotFoundError, 'Record not found.'
126
+ when 500
127
+ raise Starcraft2::ApiError, 'An API error occurred.'
128
+ end
129
+ end
98
130
  end
99
131
  end
@@ -0,0 +1,4 @@
1
+ module Starcraft2
2
+ class ApiError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Starcraft2
2
+ class NotFoundError < StandardError
3
+ end
4
+ end
@@ -1,7 +1,6 @@
1
1
  module Starcraft2
2
2
  class Ladder
3
- def self.build(ladder_json)
4
- data = JSON.parse(ladder_json)
3
+ def self.build(data)
5
4
  data['ladderMembers'].map do |member|
6
5
  Member.new(member)
7
6
  end
@@ -3,6 +3,8 @@ require 'httparty'
3
3
  require 'json'
4
4
 
5
5
  require File.join('starcraft2', 'errors', 'missing_arguments_error')
6
+ require File.join('starcraft2', 'errors', 'api_error')
7
+ require File.join('starcraft2', 'errors', 'not_found_error')
6
8
  require File.join('starcraft2', 'utils')
7
9
  require File.join('starcraft2', 'icon')
8
10
  require File.join('starcraft2', 'client')
@@ -7,8 +7,7 @@ module Starcraft2
7
7
  Utils.load(self, options)
8
8
  end
9
9
 
10
- def self.build(match_json)
11
- data = JSON.parse(match_json)
10
+ def self.build(data)
12
11
  data['matches'].map do |m|
13
12
  new(m)
14
13
  end
@@ -15,9 +15,8 @@ module Starcraft2
15
15
  })
16
16
  end
17
17
 
18
- def self.build(client, profile_json)
19
- profile = JSON.parse(profile_json)
20
- new(profile.merge!(:client => client))
18
+ def self.build(client, data)
19
+ new(data.merge!(:client => client))
21
20
  end
22
21
 
23
22
  def matches
@@ -6,8 +6,7 @@ module Starcraft2
6
6
  Utils.load(self, options, {:icon => Icon})
7
7
  end
8
8
 
9
- def self.build(rewards_json)
10
- data = JSON.parse(rewards_json)
9
+ def self.build(data)
11
10
  data['portraits'].map do |reward|
12
11
  new(reward)
13
12
  end
@@ -2,11 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe Starcraft2::Achievement do
4
4
  describe '#build' do
5
- let(:achievements) { Starcraft2::Achievement.build(@achievements_json) }
5
+ let(:achievements) { Starcraft2::Achievement.build(@achievements) }
6
6
 
7
7
  before do
8
8
  VCR.use_cassette('achievements') do
9
- @achievements_json = HTTParty.get('https://us.battle.net/api/sc2/data/achievements').body
9
+ @achievements = JSON.parse(HTTParty.get('https://us.battle.net/api/sc2/data/achievements').body)
10
10
  end
11
11
  end
12
12
 
@@ -27,7 +27,7 @@ describe Starcraft2::Achievement do
27
27
 
28
28
  it 'should import the first achievement' do
29
29
  VCR.use_cassette('achievements') do
30
- @achievement = Starcraft2::Achievement.build(HTTParty.get('https://us.battle.net/api/sc2/data/achievements').body).first
30
+ @achievement = Starcraft2::Achievement.build(JSON.parse(HTTParty.get('https://us.battle.net/api/sc2/data/achievements').body)).first
31
31
  end
32
32
 
33
33
  @achievement.title.should == 'FFA Destroyer'
@@ -52,10 +52,6 @@ describe Starcraft2::Client do
52
52
  end
53
53
  end
54
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
-
59
55
  describe '.achievements' do
60
56
  it 'should return an array of achievements' do
61
57
  VCR.use_cassette('achievements') do
@@ -8,7 +8,7 @@ describe Starcraft2::Ladder do
8
8
 
9
9
  before do
10
10
  VCR.use_cassette("ladder_#{id}") do
11
- @raw_ladder_data = HTTParty.get("https://us.battle.net/api/sc2/ladder/#{id}").body
11
+ @raw_ladder_data = JSON.parse(HTTParty.get("https://us.battle.net/api/sc2/ladder/#{id}").body)
12
12
  end
13
13
  end
14
14
 
@@ -5,11 +5,11 @@ describe Starcraft2::Profile do
5
5
  let(:profile) { Starcraft2::Profile.new(@options) }
6
6
 
7
7
  describe '#build' do
8
- let(:profile) { Starcraft2::Profile.build(client, @profile_json) }
8
+ let(:profile) { Starcraft2::Profile.build(client, @profile) }
9
9
 
10
10
  before do
11
11
  VCR.use_cassette("profile_999000") do
12
- @profile_json = HTTParty.get('https://us.battle.net/api/sc2/profile/999000/1/DayNine/').body
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
 
@@ -6,7 +6,7 @@ describe Starcraft2::Reward do
6
6
 
7
7
  before do
8
8
  VCR.use_cassette('rewards') do
9
- @raw_reward_data = HTTParty.get('https://us.battle.net/api/sc2/data/rewards').body
9
+ @raw_reward_data = JSON.parse(HTTParty.get('https://us.battle.net/api/sc2/data/rewards').body)
10
10
  end
11
11
  end
12
12
 
@@ -27,7 +27,7 @@ describe Starcraft2::Reward do
27
27
 
28
28
  it 'should import the first reward' do
29
29
  VCR.use_cassette('rewards') do
30
- @reward = Starcraft2::Reward.build(HTTParty.get('https://us.battle.net/api/sc2/data/rewards').body).first
30
+ @reward = Starcraft2::Reward.build(JSON.parse(HTTParty.get('https://us.battle.net/api/sc2/data/rewards').body)).first
31
31
  end
32
32
 
33
33
  @reward.title.should == 'Kachinsky'
data/starcraft2.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |gem|
14
14
  gem.test_files = `git ls-files -- {spec}/*`.split("\n")
15
15
  gem.name = "starcraft2"
16
16
  gem.require_paths = ["lib"]
17
- gem.version = "0.1.0"
17
+ gem.version = "0.1.1"
18
18
 
19
19
  gem.add_development_dependency 'rspec', '~> 2.6.0'
20
20
  gem.add_development_dependency 'rake'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: starcraft2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Ellithorpe
@@ -136,7 +136,9 @@ files:
136
136
  - lib/starcraft2/achievement.rb
137
137
  - lib/starcraft2/character.rb
138
138
  - lib/starcraft2/client.rb
139
+ - lib/starcraft2/errors/api_error.rb
139
140
  - lib/starcraft2/errors/missing_arguments_error.rb
141
+ - lib/starcraft2/errors/not_found_error.rb
140
142
  - lib/starcraft2/icon.rb
141
143
  - lib/starcraft2/ladder.rb
142
144
  - lib/starcraft2/loader.rb