Skip to content

Commit 5d562a2

Browse files
committed
Adds LeagueScraper capability
1 parent 3f97b45 commit 5d562a2

File tree

5 files changed

+114
-15
lines changed

5 files changed

+114
-15
lines changed

bnet_scraper.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- encoding: utf-8 -*-
22
$:.push File.expand_path("../lib", __FILE__)
3-
require "bnet_scraper/version"
43

54
Gem::Specification.new do |s|
65
s.name = "bnet_scraper"
@@ -17,4 +16,5 @@ Gem::Specification.new do |s|
1716

1817
s.add_runtime_dependency 'mechanize'
1918
s.add_development_dependency 'rspec'
19+
s.add_development_dependency 'fakeweb'
2020
end

lib/bnet_scraper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
require 'bnet_scraper/starcraft2'
2+
require 'mechanize'
3+
require 'net/http'
4+
15
module BnetScraper
26
# Your code goes here...
37
end

lib/bnet_scraper/starcraft2.rb

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,42 @@ def profile_url bnet_index = @bnet_index
5454
"http://#{region_info[:domain]}/sc2/#{region_info[:dir]}/profile/#{bnet_id}/#{bnet_index}/#{account}/"
5555
end
5656

57-
def get_bnet_index
58-
unless @bnet_index
59-
[1,2].each do |idx|
60-
61-
end
62-
end
63-
end
64-
6557
def region_info
6658
REGIONS[region]
6759
end
6860
end
6961

7062
class LeagueScraper
71-
attr_reader :url
63+
attr_reader :url, :bnet_id, :bnet_index, :account, :league_id, :lang,
64+
:season, :size, :random, :name, :division
7265

7366
def initialize(url)
74-
@url = url
67+
@url, @lang, @bnet_id, @bnet_index, @account, @league_id = url.match(/http:\/\/.+\/sc2\/(.+)\/profile\/(.+)\/(\d{1})\/(.+)\/ladder\/(.+)(#current-rank)?/).to_a
68+
@agent = Mechanize.new
69+
end
70+
71+
def scrape
72+
@response = @agent.get(@url)
73+
value = @response.search(".data-title .data-label h3").inner_text().strip
74+
header_regex = /Season (\d{1}) - \s+(\dv\d)( Random)? (\w+)\s+Division (.+)/
75+
header_values = value.match(header_regex).to_a
76+
header_values.shift()
77+
@season, @size, @random, @division, @name = header_values
78+
79+
@random = !@random.nil?
80+
parse_response
81+
end
82+
83+
def parse_response
84+
{
85+
season: @season,
86+
size: @size,
87+
name: @name,
88+
division: @division,
89+
random: @random,
90+
bnet_id: @bnet_id,
91+
account: @account
92+
}
7593
end
7694
end
7795
end
Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,86 @@
11
require 'spec_helper'
22

33
describe BnetScraper::Starcraft2::LeagueScraper do
4+
let(:url) { "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/12345" }
5+
subject { BnetScraper::Starcraft2::LeagueScraper.new(url) }
6+
47
describe '#initialize' do
58
it 'should take a league URL parameter' do
6-
url = "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/12345"
7-
scraper = BnetScraper::Starcraft2::LeagueScraper.new(url)
8-
scraper.url.should == url
9+
subject.url.should == url
10+
end
11+
12+
it 'should dissect the bnet_id from the URL' do
13+
subject.bnet_id.should == '2377239'
14+
end
15+
16+
it 'should dissect the account from the URL' do
17+
subject.account.should == 'Demon'
18+
end
19+
20+
it 'should dissect the league_id from the URL' do
21+
subject.league_id.should == '12345'
22+
end
23+
24+
it 'should dissect the bnet_index from the URL' do
25+
subject.bnet_index.should == '1'
26+
end
27+
28+
it 'should dissect the lang from the URL' do
29+
subject.lang.should == 'en'
30+
end
31+
end
32+
33+
describe '#scrape' do
34+
it 'should set the season value' do
35+
subject.season.should be_nil
36+
subject.scrape
37+
subject.season.should == '6'
38+
end
39+
40+
it 'should set the name' do
41+
subject.name.should be_nil
42+
subject.scrape
43+
subject.name.should == 'Aleksander Pepper'
44+
end
45+
46+
it 'should set the divison' do
47+
subject.division.should be_nil
48+
subject.scrape
49+
subject.division.should == 'Diamond'
50+
end
51+
52+
it 'should set the size' do
53+
subject.size.should be_nil
54+
subject.scrape
55+
subject.size.should == '4v4'
56+
end
57+
58+
it 'should set if player is random' do
59+
subject.random.should be_nil
60+
subject.scrape
61+
subject.random.should be_false
62+
end
63+
64+
it 'should call parse_response' do
65+
subject.should_receive(:parse_response)
66+
subject.scrape
67+
end
68+
end
69+
70+
describe '#parse_response' do
71+
it 'should return a hash of league data' do
72+
expected = {
73+
season: '6',
74+
name: 'Aleksander Pepper',
75+
division: 'Diamond',
76+
size: '4v4',
77+
random: false,
78+
bnet_id: '2377239',
79+
account: 'Demon'
80+
}
81+
82+
subject.scrape
83+
subject.parse_response.should == expected
984
end
1085
end
1186
end

spec/support/load_fakeweb.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'fakeweb'
22

3-
profile_html = File.read File.dirname(__FILE__) + '/profile.html'
3+
profile_html = File.read File.dirname(__FILE__) + '/profile.html'
4+
league_html = File.read File.dirname(__FILE__) + '/league.html'
45

56
FakeWeb.allow_net_connect = false
67
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/', body: profile_html, status: 200, content_type: 'text/html'
8+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/12345', body: league_html, status: 200, content_type: 'text/html'

0 commit comments

Comments
 (0)