Class: BnetScraper::Starcraft2::BaseScraper

Inherits:
Object
  • Object
show all
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)

Instance Method Summary (collapse)

Constructor Details

- (BaseScraper) initialize(options = {})



22
23
24
25
26
27
28
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 22

def initialize options = {}
  if options[:url]
    extract_data_from_url options[:url]
  elsif options[:bnet_id] && options[:account]
    extract_data_from_options options
  end
end

Instance Attribute Details

- (Object) account (readonly)

Returns the value of attribute account



20
21
22
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 20

def 
  @account
end

- (Object) bnet_id (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

- (Object) bnet_index (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

- (Object) region (readonly)

Returns the value of attribute region



20
21
22
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 20

def region
  @region
end

- (Object) url (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

- (Object) extract_data_from_options(options)

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 extract_data_from_options options
  @bnet_id  = options[:bnet_id]
  @account  = options[:account]
  @region   = options[:region] || 'na'

  if options[:bnet_index]
    @bnet_index = options[:bnet_index]
  else
    set_bnet_index
  end
end

- (Object) extract_data_from_url(url)

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

- (Object) profile_url(bnet_index = @bnet_index)



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}/#{}/"
end

- (Object) region_info

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

- (Object) scrape

Raises:

  • (NotImplementedError)


91
92
93
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 91

def scrape
  raise NotImplementedError, "Abstract method #scrape called."
end

- (Object) set_bnet_index

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

- (Boolean) valid?



86
87
88
89
# File 'lib/bnet_scraper/starcraft2/base_scraper.rb', line 86

def valid?
  result = Faraday.get profile_url
  result.success?
end