Class: TechNews::Article

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#articlesObject

Returns the value of attribute articles.



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

def articles
  @articles
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.allObject



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

def self.all
  @@all = self.save
end

.saveObject

scrape_articles



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

def self.save #scrape_articles
  articles = []
  articles << self.scrape_google
  articles.flatten!
end

.scrape_googleObject

go to Google Technology News, find and scrape articles, instantiate an article



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

def self.scrape_google # go to Google Technology News, find and scrape articles, instantiate an article
  data = []
  doc = Nokogiri::HTML(open("https://news.google.com/news/headlines/section/topic/TECHNOLOGY?ned=us&hl=en"))
  title_array = doc.css("a.nuEeue").select {|element| element['aria-level'] == "2"}
  title_array.each do |element|
    article = self.new
    article.title = element.children.text
    article.url = element.attributes['href'].value
    data << article
  end
  data
end