Class: BnetScraper::Starcraft2::MatchHistoryScraper
- Inherits:
-
BaseScraper
- Object
- BaseScraper
- BnetScraper::Starcraft2::MatchHistoryScraper
- 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
-
#matches ⇒ Object
readonly
Returns the value of attribute matches.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Attributes inherited from BaseScraper
#account, #bnet_id, #bnet_index, #region, #url
Instance Method Summary collapse
- #extract_match_info(m) ⇒ Object
-
#get_response ⇒ Object
retrieves the match history HTML for scraping.
- #losses ⇒ Object
-
#match_url ⇒ Object
account’s match history URL.
- #scrape ⇒ Object
- #wins ⇒ Object
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
#matches ⇒ Object (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 |
#response ⇒ Object (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
#extract_match_info(m) ⇒ Object
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 |
#get_response ⇒ Object
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 |
#losses ⇒ Object
54 55 56 |
# File 'lib/bnet_scraper/starcraft2/match_history_scraper.rb', line 54 def losses @losses ||= @matches.count { |m| m.outcome == :loss } end |
#match_url ⇒ Object
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 |
#scrape ⇒ Object
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 |
#wins ⇒ Object
50 51 52 |
# File 'lib/bnet_scraper/starcraft2/match_history_scraper.rb', line 50 def wins @wins ||= @matches.count { |m| m.outcome == :win } end |