class Leaderboard

Constants

HERO_CLASSES

Attributes

hero[RW]
hero_classes[R]
ladder[RW]
season[RW]

Public Class Methods

new(season = 6, hero = :barb) click to toggle source
# File lib/leaderboard/leaderboard.rb, line 20
def initialize(season = 6, hero = :barb)
  @season = season.to_s
  @hero = HERO_CLASSES[hero]
  @ladder = []
end

Public Instance Methods

print(range = (0..9)) click to toggle source
scrape(timeout = 5) click to toggle source
# File lib/leaderboard/leaderboard.rb, line 30
def scrape(timeout = 5)
  return false if valid_leaderboard?
  return scrape_core timeout
end
valid_leaderboard?() click to toggle source
# File lib/leaderboard/leaderboard.rb, line 26
def valid_leaderboard?
  return true if !HERO_CLASSES.values.include?(@hero) || !@season.to_i.between?(1, 10)
end

Private Instance Methods

scrape_core(timeout = 5) click to toggle source

The core of the scrape, there is a timeout. If any error occurs the method returns false.

# File lib/leaderboard/leaderboard.rb, line 43
def scrape_core(timeout = 5)
  begin
    url = "https://us.battle.net/d3/en/rankings/season/#{@season}/rift-#{@hero}"
    doc = Timeout.timeout(timeout) do
      Nokogiri::HTML(open(url), nil, Encoding::UTF_8.to_s)
    end
    unless doc.nil?
      leaders_unformated = doc.xpath('//*/table/tbody/tr//*/a/@href')
      @ladder.clear
      leaders_unformated.each_with_index do |player, index|
          @ladder << player.value
          @ladder[index].slice!('/d3/en/profile/')
          @ladder[index].slice!('/')
      end
    end
    return true
  rescue
    # Use this as a catch all for any exceptions.
    # In my testing scraping can sometimes take too long.
    return false
  end
end