Class: BnetScraper::Starcraft2::GrandmasterScraper

Inherits:
BaseScraper
  • Object
show all
Defined in:
lib/bnet_scraper/starcraft2/grandmaster_scraper.rb

Constant Summary collapse

REGIONS =
[:us, :eu, :kr, :tw, :sea]

Instance Attribute Summary collapse

Attributes inherited from BaseScraper

#account, #bnet_id, #bnet_index, #region, #url

Instance Method Summary collapse

Methods inherited from BaseScraper

#extract_data_from_options, #extract_data_from_url, #profile_url, #region_info, #set_bnet_index, #valid?

Constructor Details

#initialize(options = {}) ⇒ GrandmasterScraper

Returns a new instance of GrandmasterScraper.



7
8
9
10
11
12
13
14
# File 'lib/bnet_scraper/starcraft2/grandmaster_scraper.rb', line 7

def initialize options = {}
  @players = []
  if REGIONS.include? options[:region]
    @url = "http://#{options[:region]}.battle.net/sc2/en/ladder/grandmaster/heart-of-the-swarm"
  else
    raise "Invalid Region #{options[:region]}"
  end
end

Instance Attribute Details

#playersObject (readonly)

Returns the value of attribute players.



4
5
6
# File 'lib/bnet_scraper/starcraft2/grandmaster_scraper.rb', line 4

def players
  @players
end

Instance Method Details

#get_grandmasters(html) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bnet_scraper/starcraft2/grandmaster_scraper.rb', line 32

def get_grandmasters html
  html.css("#ladder tbody tr").each do |player_data|
    player_cells = player_data.css("td[class='align-center']")
    player = {
      name: player_data.css("td a").inner_text.strip,
      points: player_cells[2].inner_text,
      wins: player_cells[3].inner_text,
      losses: player_cells[4].inner_text,
      race: player_data.css("td a").attr('class').value.sub('race-', ''),
      rank: player_cells[1].inner_text.strip
    }

    @players << player
  end
end

#retrieve_dataObject



24
25
26
27
28
29
30
# File 'lib/bnet_scraper/starcraft2/grandmaster_scraper.rb', line 24

def retrieve_data
  response = Faraday.get @url

  if response.success?
    Nokogiri::HTML(response.body)
  end
end

#scrapeObject



16
17
18
19
20
21
22
# File 'lib/bnet_scraper/starcraft2/grandmaster_scraper.rb', line 16

def scrape
  html = retrieve_data    

  get_grandmasters html

  @players
end