lib/espn_fantasy_news/parser.rb in espn-fantasy-news-0.1.2 vs lib/espn_fantasy_news/parser.rb in espn-fantasy-news-0.2.0
- old
+ new
@@ -2,10 +2,11 @@
module ESPNFantasyNews
class Parser
+ # Load the entire universe of ESPN players
def self.load_all_players
offset = 0
res = []
while offset <= 1080 do
url = ESPNFantasyNews::PLAYER_LIST_ENDPOINT + "?startIndex=#{offset.to_s}"
@@ -13,10 +14,11 @@
offset += 40
end
res
end
+ # Load all the players at the url
def self.players_from_url(url)
doc = Nokogiri::HTML(open(url))
player_attributes = doc.css('.pncPlayerRow').collect do |x|
cell = x.css('td')[1]
cell_text = cell.text
@@ -32,14 +34,16 @@
ESPNFantasyNews::Player.new(name, player_id, pos, team)
end
player_attributes
end
+ # Return all the latest news stories
def self.load_all_news
self.news_from_url(ESPNFantasyNews::NEWS_ENDPOINT)
end
+ # Return all the news stories from the given url
def self.news_from_url(url)
doc = Nokogiri::HTML(open(url))
news_attributes = doc.css('tr.tableBody').collect do |player_row|
player_id = player_row.css('a')[0].get_attribute('playerid').to_i
text = player_row.css('.pni-shorttext')[0].content
@@ -47,10 +51,25 @@
ESPNFantasyNews::News.new(player_id, text)
end
news_attributes
end
+ # Return an array of espn player ids for the players on the team at URL
+ def self.load_team_player_ids(url)
+ ids = []
+ doc = Nokogiri::HTML(open(url))
+ player_row = doc.css('.pncPlayerRow').each do |row|
+ link = row.css('a')[0]
+ if link
+ player_id = link.get_attribute('playerid').to_i
+ ids << player_id
+ end
+ end
+ name = self.parse_team_name(doc.css('title').text)
+ Team.new(name, ids)
+ end
+
private
# It isn't a space that seperates the team and position, it's ascii char 194 for some reason?
def self.split_string_on_char_194(str)
return str if (str.nil? || str.length < 4)
@@ -75,9 +94,18 @@
"T" => "TE",
"K" => "K",
"D" => "D/ST"
}
end
-
+
+ def self.parse_team_name(long_name)
+ dash_pos = long_name =~ /-/
+ if dash_pos
+ long_name[0, dash_pos - 1]
+ else
+ long_name
+ end
+ end
+
end
end