battlenet_info 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/battlenet_info.rb +86 -0
  2. metadata +45 -0
@@ -0,0 +1,86 @@
1
+ require 'net/http'
2
+
3
+ class BattleNetInfo
4
+
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
+ def download_data
17
+ download_profile_content
18
+ download_ladder_content
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
+ match_data = /<h3>(?<achievement_points>\d+)<\/h3>/i.match self.profile_content
39
+
40
+ match_data[:achievement_points].to_i
41
+ end
42
+
43
+ def race
44
+ match_data = /class=\Wrace-(?<race>\w+)\W/i.match self.profile_content
45
+
46
+ match_data[:race]
47
+ end
48
+
49
+ def stats
50
+ 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
51
+
52
+ return [0, 0] if match_data.nil?
53
+ [match_data[:points].to_i, match_data[:wins].to_i]
54
+ end
55
+
56
+ def rank
57
+ 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
58
+
59
+ return 0 if match_data.nil?
60
+ match_data[:rank].to_i
61
+ end
62
+
63
+ def league
64
+ expr = /badge-(?<league>\w+)\sbadge-medium-(?<top>\d+)\W>\s+<\/span>\s+<\/a>\s+<div\sid=\Wbest-team-1/im
65
+ match_data = expr.match self.profile_content
66
+
67
+ "#{match_data[:league]}_#{match_data[:top]}"
68
+ end
69
+
70
+ def portrait_html_style(new_path)
71
+ 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
72
+
73
+ match_data[:picture_style].sub('/sc2/static/local-common/images/sc2/portraits/', new_path)
74
+ end
75
+
76
+ private
77
+
78
+ def download_profile_content
79
+ @profile_content = Net::HTTP.get_response(URI.parse(@profile_url)).body if self.valid_url?
80
+ end
81
+
82
+ def download_ladder_content
83
+ @ladder_content = Net::HTTP.get_response(URI.parse(@ladder_url)).body if self.valid_url?
84
+ end
85
+
86
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: battlenet_info
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vitaly Tatarintsev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-25 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: A simple Battle.Net info parser gem
15
+ email: Kalastiuz@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/battlenet_info.rb
21
+ homepage: ''
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.6
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Battle.Net info
45
+ test_files: []