Skip to content

Commit 353c550

Browse files
committed
ProfileScraper now takes a hash of options
ProfileScraper.new takes a hash for parsing. Among them, :url is now accepted. If :url is passed, it will dissect the URL into the relevant information. Additionally, :bnet_index can now be passed, which means saving 1-2 HTTP calls to determine the index.
1 parent 50651c0 commit 353c550

File tree

3 files changed

+70
-24
lines changed

3 files changed

+70
-24
lines changed

lib/bnet_scraper/starcraft2.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module Starcraft2
2828
# @return profile_data - Hash containing complete profile and league data
2929
# scraped from the website
3030
def self.full_profile_scrape bnet_id, account, region = 'na'
31-
profile_scraper = ProfileScraper.new bnet_id, account, region
31+
profile_scraper = ProfileScraper.new bnet_id: bnet_id, account: account, region: region
3232
profile_output = profile_scraper.scrape
3333

3434
parsed_leagues = []

lib/bnet_scraper/starcraft2/profile_scraper.rb

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,37 @@ module Starcraft2
1010
# One thing of note is the bnet_index. In Battle.net URLs, there is a single-digit index
1111
# used on accounts (1 or 2). This index is seemingly arbitrary, but critical to properly
1212
# accessing the data.
13+
#
14+
# ProfileScraper requires that either you pass the URL of the profile, or the bnet_id and
15+
# account name of the profile. The URL scheme is as such:
16+
#
17+
# http://<REGION_DOMAIN>/sc2/<REGION_LANG>/profile/<BNET_ID>/<BNET_INDEX>/<ACCOUNT>/
18+
#
19+
# Using this URL we can extract the critical information. However, sometimes we do not have
20+
# the URL and have to make do with a bnet_id and account. This is the bare minimum needed,
21+
# unless the account is in a region other than 'na'. In such cases, region all needs to be passed.
1322
class ProfileScraper
1423
attr_reader :bnet_id, :account, :region, :agent, :bnet_index
1524

16-
# @param bnet_id - The unique ID for each battle.net account. in the URL scheme, it immediately
17-
# follows the /profile/ directive.
18-
# @param account - The account name. This is a non-unique string that immediately follows the
19-
# bnet_index
20-
# @oaram region - The region of the account. This defaults to 'na' for North America. This
21-
# uses the bnet region codes instead of the full names. The region is used to determine the
22-
# domain to find the profile, as well as teh default language code to use.
25+
# @param options - Hash of options to parse.
2326
# @return profile_data - The hash of profile data scraped, including array of leagues to scrape
24-
def initialize bnet_id, account, region = 'na'
25-
@bnet_id = bnet_id
26-
@account = account
27-
@region = region
28-
set_bnet_index
27+
def initialize options = {}
28+
if options[:url]
29+
extracted_data = options[:url].match(/http:\/\/(.+)\/sc2\/(.+)\/profile\/(.+)\/(\d{1})\/(.+)\//)
30+
@region = REGIONS.key({ domain: extracted_data[1], dir: extracted_data[2] })
31+
@bnet_id = extracted_data[3]
32+
@bnet_index = extracted_data[4]
33+
@account = extracted_data[5]
34+
elsif options[:bnet_id] && options[:account]
35+
@bnet_id = options[:bnet_id]
36+
@account = options[:account]
37+
@region = options[:region] || 'na'
38+
if options[:bnet_index]
39+
@bnet_index = options[:bnet_index]
40+
else
41+
set_bnet_index
42+
end
43+
end
2944
end
3045

3146
# set_bnet_index

spec/starcraft2/profile_scraper_spec.rb

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,53 @@
11
require 'spec_helper'
22

33
describe BnetScraper::Starcraft2::ProfileScraper do
4-
subject { BnetScraper::Starcraft2::ProfileScraper.new('2377239', 'Demon') }
4+
subject { BnetScraper::Starcraft2::ProfileScraper.new(bnet_id: '2377239', account: 'Demon') }
55

66
describe '#initialize' do
7-
it 'should take bnet_id and account parameters' do
8-
subject.bnet_id.should == '2377239'
9-
subject.account.should == 'Demon'
7+
context 'when bnet_id and account parameters are passed' do
8+
it 'should set the bnet_id and account parameters' do
9+
subject.bnet_id.should == '2377239'
10+
subject.account.should == 'Demon'
11+
end
12+
13+
it 'should default the region to na' do
14+
subject.region.should == 'na'
15+
end
16+
17+
it 'should assign region if passed' do
18+
BnetScraper::Starcraft2::ProfileScraper.any_instance.should_receive(:set_bnet_index)
19+
scraper = BnetScraper::Starcraft2::ProfileScraper.new(bnet_id: '2377239', account: 'Demon', region: 'fea')
20+
scraper.region.should == 'fea'
21+
end
22+
23+
it 'should not call set_bnet_index if bnet_index is passed' do
24+
BnetScraper::Starcraft2::ProfileScraper.any_instance.should_not_receive(:set_bnet_index)
25+
scraper = BnetScraper::Starcraft2::ProfileScraper.new(bnet_id: '2377239', account: 'Demon', region: 'fea', bnet_index: '1')
26+
end
27+
28+
it 'should call set_bnet_index_if bnet_index is not passed' do
29+
BnetScraper::Starcraft2::ProfileScraper.any_instance.should_receive(:set_bnet_index)
30+
scraper = BnetScraper::Starcraft2::ProfileScraper.new(bnet_id: '2377239', account: 'Demon', region: 'fea')
31+
end
1032
end
1133

12-
it 'should default the region to na' do
13-
subject.region.should == 'na'
14-
end
34+
context 'when url is passed' do
35+
subject { BnetScraper::Starcraft2::ProfileScraper.new(url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/') }
36+
it 'should extract the bnet_id from the url' do
37+
subject.bnet_id.should == '2377239'
38+
end
39+
40+
it 'should extract the account from the url' do
41+
subject.account.should == 'Demon'
42+
end
43+
44+
it 'should extract the region from the url' do
45+
subject.region.should == 'na'
46+
end
1547

16-
it 'should assign region if passed' do
17-
BnetScraper::Starcraft2::ProfileScraper.any_instance.should_receive(:set_bnet_index)
18-
scraper = BnetScraper::Starcraft2::ProfileScraper.new('2377239', 'Demon', 'fea')
19-
scraper.region.should == 'fea'
48+
it 'should extract the bnet_index from the url' do
49+
subject.bnet_index.should == '1'
50+
end
2051
end
2152
end
2253

0 commit comments

Comments
 (0)