Class: Headlines

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, css_class) ⇒ Headlines

Returns a new instance of Headlines.



6
7
8
9
# File 'lib/covid_19_updates/headlines.rb', line 6

def initialize(url, css_class)
  @url = url
  @css_class = css_class
end

Instance Attribute Details

#css_classObject (readonly)

Returns the value of attribute css_class.



4
5
6
# File 'lib/covid_19_updates/headlines.rb', line 4

def css_class
  @css_class
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/covid_19_updates/headlines.rb', line 4

def url
  @url
end

Instance Method Details

#show_headlinesObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/covid_19_updates/headlines.rb', line 11

def show_headlines
  doc = Nokogiri::HTML(URI.open(url))
  items = doc.css(css_class)

  array = []

  if url.include?('foxnews')
    items.each do |item|
      if item.at_css('h4.title a')&.text != nil
        array << { title: item.at_css('h4.title a')&.text, link: item.at_css('h4.title a')&.attribute('href')&.value }
      end
    end
  else
    items.each do |item|
      array << if url.include?('cnbc')
                 { title: item.text.strip.red, link: item.children.attribute('href').value }
               else
                 { title: item.text.strip.red, link: item.attribute('href').value }
               end
    end
  end

  array
end