Class: Nanowrimo::User

Inherits:
Core
  • Object
show all
Defined in:
lib/nanowrimo/user.rb

Overview

Handles Nanowrimo User data.

Constant Summary collapse

FIELDS =

fields expected from the main User WCAPI

%w[uid uname user_wordcount]
HISTORY_FIELDS =

history fields expected from the User History WCAPI

%w[wc wcdate]
USER_FIELDS =

fields needed to store data ripped from a user’s profile page

%w[rid novel genre buddies]
PROFILE_URI =

profile page base URI

"http://www.nanowrimo.org/eng/user"

Instance Attribute Summary collapse

Attributes inherited from Core

#error

Instance Method Summary collapse

Methods inherited from Core

#has_error?, #load, #load_history

Constructor Details

#initialize(uid) ⇒ User

creates a new User object



19
20
21
22
23
24
# File 'lib/nanowrimo/user.rb', line 19

def initialize uid
  @uid = uid
  @novel = {}
  @genre = {}
  @buddies = []
end

Instance Attribute Details

#historyObject

Returns the value of attribute history.



17
18
19
# File 'lib/nanowrimo/user.rb', line 17

def history
  @history
end

#profile_dataObject

Returns the value of attribute profile_data.



17
18
19
# File 'lib/nanowrimo/user.rb', line 17

def profile_data
  @profile_data
end

Instance Method Details

#idObject

converts the WCAPI ‘uid’ into a Nanowrimo::Core-friendly ‘id’



27
28
29
# File 'lib/nanowrimo/user.rb', line 27

def id
  @uid
end

#load_fieldObject

converts the WCAPI path for this type into something Nanowrimo::Core-friendly



32
33
34
# File 'lib/nanowrimo/user.rb', line 32

def load_field
  'wc'
end

#load_history_fieldObject

converts the WCAPI history path for this type into something Nanowrimo::Core-friendly



37
38
39
# File 'lib/nanowrimo/user.rb', line 37

def load_history_field
  'wchistory/wordcounts/wcentry'
end

#load_profile_dataObject

Method to pull down a WWW::Mechanize::Page instance of the User’s profile page



47
48
49
# File 'lib/nanowrimo/user.rb', line 47

def load_profile_data
  @profile_data = Nokogiri::HTML(open("#{PROFILE_URI}/#{@uid}"))
end

#parse_profileObject

Parses the profile page data pulling out extra information for the User.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nanowrimo/user.rb', line 52

def parse_profile
  load_profile_data
  # get the buddies
  @buddies = @profile_data.search("div[@class='buddies']//a").map{|b| b['href'].split('/').last; }
  @buddies.reject!{|b| b.to_i == 0}
  # title and genre are in the same element
  titlegenre = @profile_data.search("div[@class='titlegenre']").text.split("Genre:")
  unless titlegenre.empty?
    @genre[:name] = titlegenre.last.strip
    @novel[:title] = titlegenre.first.gsub('Novel:','').strip
  else
    @genre[:name] = ""
    @novel[:title] = ""
  end
  # finally, the region is annoying to grab
  @rid = @profile_data.search("div[@class='infoleft']//a").first['href'].split('/').last
  nil
end

#winner?Boolean

Determines if the User’s current wordcount meets the month’s goal.

Returns:

  • (Boolean)


42
43
44
# File 'lib/nanowrimo/user.rb', line 42

def winner?
  self.user_wordcount.to_i >= Nanowrimo::GOAL
end