Skip to content

Commit 5e2a085

Browse files
committed
Throw exception if URL is formatted incorrectly
1 parent 741c83b commit 5e2a085

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/bnet_scraper/starcraft2/base_scraper.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ class BaseScraper
2020
def initialize options = {}
2121
if options[:url]
2222
extracted_data = options[:url].match(/http:\/\/(.+)\/sc2\/(.+)\/profile\/(.+)\/(\d{1})\/(.[^\/]+)\//)
23-
@region = REGION_DOMAINS[extracted_data[1]]
24-
@bnet_id = extracted_data[3]
25-
@bnet_index = extracted_data[4]
26-
@account = extracted_data[5]
27-
@url = options[:url]
23+
if extracted_data
24+
@region = REGION_DOMAINS[extracted_data[1]]
25+
@bnet_id = extracted_data[3]
26+
@bnet_index = extracted_data[4]
27+
@account = extracted_data[5]
28+
@url = options[:url]
29+
else
30+
raise BnetScraper::InvalidProfileError, "URL provided does not match Battle.net format"
31+
end
2832
elsif options[:bnet_id] && options[:account]
2933
@bnet_id = options[:bnet_id]
3034
@account = options[:account]

spec/starcraft2/base_scraper_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
let(:subject) { scraper_class.new(url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/') }
77
end
88

9+
it 'raises InvalidProfileError on poorly formatted URL' do
10+
expect {
11+
BnetScraper::Starcraft2::BaseScraper.new url: 'mangled_url'
12+
}.to raise_error BnetScraper::InvalidProfileError
13+
end
14+
915
describe '#scrape' do
1016
it 'should raise an error calling scrape' do
1117
expect { subject.scrape }.to raise_error NotImplementedError

0 commit comments

Comments
 (0)