Class: BnetScraper::Starcraft2::MatchHistoryScraper

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

Overview

This pulls the 25 most recent matches played for an account. Note that this is only as up-to-date as battle.net is, and will likely not be as fast as in-game.

scraper = BnetScraper::Starcraft2::MatchHistoryScraper.new(url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/')
scraper.scrape
# => {
  wins: '15',
  losses: '10',
  matches: [
    { map_name: 'Bx Monobattle - Sand Canyon (Fix)', outcome: :win, type: 'Custom', date: '3/12/2012' },
    { map_name: 'Deadlock Ridge', outcome: :loss, type: '4v4', date: '3/12/2012' },
    { map_name: 'District 10', outcome: :win, type: '4v4', date: '3/12/2012' },
    # ...
  ]
}

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, #initialize, #profile_url, #region_info, #set_bnet_index, #valid?

Constructor Details

This class inherits a constructor from BnetScraper::Starcraft2::BaseScraper

Instance Attribute Details

- (Object) matches (readonly)

Returns the value of attribute matches



21
22
23
# File 'lib/bnet_scraper/starcraft2/match_history_scraper.rb', line 21

def matches
  @matches
end

- (Object) response (readonly)

Returns the value of attribute response



21
22
23
# File 'lib/bnet_scraper/starcraft2/match_history_scraper.rb', line 21

def response
  @response
end

Instance Method Details

- (Object) extract_match_info(m)



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bnet_scraper/starcraft2/match_history_scraper.rb', line 58

def extract_match_info m
  match = Match.new

  cells = m.css('td')
  match.map_name = cells[1].inner_text
  match.type = cells[2].inner_text
  match.outcome = (cells.css('.match-win')[0] ? :win : :loss)
  match.date = cells[4].inner_text.strip

  match
end

- (Object) get_response

retrieves the match history HTML for scraping



29
30
31
32
33
34
35
36
37
# File 'lib/bnet_scraper/starcraft2/match_history_scraper.rb', line 29

def get_response
  @response = Faraday.get match_url

  if @response.success?
    @response = Nokogiri::HTML(@response.body)
  else
    raise BnetScraper::InvalidProfileError
  end
end

- (Object) losses



54
55
56
# File 'lib/bnet_scraper/starcraft2/match_history_scraper.rb', line 54

def losses
  @losses ||= @matches.count { |m| m.outcome == :loss }
end

- (Object) match_url

account's match history URL



24
25
26
# File 'lib/bnet_scraper/starcraft2/match_history_scraper.rb', line 24

def match_url
  profile_url + "matches"
end

- (Object) scrape



39
40
41
42
43
44
45
46
47
48
# File 'lib/bnet_scraper/starcraft2/match_history_scraper.rb', line 39

def scrape
  get_response
  @matches = []

  response.css('.match-row').each do |m|
    @matches.push extract_match_info m
  end

  @matches
end

- (Object) wins



50
51
52
# File 'lib/bnet_scraper/starcraft2/match_history_scraper.rb', line 50

def wins
  @wins ||= @matches.count { |m| m.outcome == :win }
end