Class: Scraper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScraper

Returns a new instance of Scraper.



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

def initialize
  @doc = Nokogiri::HTML(open("https://www.nytimes.com/books/best-sellers/"))
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



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

def doc
  @doc
end

Instance Method Details

#scrapeObject



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

def scrape
  scrape_category_name
  scrape_book_details
end

#scrape_book_detailsObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nyt_bestsellers_cli_gem/scraper.rb', line 25

def scrape_book_details
  @doc.css("section.subcategory").each do |section|
    category = section.css("a.subcategory-heading-link").text.strip
      section.css("li.trending").each do |bestseller|
        title = bestseller.css("h3.title").text
        author = bestseller.css("p.author").text.gsub("by ", "")
        summary = bestseller.css("p.description").text
        Book.new(title, author, summary, category)
      end
    end
end

#scrape_category_nameObject



18
19
20
21
22
23
# File 'lib/nyt_bestsellers_cli_gem/scraper.rb', line 18

def scrape_category_name
  @doc.css("section.subcategory").each do |section|
    name = section.css("a.subcategory-heading-link").text.strip
    Category.new(name)
  end
end