Skip to content

Commit 53d567e

Browse files
committed
Adds MatchHistoryScraper#scrape
1 parent e69ee89 commit 53d567e

File tree

5 files changed

+1322
-3
lines changed

5 files changed

+1322
-3
lines changed

lib/bnet_scraper/starcraft2/achievement_scraper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def scrape
88
scrape_recent
99
scrape_progress
1010
scrape_showcase
11+
output
1112
end
1213

1314
def get_response
Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,52 @@
11
module BnetScraper
22
module Starcraft2
3-
class MatchHistoryScraper
3+
# MatchHistoryScraper
4+
#
5+
# Scrapes the 25 most recent matches played by an account and returns it
6+
# as an array of match hashes.
7+
class MatchHistoryScraper < BaseScraper
8+
attr_reader :matches, :wins, :losses, :response
9+
10+
def match_url
11+
profile_url + "matches"
12+
end
413

14+
def get_response
15+
@response = Nokogiri::HTML(open(match_url))
16+
end
17+
18+
def scrape
19+
get_response
20+
@matches = []
21+
@wins = 0
22+
@losses = 0
23+
24+
response.css('.match-row').each do |m|
25+
match = {}
26+
27+
cells = m.css('td')
28+
match[:map_name] = cells[1].inner_text
29+
match[:type] = cells[2].inner_text
30+
match[:outcome] = (cells[3].inner_text.strip == 'Win' ? :win : :loss)
31+
match[:date] = cells[4].inner_text.strip
32+
@matches << match
33+
if match[:outcome] == :win
34+
@wins += 1
35+
else
36+
@losses += 1
37+
end
38+
output
39+
end
40+
41+
end
42+
43+
def output
44+
{
45+
matches: @matches,
46+
wins: @wins,
47+
losses: @losses
48+
}
49+
end
550
end
651
end
752
end

spec/starcraft2/match_history_scraper_spec.rb

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,50 @@
44
let(:url) { 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/matches' }
55
subject { BnetScraper::Starcraft2::MatchHistoryScraper.new(url: url) }
66

7-
it 'should be true' do
8-
true
7+
it_behaves_like 'an SC2 Scraper' do
8+
let(:scraper_class) { BnetScraper::Starcraft2::MatchHistoryScraper }
9+
let(:scraper) { scraper_class.new(url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/matches') }
10+
end
11+
12+
describe '#scrape' do
13+
14+
before :each do
15+
subject.scrape
16+
end
17+
18+
it 'sets the matches to an array of match data' do
19+
subject.matches.should have(25).matches
20+
end
21+
22+
it 'should set the name of the map for each match' do
23+
subject.matches[0][:map_name].should == 'Bx Monobattle - Sand Canyon (Fix)'
24+
end
25+
26+
it 'should set the type of the match for each match' do
27+
subject.matches[0][:type].should == 'Custom'
28+
end
29+
30+
it 'should set the outcome of each match' do
31+
subject.matches[0][:outcome].should == :win
32+
end
33+
34+
it 'should set the match date of each match' do
35+
subject.matches[0][:date].should == '3/12/2012'
36+
end
37+
end
38+
39+
describe '#output' do
40+
before :each do
41+
subject.scrape
42+
end
43+
44+
it 'should return a hash of the scraped match data' do
45+
expected = {
46+
matches: subject.matches,
47+
wins: subject.wins,
48+
losses: subject.losses
49+
}
50+
subject.output.should == expected
51+
end
952
end
1053
end

spec/support/load_fakeweb.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
leagues_html = File.read File.dirname(__FILE__) + '/leagues.html'
55
league_html = File.read File.dirname(__FILE__) + '/league.html'
66
achievements_html = File.read File.dirname(__FILE__) + '/achievements.html'
7+
matches_html = File.read File.dirname(__FILE__) + '/matches.html'
78

89
FakeWeb.allow_net_connect = false
910
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/', body: profile_html, status: 200, content_type: 'text/html'
@@ -22,3 +23,4 @@
2223
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98336', body: league_html, status: 200, content_type: 'text/html'
2324
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98936', body: league_html, status: 200, content_type: 'text/html'
2425
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/achievements/', body: achievements_html, status: 200, content_type: 'text/html'
26+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/matches', body: matches_html, status: 200, content_type: 'text/html'

0 commit comments

Comments
 (0)