Module: Partyhat::Highscores

Defined in:
lib/partyhat/highscores.rb

Constant Summary collapse

HIGHSCORES_PAGE_URL =
'http://hiscore.runescape.com/index_lite.ws?player=%s'

Class Method Summary collapse

Class Method Details

.find_player(name) ⇒ Object

Fetch a user’s highscores object



6
7
8
9
10
# File 'lib/partyhat/highscores.rb', line 6

def self.find_player name
  page_url = HIGHSCORES_PAGE_URL % URI::encode(name)
  page_src = Partyhat::Util.fetch_remote(page_url)
  self.parse_lite_page(page_src) unless page_src.nil?
end

.parse_lite_page(page) ⇒ Object

Parse the result page from the highscores into A hash containing skill/activity objects with the corresponding stat name as key.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/partyhat/highscores.rb', line 15

def self.parse_lite_page page
  lite_page_lines = page.split("\n")
  user_stats = {}
  Partyhat::Stat::List.each do |stat|
    line_parts = lite_page_lines.shift.split(',').map(&:to_i)
    if line_parts.size == 3
      rank, level, experience = line_parts.shift, line_parts.shift, line_parts.shift
      user_stats[stat]        = Partyhat::Skill.new(stat, level, experience, rank)
    else
      rank, score      = line_parts.shift, line_parts.shift
      user_stats[stat] = Partyhat::Activity.new(stat, score, rank)
    end
  end
  user_stats
end