Class: NotableBooks2018::Scraper

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

Class Method Summary collapse

Class Method Details

.create_genres(css) ⇒ Object



28
29
30
31
32
33
# File 'lib/notable_books_2018/scraper.rb', line 28

def self.create_genres(css)
  genre_data = css
  genre_data.collect do |genre_name|
    NotableBooks2018::Genre.find_or_create_by_name(genre_name)
  end
end

.scrape_book_infoObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/notable_books_2018/scraper.rb', line 7

def self.scrape_book_info
  books_array = []
   scrape_page.css(".g-book-data").each.with_index do |nodeset|
     book_hash = {}

     details = nodeset.css(".g-book-author").text
      .split(". ").map!(&:strip)

     book_hash[:title] = nodeset.css(".g-book-title").text.strip
     book_hash[:author] = nodeset.css(".g-book-author b").text.strip.chomp(".")
     book_hash[:genre] = create_genres(nodeset.css(".g-book-tag").text
      .split(".").map!(&:strip).reject(&:empty?))
     book_hash[:description] = nodeset.css(".g-book-description").text.strip
     book_hash[:price] = details.sort.first
     book_hash[:publisher] = details.last.chomp(".")
     books_array << book_hash
    end

    NotableBooks2018::Book.create_from_collection(books_array)
end

.scrape_pageObject



2
3
4
5
# File 'lib/notable_books_2018/scraper.rb', line 2

def self.scrape_page
  url = "https://www.nytimes.com/interactive/2018/11/19/books/review/100-notable-books.html"
  Nokogiri::HTML(open(url))
end