Class: Osrs::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/osrshighscores/player.rb

Constant Summary collapse

@@lookup_url =
"http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player="

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Player

Returns a new instance of Player.



9
10
11
12
# File 'lib/osrshighscores/player.rb', line 9

def initialize name
  @name = name
  validate_name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/osrshighscores/player.rb', line 7

def name
  @name
end

#raw_statsObject (readonly)

Returns the value of attribute raw_stats.



7
8
9
# File 'lib/osrshighscores/player.rb', line 7

def raw_stats
  @raw_stats
end

#statsObject (readonly)

Returns the value of attribute stats.



7
8
9
# File 'lib/osrshighscores/player.rb', line 7

def stats
  @stats
end

Instance Method Details

#fetch_highscoresObject



23
24
25
26
27
28
# File 'lib/osrshighscores/player.rb', line 23

def fetch_highscores
  f = open(@@lookup_url + @name, "User-Agent" => "Ruby/OSRSGrabber")
  
  @raw_stats = f.readlines.map &:chomp # Readlines preserves newlines??
  @stats = Osrs::Stats.new @raw_stats
end

#validate_nameObject



14
15
16
17
18
19
20
21
# File 'lib/osrshighscores/player.rb', line 14

def validate_name
  raise "invalid characters in name" if @name =~ /[^A-Za-z0-9_\- ]/

  raise "name too long" if @name.length > 12
  raise "name too short" if @name.length < 1

  raise "name starts/ends with a space character" if (name[0] + name[-1]) =~ /[_\- ]/
end