battlenet_info 0.1.2 → 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 (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/battlenet_info.rb +20 -20
  3. data/lib/exceptions.rb +2 -2
  4. metadata +22 -10
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 27e137e29486a3824c7c4f1653b7ab3fe9ef2388
4
+ data.tar.gz: cb40755126edfebafbd9d6fe594023ad10c364eb
5
+ SHA512:
6
+ metadata.gz: 0cab450782e73e00d334d9585c7caaddefc317537172f6af38bc6912070335011b3fd10c86d45ab9235404cbfd9e5ff553ebaec73bef3dadc5b2ac40acca46eb
7
+ data.tar.gz: a626d94de1a67b5c75bddbefd530f6c7fac8f9deb37a0abe94d5a203c86a3c96e0cb4cabff119c1ee51f54272aeafa65728d5fc6515bffcb9dfdf185e2371a2f
@@ -1,10 +1,13 @@
1
1
  require 'net/http'
2
+ require "nokogiri"
2
3
  require 'exceptions.rb'
3
4
 
4
5
  class BattleNetInfo
5
6
 
6
7
  EXCEPTION_MESSAGE = 'Profile not found or Battle.Net temporary unavailable'
7
8
 
9
+ attr_reader :profile_url, :ladder_url
10
+
8
11
  def initialize(url)
9
12
  @profile_url = url
10
13
  splitter = '/' unless @profile_url[-1, 1] == '/'
@@ -19,40 +22,36 @@ class BattleNetInfo
19
22
  @ladder_content ||= download_ladder_content
20
23
  end
21
24
 
22
- # <b>DEPRECATED:</b> Now calls from each method if needed
23
- def download_data
24
- warn "[DEPRECATION] `download_data` is deprecated. Now calls from each method if needed."
25
- end
26
-
27
25
  def valid_url?
28
26
  (@profile_url =~ /http:\/\/\w+\.battle.net\/sc2\/\w+\/profile\/\d+\/\d\/\w+/i) != nil
29
27
  end
30
28
 
31
29
  def server
32
30
  match_data = /http:\/\/(?<server>\w+)/i.match @profile_url
33
-
31
+
34
32
  match_data[:server]
35
33
  end
36
34
 
37
35
  def player_name
38
36
  match_data = /profile\/\d+\/\d\/(?<name>\w+)/i.match @profile_url
39
-
37
+
40
38
  match_data[:name]
41
39
  end
42
40
 
43
41
  def achievement_points
44
- download_profile_content if self.profile_content.empty?
45
42
  match_data = /<h3>(?<achievement_points>\d+)<\/h3>/i.match self.profile_content
46
-
43
+
47
44
  match_data[:achievement_points].to_i
48
45
  end
49
46
 
50
47
  def race
48
+ raise RaceTemporaryUnvailable
51
49
  match_data = /class=\Wrace-(?<race>\w+)\W/i.match self.profile_content
52
-
50
+
53
51
  match_data[:race]
54
52
  end
55
53
 
54
+ # TODO: calculate loses again
56
55
  def stats
57
56
  match_data = /<div\s+class=\Wtooltip-title\W>#{self.player_name}<\/div>[\n\t\s]+<strong>[\w\s]+\W<\/strong>\s*\d+<br\s*\/>[\t\n\s]+<strong>[\w\s]+\W\s*<\/strong>\s*\w+\s*<\/div>[\t\n\s]+<\/td>[\t\n\s]+<td\sclass=\Walign-center\W>(?<points>\d+)<\/td>[\t\n\s]+<td\sclass=\Walign-center\W>(?<wins>\d+)<\/td>/mi.match self.ladder_content
58
57
 
@@ -68,8 +67,9 @@ class BattleNetInfo
68
67
  end
69
68
 
70
69
  def league
71
- expr = /badge-(?<league>\w+)\sbadge-medium-(?<top>\d+)\W>\s+<\/span>\s+<\/a>\s+<div\sid=\Wbest-team-1/im
72
- match_data = expr.match self.profile_content
70
+ html_doc = Nokogiri::HTML(profile_content)
71
+ badge_data = html_doc.at_css("#season-snapshot").children.css(".badge-item").first.at_css("span.badge")[:class]
72
+ match_data = /badge-(?<league>\w+)\sbadge-medium-(?<top>\d+)/.match badge_data
73
73
 
74
74
  "#{match_data[:league]}_#{match_data[:top]}"
75
75
  end
@@ -77,7 +77,7 @@ class BattleNetInfo
77
77
  def portrait_html_style(new_path)
78
78
  match_data = /<span\s+class=\Wicon-frame\s+\W[\s\t\n]+(?<picture_style>\w+\=[\w\s\W]+portraits[\w\s\W]+90px;)\W>[\t\n\s]+<\/span>/im.match self.profile_content
79
79
 
80
- match_data[:picture_style].sub('/sc2/static/local-common/images/sc2/portraits/', new_path)
80
+ match_data[:picture_style].sub('http://media.blizzard.com/sc2/portraits/', new_path)
81
81
  end
82
82
 
83
83
  def to_hash
@@ -93,22 +93,22 @@ class BattleNetInfo
93
93
  :rank => self.rank,
94
94
  :league => self.league
95
95
  }
96
-
97
- player_data
98
96
  end
99
97
 
100
98
  private
101
99
 
102
100
  def download_profile_content
103
- response = Net::HTTP.get_response(URI.parse(@profile_url)) if self.valid_url?
104
- raise ProfileNotFound.new(EXCEPTION_MESSAGE) unless response.is_a? Net::HTTPOK
105
- @profile_content = response.body
101
+ @profile_content = get_content(@profile_url)
106
102
  end
107
103
 
108
104
  def download_ladder_content
109
- response = Net::HTTP.get_response(URI.parse(@ladder_url)) if self.valid_url?
105
+ @ladder_content = get_content(@ladder_url)
106
+ end
107
+
108
+ def get_content(url)
109
+ response = Net::HTTP.get_response(URI.parse(url)) if self.valid_url?
110
110
  raise ProfileNotFound.new(EXCEPTION_MESSAGE) unless response.is_a? Net::HTTPOK
111
- @ladder_content = response.body
111
+ response.body
112
112
  end
113
113
 
114
114
  end
data/lib/exceptions.rb CHANGED
@@ -1,2 +1,2 @@
1
- class ProfileNotFound < Exception
2
- end
1
+ class ProfileNotFound < StandardError; end
2
+ class RaceTemporaryUnvailable < StandardError; end
metadata CHANGED
@@ -1,16 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: battlenet_info
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Vitaly Tatarintsev
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2011-09-28 00:00:00.000000000Z
13
- dependencies: []
11
+ date: 2013-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.6
14
27
  description: A simple Battle.Net info parser gem
15
28
  email: Kalastiuz@gmail.com
16
29
  executables: []
@@ -21,26 +34,25 @@ files:
21
34
  - lib/exceptions.rb
22
35
  homepage: https://github.com/ck3g/battlenet_info
23
36
  licenses: []
37
+ metadata: {}
24
38
  post_install_message:
25
39
  rdoc_options: []
26
40
  require_paths:
27
41
  - lib
28
42
  required_ruby_version: !ruby/object:Gem::Requirement
29
- none: false
30
43
  requirements:
31
- - - ! '>='
44
+ - - '>='
32
45
  - !ruby/object:Gem::Version
33
46
  version: '0'
34
47
  required_rubygems_version: !ruby/object:Gem::Requirement
35
- none: false
36
48
  requirements:
37
- - - ! '>='
49
+ - - '>='
38
50
  - !ruby/object:Gem::Version
39
51
  version: '0'
40
52
  requirements: []
41
53
  rubyforge_project:
42
- rubygems_version: 1.8.6
54
+ rubygems_version: 2.0.0
43
55
  signing_key:
44
- specification_version: 3
56
+ specification_version: 4
45
57
  summary: Battle.Net info
46
58
  test_files: []