Class: Nanowrimo::User
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
-
#history ⇒ Object
Returns the value of attribute history.
-
#profile_data ⇒ Object
Returns the value of attribute profile_data.
Attributes inherited from Core
Instance Method Summary collapse
-
#id ⇒ Object
converts the WCAPI ‘uid’ into a Nanowrimo::Core-friendly ‘id’.
-
#initialize(uid) ⇒ User
constructor
creates a new User object.
-
#load_field ⇒ Object
converts the WCAPI path for this type into something Nanowrimo::Core-friendly.
-
#load_history_field ⇒ Object
converts the WCAPI history path for this type into something Nanowrimo::Core-friendly.
-
#load_profile_data ⇒ Object
Method to pull down a WWW::Mechanize::Page instance of the User’s profile page.
-
#parse_profile ⇒ Object
Parses the profile page data pulling out extra information for the User.
-
#winner? ⇒ Boolean
Determines if the User’s current wordcount meets the month’s goal.
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
#history ⇒ Object
Returns the value of attribute history.
17 18 19 |
# File 'lib/nanowrimo/user.rb', line 17 def history @history end |
#profile_data ⇒ Object
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
#id ⇒ Object
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_field ⇒ Object
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_field ⇒ Object
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_data ⇒ Object
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_profile ⇒ Object
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 |