Class: BnetScraper::Starcraft2::BaseScraper
- Inherits:
-
Object
- Object
- BnetScraper::Starcraft2::BaseScraper
- Defined in:
- lib/bnet_scraper/starcraft2/base_scraper.rb
Overview
BaseScraper handles the account information extraction. Each scraper can either be passed a profile URL or the minimum information needed to access an account. This means passing in account and bnet_id at minimum. Both of the following are valid ways to instantiate a scraper for the same account:
BnetScraper::Starcraft2::BaseScraper.new(url: 'http://us.battle.net/sc2/en/profile/12345/1/TestAccount/')
BnetScraper::Starcraft2::BaseScraper.new(bnet_id: '12345', account: 'TestAccount')
The URL scheme is the following:
http://<REGION_DOMAIN>/sc2/<REGION_LANG>/profile/<BNET_ID>/<BNET_INDEX>/<ACCOUNT>/
Note that by default, the region will be set to ‘na’ if you opt not to specify the URL or region. The scraper uses the short-codes for regions. See ‘BnetScraper::Starcraft2::REGIONS` for the address translations.
Direct Known Subclasses
AchievementScraper, GrandmasterScraper, LeagueScraper, MatchHistoryScraper, ProfileScraper
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Returns the value of attribute account.
-
#bnet_id ⇒ Object
readonly
Returns the value of attribute bnet_id.
-
#bnet_index ⇒ Object
readonly
Returns the value of attribute bnet_index.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#extract_data_from_options(options) ⇒ Object
Extracts information about the account from an options hash.
-
#extract_data_from_url(url) ⇒ Object
Extracts information about the account from the URL string.
-
#initialize(options = {}) ⇒ BaseScraper
constructor
A new instance of BaseScraper.
- #profile_url(bnet_index = @bnet_index) ⇒ Object
-
#region_info ⇒ Object
converts region short-code to region-based URL information ‘na’ => { domain: ‘us.battle.net’, :dir: ‘en’ }.
- #scrape ⇒ Object
-
#set_bnet_index ⇒ Object
set_bnet_index.
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ BaseScraper
Returns a new instance of BaseScraper.
22 23 24 25 26 27 28 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 22 def initialize = {} if [:url] extract_data_from_url [:url] elsif [:bnet_id] && [:account] end end |
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
20 21 22 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 20 def account @account end |
#bnet_id ⇒ Object (readonly)
Returns the value of attribute bnet_id.
20 21 22 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 20 def bnet_id @bnet_id end |
#bnet_index ⇒ Object (readonly)
Returns the value of attribute bnet_index.
20 21 22 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 20 def bnet_index @bnet_index end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
20 21 22 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 20 def region @region end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
20 21 22 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 20 def url @url end |
Instance Method Details
#extract_data_from_options(options) ⇒ Object
Extracts information about the account from an options hash
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 50 def @bnet_id = [:bnet_id] @account = [:account] @region = [:region] || 'na' if [:bnet_index] @bnet_index = [:bnet_index] else set_bnet_index end end |
#extract_data_from_url(url) ⇒ Object
Extracts information about the account from the URL string
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 33 def extract_data_from_url url extracted_data = url.match(/http:\/\/(.+)\/sc2\/(.+)\/profile\/(.+)\/(\d{1})\/(.[^\/]+)\//) if extracted_data @region = REGION_DOMAINS[extracted_data[1]] @language = extracted_data[2] @bnet_id = extracted_data[3] @bnet_index = extracted_data[4] @account = extracted_data[5] @url = url else raise BnetScraper::InvalidProfileError, "URL provided does not match Battle.net format" end end |
#profile_url(bnet_index = @bnet_index) ⇒ Object
76 77 78 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 76 def profile_url bnet_index = @bnet_index "http://#{region_info[:domain]}/sc2/#{region_info[:dir]}/profile/#{bnet_id}/#{bnet_index}/#{account}/" end |
#region_info ⇒ Object
converts region short-code to region-based URL information
'na' => { domain: 'us.battle.net', :dir: 'en' }
82 83 84 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 82 def region_info REGIONS[region] end |
#scrape ⇒ Object
91 92 93 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 91 def scrape raise NotImplementedError, "Abstract method #scrape called." end |
#set_bnet_index ⇒ Object
set_bnet_index
Because profile URLs have to have a specific bnet_index that is seemingly incalculable, we must ping both variants to determine the correct bnet_index. We then store that value.
66 67 68 69 70 71 72 73 74 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 66 def set_bnet_index [1,2].each do |idx| res = Net::HTTP.get_response URI profile_url idx if res.is_a? Net::HTTPSuccess @bnet_index = idx return end end end |
#valid? ⇒ Boolean
86 87 88 89 |
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 86 def valid? result = Faraday.get profile_url result.success? end |