Class: DeveloperNewsCliApp::FreeCodeCampScrapper

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

Constant Summary collapse

@@all =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.article_countObject



31
32
33
# File 'lib/developer_news_cli_app/FreeCodeCampScrapper.rb', line 31

def self.article_count
  @@all.count
end

.articlesObject



27
28
29
# File 'lib/developer_news_cli_app/FreeCodeCampScrapper.rb', line 27

def self.articles
  @@all
end

Instance Method Details

#get_articlesObject



8
9
10
# File 'lib/developer_news_cli_app/FreeCodeCampScrapper.rb', line 8

def get_articles
  self.get_page.css(".postArticle")
end

#get_pageObject



4
5
6
# File 'lib/developer_news_cli_app/FreeCodeCampScrapper.rb', line 4

def get_page
  Nokogiri::HTML(open("https://medium.freecodecamp.org/"))
end

#make_articleObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/developer_news_cli_app/FreeCodeCampScrapper.rb', line 12

def make_article
  self.get_articles.each do |article|
    a = DeveloperNewsCliApp::Article.new
    a.title = article.css("h3").text
    a.author = article.css(".ds-link").text
    a.date = article.css("time").text
    a.read_time = article.css(".readingTime").attribute("title").value
    a.url = article.css(".postArticle-readMore a").attribute("href").value
    a.website = "FreeCodeCamp"
    a.subtitle = article.css("h4").text == "" ? nil : article.css("h4").text
    a.trailing = article.css("p").text == "" ? nil : article.css("p").text 
    @@all << a
  end
end