Class: Scraper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScraper

Returns a new instance of Scraper.



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

def initialize
    @newspaper = Newspaper.new
    @doc = Nokogiri::HTML(open("https://www.economist.com/sections/business-finance"))
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



7
8
9
# File 'lib/scraper.rb', line 7

def doc
  @doc
end

#newspaperObject (readonly)

Returns the value of attribute newspaper.



7
8
9
# File 'lib/scraper.rb', line 7

def newspaper
  @newspaper
end

Instance Method Details

#articlesObject



14
15
16
# File 'lib/scraper.rb', line 14

def articles
    @doc.css("article.teaser")
end

#categoryObject



26
27
28
# File 'lib/scraper.rb', line 26

def category
    @doc.css("h1.simple-header__name").text
end

#content(article) ⇒ Object



22
23
24
# File 'lib/scraper.rb', line 22

def content(article)
    article.css("div.teaser__text").text
end

#create_articlesObject



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

def create_articles
    cat = category
    articles.each do |x|
        article = Article.new(title(x))
        article.category = cat
        article.content = content(x)
        @newspaper.add_article(article)
    end
end

#title(article) ⇒ Object



18
19
20
# File 'lib/scraper.rb', line 18

def title(article)
    article.css("span.flytitle-and-title__title").text
end