Class: CoronaVirusCLI::Scraper

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

Constant Summary collapse

@@doc =
Nokogiri::HTML(open('https://www.worldometers.info/coronavirus/'))

Class Method Summary collapse

Class Method Details

.docObject



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

def self.doc
  @@doc
end

.get_activeObject



38
39
40
41
42
43
44
45
46
# File 'lib/corona_virus_cli/scraper.rb', line 38

def self.get_active
  active = @@doc.css('div.panel_front')[0]

  {
    active: active.css('div.number-table-main').text.strip,
    active_mild: active.css('span.number-table')[0].text.strip,
    active_severe: active.css('span.number-table')[1].text.strip
  }
end

.get_closedObject



48
49
50
51
52
53
54
55
56
# File 'lib/corona_virus_cli/scraper.rb', line 48

def self.get_closed
  closed = @@doc.css('div.panel_front')[1]

  {
    closed: closed.css('div.number-table-main').text.strip,
    closed_recovered: closed.css('span.number-table')[0].text.strip,
    closed_deaths: closed.css('span.number-table')[1].text.strip
  }
end

.get_totalsObject



29
30
31
32
33
34
35
36
# File 'lib/corona_virus_cli/scraper.rb', line 29

def self.get_totals
  totals = @@doc.css('div.maincounter-number')
  {
    total: totals[0].text.strip,
    total_deaths: totals[1].text.strip,
    total_recovered: totals[2].text.strip,
  }
end

.get_virusObject



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

def self.get_virus
  all = self.get_totals.merge(self.get_active, self.get_closed)

  virus = CoronaVirusCLI::Virus.create('World', all)
end

.get_virus_per_countryObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/corona_virus_cli/scraper.rb', line 15

def self.get_virus_per_country
  countries = @@doc.css('tbody')[0].css('tr')
  countries[0..19].each do |country|
    name = country.css('a').text.strip
    name = country.css('td')[0].text.strip if name.empty?
    total = country.css('td')[1].text.strip
    deaths = country.css('td')[3].text.strip
    recovered = country.css('td')[5].text.strip
    active = country.css('td')[6].text.strip

    CoronaVirusCLI::Country.new(name, total, deaths, recovered, active)
  end
end