Class: SC2Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/sc2stats.rb

Instance Method Summary collapse

Constructor Details

#initialize(profileurl) ⇒ SC2Stats

Returns a new instance of SC2Stats.



69
70
71
72
73
# File 'lib/sc2stats.rb', line 69

def initialize(profileurl)
	@url = profileurl

	self.scrape
end

Instance Method Details

#==(o) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/sc2stats.rb', line 88

def ==(o)
	if o.is_a? SC2Stats
		@url == o.url
	else
		false
	end
end

#leagueObject



80
# File 'lib/sc2stats.rb', line 80

def league; @league; end

#nameObject



82
# File 'lib/sc2stats.rb', line 82

def name; @name; end

#pointsObject



81
# File 'lib/sc2stats.rb', line 81

def points; @points; end

#scrapeObject



29
30
31
32
33
34
35
36
37
# File 'lib/sc2stats.rb', line 29

def scrape
	html = Nokogiri::HTML(open("#{@url}/ladder/leagues","User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0) Gecko/20100101 Firefox/4.0"))

	@league = scrape_league(html)
	@points = scrape_points(html)
	@name = scrape_name(html)

	true
end

#scrape_league(html) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sc2stats.rb', line 39

def scrape_league(html)
	league = nil

	html.css("#profile-menu li a").each do |l|
		if l.text.include? "1v1"
			league = l
			break
		end
	end

	leaguename = league.text.strip.gsub(/\dv\d /, "").gsub(/ Rank.+/, "").downcase unless league.nil?
	leaguename ||= SC2League::LEAGUES.last

	SC2League.new(leaguename)
end

#scrape_name(html) ⇒ Object



65
66
67
# File 'lib/sc2stats.rb', line 65

def scrape_name(html)
	html.css("#profile-header h2 a").first.text
end

#scrape_points(html) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/sc2stats.rb', line 55

def scrape_points(html)
	td = html.css("#current-rank td")[3]

	if td.nil?
		0
	else
		Integer(td.text)
	end
end

#to_sObject



84
85
86
# File 'lib/sc2stats.rb', line 84

def to_s
	"#{@name} (#{@league}, #{@points} pts)"
end

#urlObject



75
# File 'lib/sc2stats.rb', line 75

def url; @url; end

#url=(value) ⇒ Object



76
77
78
# File 'lib/sc2stats.rb', line 76

def url=(value)
	@url = value
end