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.
- data/lib/battlenet_info.rb +108 -103
- data/lib/exceptions.rb +2 -0
- metadata +3 -2
data/lib/battlenet_info.rb
CHANGED
@@ -1,109 +1,114 @@
|
|
1
1
|
require 'net/http'
|
2
|
+
require 'exceptions.rb'
|
2
3
|
|
3
4
|
class BattleNetInfo
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
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
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.
|
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-
|
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:
|