battlenet_info 0.1.1 → 0.1.2

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 (3) hide show
  1. data/lib/battlenet_info.rb +108 -103
  2. data/lib/exceptions.rb +2 -0
  3. metadata +3 -2
@@ -1,109 +1,114 @@
1
1
  require 'net/http'
2
+ require 'exceptions.rb'
2
3
 
3
4
  class BattleNetInfo
4
5
 
5
- attr_reader :profile_content
6
- attr_reader :ladder_content
7
-
8
- def initialize(url)
9
- @profile_content = ''
10
- @ladder_content = ''
11
- @profile_url = url
12
- splitter = '/' unless @profile_url[-1, 1] == '/'
13
- @ladder_url = "#{@profile_url}#{splitter}ladder/leagues"
14
- end
15
-
16
- # <b>DEPRECATED:</b> Now calls from each method if needed
17
- def download_data
18
- warn "[DEPRECATION] `download_data` is deprecated. Now calls from each method if needed."
19
- end
20
-
21
- def valid_url?
22
- (@profile_url =~ /http:\/\/\w+\.battle.net\/sc2\/\w+\/profile\/\d+\/\d\/\w+/i) != nil
23
- end
24
-
25
- def server
26
- match_data = /http:\/\/(?<server>\w+)/i.match @profile_url
27
-
28
- match_data[:server]
29
- end
30
-
31
- def player_name
32
- match_data = /profile\/\d+\/\d\/(?<name>\w+)/i.match @profile_url
33
-
34
- match_data[:name]
35
- end
36
-
37
- def achievement_points
38
- download_profile_content if self.profile_content.empty?
39
- match_data = /<h3>(?<achievement_points>\d+)<\/h3>/i.match self.profile_content
40
-
41
- match_data[:achievement_points].to_i
42
- end
43
-
44
- def race
45
- download_profile_content if self.profile_content.empty?
46
- match_data = /class=\Wrace-(?<race>\w+)\W/i.match self.profile_content
47
-
48
- match_data[:race]
49
- end
50
-
51
- def stats
52
- download_ladder_content if self.ladder_content.empty?
53
- 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
54
-
55
- return [0, 0] if match_data.nil?
56
- [match_data[:points].to_i, match_data[:wins].to_i]
57
- end
58
-
59
- def rank
60
- download_ladder_content if self.ladder_content.empty?
61
- match_data = /<td\s+class=\Walign-center\W\sstyle=\Wwidth:\s40px\W>(?<rank>\d+)\W*\w+<\/td>[\n\t\s]+<td>[\n\t\s]+<a\shref=\W\/sc2\/\w+\/profile\/\d+\/\d\/#{self.player_name}/mi.match self.ladder_content
62
-
63
- return 0 if match_data.nil?
64
- match_data[:rank].to_i
65
- end
66
-
67
- def league
68
- download_profile_content if self.profile_content.empty?
69
- expr = /badge-(?<league>\w+)\sbadge-medium-(?<top>\d+)\W>\s+<\/span>\s+<\/a>\s+<div\sid=\Wbest-team-1/im
70
- match_data = expr.match self.profile_content
71
-
72
- "#{match_data[:league]}_#{match_data[:top]}"
73
- end
74
-
75
- def portrait_html_style(new_path)
76
- download_profile_content if self.profile_content.empty?
77
- 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
78
-
79
- match_data[:picture_style].sub('/sc2/static/local-common/images/sc2/portraits/', new_path)
80
- end
81
-
82
- def to_hash
83
- points, wins = self.stats
84
-
85
- player_data = {
86
- :server => self.server,
87
- :player_name => self.player_name,
88
- :achievement_points => self.achievement_points,
89
- :race => self.race,
90
- :points => points,
91
- :wins => wins,
92
- :rank => self.rank,
93
- :league => self.league
94
- }
95
-
96
- player_data
97
- end
98
-
99
- private
100
-
101
- def download_profile_content
102
- @profile_content = Net::HTTP.get_response(URI.parse(@profile_url)).body if self.valid_url?
103
- end
104
-
105
- def download_ladder_content
106
- @ladder_content = Net::HTTP.get_response(URI.parse(@ladder_url)).body if self.valid_url?
107
- end
6
+ EXCEPTION_MESSAGE = 'Profile not found or Battle.Net temporary unavailable'
7
+
8
+ def initialize(url)
9
+ @profile_url = url
10
+ splitter = '/' unless @profile_url[-1, 1] == '/'
11
+ @ladder_url = "#{@profile_url}#{splitter}ladder/leagues"
12
+ end
13
+
14
+ def profile_content
15
+ @profile_content ||= download_profile_content
16
+ end
17
+
18
+ def ladder_content
19
+ @ladder_content ||= download_ladder_content
20
+ end
21
+
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
+ def valid_url?
28
+ (@profile_url =~ /http:\/\/\w+\.battle.net\/sc2\/\w+\/profile\/\d+\/\d\/\w+/i) != nil
29
+ end
30
+
31
+ def server
32
+ match_data = /http:\/\/(?<server>\w+)/i.match @profile_url
33
+
34
+ match_data[:server]
35
+ end
36
+
37
+ def player_name
38
+ match_data = /profile\/\d+\/\d\/(?<name>\w+)/i.match @profile_url
39
+
40
+ match_data[:name]
41
+ end
42
+
43
+ def achievement_points
44
+ download_profile_content if self.profile_content.empty?
45
+ match_data = /<h3>(?<achievement_points>\d+)<\/h3>/i.match self.profile_content
46
+
47
+ match_data[:achievement_points].to_i
48
+ end
49
+
50
+ def race
51
+ match_data = /class=\Wrace-(?<race>\w+)\W/i.match self.profile_content
52
+
53
+ match_data[:race]
54
+ end
55
+
56
+ def stats
57
+ 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
+
59
+ return [0, 0] if match_data.nil?
60
+ [match_data[:points].to_i, match_data[:wins].to_i]
61
+ end
62
+
63
+ def rank
64
+ match_data = /<td\s+class=\Walign-center\W\sstyle=\Wwidth:\s40px\W>(?<rank>\d+)\W*\w+<\/td>[\n\t\s]+<td>[\n\t\s]+<a\shref=\W\/sc2\/\w+\/profile\/\d+\/\d\/#{self.player_name}/mi.match self.ladder_content
65
+
66
+ return 0 if match_data.nil?
67
+ match_data[:rank].to_i
68
+ end
69
+
70
+ 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
73
+
74
+ "#{match_data[:league]}_#{match_data[:top]}"
75
+ end
76
+
77
+ def portrait_html_style(new_path)
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
+
80
+ match_data[:picture_style].sub('/sc2/static/local-common/images/sc2/portraits/', new_path)
81
+ end
82
+
83
+ def to_hash
84
+ points, wins = self.stats
85
+
86
+ player_data = {
87
+ :server => self.server,
88
+ :player_name => self.player_name,
89
+ :achievement_points => self.achievement_points,
90
+ :race => self.race,
91
+ :points => points,
92
+ :wins => wins,
93
+ :rank => self.rank,
94
+ :league => self.league
95
+ }
96
+
97
+ player_data
98
+ end
99
+
100
+ private
101
+
102
+ 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
106
+ end
107
+
108
+ def download_ladder_content
109
+ response = Net::HTTP.get_response(URI.parse(@ladder_url)) if self.valid_url?
110
+ raise ProfileNotFound.new(EXCEPTION_MESSAGE) unless response.is_a? Net::HTTPOK
111
+ @ladder_content = response.body
112
+ end
108
113
 
109
114
  end
data/lib/exceptions.rb ADDED
@@ -0,0 +1,2 @@
1
+ class ProfileNotFound < Exception
2
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: battlenet_info
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-25 00:00:00.000000000Z
12
+ date: 2011-09-28 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A simple Battle.Net info parser gem
15
15
  email: Kalastiuz@gmail.com
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/battlenet_info.rb
21
+ - lib/exceptions.rb
21
22
  homepage: https://github.com/ck3g/battlenet_info
22
23
  licenses: []
23
24
  post_install_message: