Class: RsHighscores::Player
- Inherits:
-
Object
- Object
- RsHighscores::Player
- Defined in:
- lib/rshighscores/player.rb
Direct Known Subclasses
Constant Summary collapse
- LookupUrl =
"http://hiscore.runescape.com/index_lite.ws?player="
- StatClass =
RsHighscores::Stats
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#raw_stats ⇒ Object
readonly
Returns the value of attribute raw_stats.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
- #fetch_highscores ⇒ Object
-
#initialize(name, params = {}) ⇒ Player
constructor
A new instance of Player.
- #validate_name ⇒ Object
Constructor Details
#initialize(name, params = {}) ⇒ Player
Returns a new instance of Player.
10 11 12 13 14 15 |
# File 'lib/rshighscores/player.rb', line 10 def initialize name, params = {} @name = name validate_name fetch_highscores if params[:force] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/rshighscores/player.rb', line 8 def name @name end |
#raw_stats ⇒ Object (readonly)
Returns the value of attribute raw_stats.
8 9 10 |
# File 'lib/rshighscores/player.rb', line 8 def raw_stats @raw_stats end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
8 9 10 |
# File 'lib/rshighscores/player.rb', line 8 def stats @stats end |
Instance Method Details
#fetch_highscores ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/rshighscores/player.rb', line 26 def fetch_highscores safe_name = @name.gsub " ", "%20" f = open(self.class::LookupUrl + safe_name, "User-Agent" => "Ruby/RsHighscoresGrabber") @raw_stats = f.readlines.map &:chomp # readlines preserves newlines?? @stats = self.class::StatClass.new @raw_stats end |
#validate_name ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/rshighscores/player.rb', line 17 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 |