Class: ESPNFantasyNews::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/espn_fantasy_news/parser.rb

Class Method Summary collapse

Class Method Details

.load_all_newsObject

Return all the latest news stories



43
44
45
# File 'lib/espn_fantasy_news/parser.rb', line 43

def self.load_all_news
  self.news_from_url(ESPNFantasyNews::NEWS_ENDPOINT)
end

.load_all_playersObject

Load the entire universe of ESPN players



9
10
11
12
13
14
15
16
17
18
# File 'lib/espn_fantasy_news/parser.rb', line 9

def self.load_all_players
  offset = 0
  res = []
  while offset <= 1080 do
    url = ESPNFantasyNews::PLAYER_LIST_ENDPOINT + "?startIndex=#{offset.to_s}"
    res += self.players_from_url(url)
    offset += 40
  end
  res
end

.load_team_player_ids(url) ⇒ Object

Return an array of espn player ids for the players on the team at URL



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/espn_fantasy_news/parser.rb', line 60

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

.news_from_url(url) ⇒ Object

Return all the news stories from the given url



48
49
50
51
52
53
54
55
56
57
# File 'lib/espn_fantasy_news/parser.rb', line 48

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

    ESPNFantasyNews::News.new(player_id, text)
  end
  news_attributes
end

.players_from_url(url) ⇒ Object

Load all the players at the url



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/espn_fantasy_news/parser.rb', line 21

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
    unless cell_text.include?("D/ST")
      name, teampos = cell_text.split(',')
      team, pos = self.split_string_on_char_194(teampos.strip)
      player_id = cell.css('a')[0].get_attribute('playerid').to_i
      
      pos = self.parse_pos(pos)          
      
      # Remove the asterisk from players who are on the IR
      name = name[0, name.length - 1] if name[-1, 1] == "*"
      
      ESPNFantasyNews::Player.new(name, player_id, pos, team)
    end
  end
  player_attributes.compact
end