Class: TableScraper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTableScraper

Returns a new instance of TableScraper.



5
6
7
# File 'lib/table_scraper.rb', line 5

def initialize
  @url = "https://www.theguardian.com/football/" + LeagueReference::league_full_name + "/table"
end

Instance Attribute Details

#teamsObject

Returns the value of attribute teams.



3
4
5
# File 'lib/table_scraper.rb', line 3

def teams
  @teams
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/table_scraper.rb', line 3

def url
  @url
end

Instance Method Details

#build_teamsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/table_scraper.rb', line 15

def build_teams
  teams.length.times do |i|
    source = teams[i].children.text
    team_data = source.split("\n").select { |s| s != "" }

    #["1", "Spurs", "2", "2", "0", "0", "5", "0", "5", "6"]
    #Pos  Team  P W D L F A GD  Pts

    Team.new({
      :position => team_data[0],
      :name => team_data[1],
      :played => team_data[2],
      :points => team_data[9]
    })
  end
end

#callObject



9
10
11
12
13
# File 'lib/table_scraper.rb', line 9

def call
  site = Nokogiri::HTML(open(@url))
  self.teams = site.css('.table--league-table tbody tr')
  build_teams
end