class BnetScraper::Starcraft2::AchievementScraper

This pulls achievement information for an account. Note that currently only returns the overall achievements, not the in-depth, by-category achievement information.

“‘ ruby scraper = BnetScraper::Starcraft2::AchievementScraper.new(

url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/'

) achievement_information = scraper.scrape achievement_information.size # => 6 achievement_information.first # => #<BnetScraper::Starcraft2::Achievement:0x007fef52b0b488 @description=“Win 50 Team Unranked or Ranked games as Zerg.”,

@earned=#<Date: 2013-04-04 ((2456387j,0s,0n),+0s,2299161j)>,
@title="50 Wins: Team Zerg">

achievement_information # => {:liberty_campaign=>1580, :swarm_campaign=>1120,

:matchmaking=>1410,
:custom_game=>120,
:arcade=>220,
:exploration=>530}

achievement_information.size # => 5 achievement_information.first # => #<BnetScraper::Starcraft2::Achievement:0x007fef52abcb08 @description=“Finish a Qualification Round with an undefeated record.”,

@title="Hot Shot">

“‘

Attributes

progress[R]
recent[R]
response[R]
showcase[R]

Public Instance Methods

convert_date(date) click to toggle source
# File lib/bnet_scraper/starcraft2/achievement_scraper.rb, line 85
def convert_date date
  dates = date.scan(/(\d+)\/(\d+)\/(\d+)/).first.map(&:to_i)
  
  if region == 'na'
    month, day, year = dates
  else
    day, month, year = dates
  end
    
  Date.new year, month, day
end
extract_recent_achievement(num) click to toggle source

Scrapes recent achievement by position in the sidebar

@param [Fixnum] achievement position number, top-down @return [Achievement] achievement object containing all achievement information

# File lib/bnet_scraper/starcraft2/achievement_scraper.rb, line 75
def extract_recent_achievement num
  if div = response.css("#achv-recent-#{num}")
    Achievement.new({
      title: div.children[1].inner_text,
      description: div.children[2].inner_text.strip,
      earned: convert_date(response.css(".recent-tile")[num].css('span')[1].inner_text)
    })
  end
end
get_response() click to toggle source

retrieves the account’s achievements overview page HTML for scraping

@return [Nokogiri::HTML] The parsed HTML document

# File lib/bnet_scraper/starcraft2/achievement_scraper.rb, line 49
def get_response
  response = Faraday.get "#{profile_url}achievements/"
  if response.success?
    @response = Nokogiri::HTML(response.body) 
  else
    raise BnetScraper::InvalidProfileError
  end
end
output() click to toggle source
# File lib/bnet_scraper/starcraft2/achievement_scraper.rb, line 130
def output
  {
    recent: @recent,
    progress: @progress,
    showcase: @showcase
  }
end
scrape() click to toggle source
# File lib/bnet_scraper/starcraft2/achievement_scraper.rb, line 38
def scrape
  get_response
  scrape_recent
  scrape_progress
  scrape_showcase
  output
end
scrape_progress() click to toggle source

Scrapes the progress of each achievement category from the account’s achievements overview page and returns them as a hash

@return [Hash] Hash of achievement indicators broken down by category

# File lib/bnet_scraper/starcraft2/achievement_scraper.rb, line 103
def scrape_progress
  keys = [:liberty_campaign, :swarm_campaign, :matchmaking, :custom_game, :arcade, :exploration]
  index = 1

  @progress = keys.inject({}) do |hash, key|
    hash[key] = response.css(".progress-tile:nth-child(#{index}) .profile-progress span").inner_text.to_i
    index += 1

    hash
  end
end
scrape_recent() click to toggle source

scrapes the recent achievements from the account’s achievements overview page

@return [Array<Achievement>] Array of recent achievements

# File lib/bnet_scraper/starcraft2/achievement_scraper.rb, line 61
def scrape_recent
  @recent = []
  response.css(".recent-tile").size.times do |num|
    achievement = extract_recent_achievement num
    @recent.push(achievement) if achievement
  end

  @recent
end
scrape_showcase() click to toggle source

Scrapes the showcase achievements from the account’s achievements overview page

@return [Array<Achievement>] Array containing all the showcased achievements

# File lib/bnet_scraper/starcraft2/achievement_scraper.rb, line 118
def scrape_showcase
  @showcase = response.css("#showcase-module .progress-tile").map do |achievement|
    if !achievement.values.first.split.include? 'empty'
      Achievement.new({
        title: achievement.css('.tooltip-title').inner_text.strip,
        description: achievement.children[3].children[2].inner_text.strip
      })
    end
  end
  @showcase
end