Class: NYTBestsellers::Scraper

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

Class Method Summary collapse

Class Method Details

.get_dateObject



43
44
45
# File 'lib/nytimes/scraper.rb', line 43

def self.get_date
  get_page.css("div.date-range").text.strip
end

.get_genre_pagesObject



16
17
18
19
20
21
# File 'lib/nytimes/scraper.rb', line 16

def self.get_genre_pages
  agent = Mechanize.new
  NYTBestsellers::Genre.all.collect do |genre|
    page = agent.get("#{genre.url}")
  end
end

.get_pageObject



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

def self.get_page
  @@page ||= Mechanize.new.get("http://www.nytimes.com/books/best-sellers/")
end

.make_booksObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nytimes/scraper.rb', line 23

def self.make_books
  get_genre_pages.each do |page|
    book_genre = page.css("section h2")[0].text
    books = page.css("ol")[0].css("li article a")

    books.each do |book|
      if book.css("p")[1] != nil && book.css("p")[1].attribute("itemprop").value == "author"
        NYTBestsellers::Book.new({
          genre: book_genre,
          title: book.css("h3").text.split.collect(&:capitalize).join(" "),
          author: book.css("p")[1].text.split.delete_if{|x| x == "by"}.join(" "),
          publisher: book.css("p")[2].text,
          wol: book.css("p")[0].text,
          summary: book.css("p")[3].text
        })
      end
    end
  end
end

.make_genresObject



7
8
9
10
11
12
13
14
# File 'lib/nytimes/scraper.rb', line 7

def self.make_genres
  get_page.css("section h2").each do |category|
    NYTBestsellers::Genre.new({
      name: category.css("a").text.strip, 
      url: "http://www.nytimes.com#{category.css("a").attr("href").text}"
    })
  end
end