lib/libri/book.rb in libri-0.2.1 vs lib/libri/book.rb in libri-0.2.2
- old
+ new
@@ -1,31 +1,31 @@
class Libri::Book
- attr_accessor :title_by_author, :about_author, :availability, :blurbs_and_plot, :url, :excerpt, :related_books, :book
+ attr_accessor :title_by_author, :blurbs_and_plot, :about_author,:excerpt, :availability, :url, :book
- def self.scrape_book(book)
- html = book[:url]
- book_page = Nokogiri::HTML(open(html))
- info_section = book_page.css("div.tabpanel")
+ @@all = []
- related_books_hash = {}
+ def initialize(book_info_hash)
+ book_info_hash.each { |key, val|
+ send "#{key}=", val
+ }
+ @@all << self
+ end
- book_info_hash = {
- :title_by_author => info_section.css("div#productInfoOverview div.mb-m").text,
- :blurbs_and_plot => info_section.css("div#productInfoOverview p").map(&:text).join("\n").strip,
- :about_author => info_section.css("div#MeetTheAuthor div.text--medium").text.strip,
- :excerpt => info_section.xpath("//div[@class='read-an-excerpt']/p[not(@class) and position()<7]").map(&:text).join("\n"),
- # :related_books => book_page.css("div.product-shelf-info").each { |book|
- # related_books_hash = {
- # :title => book.css("div.product-shelf-title").text.strip,
- # :author => book.css("div.product-shelf-author").text.strip,
- # :url => "https://www.barnesandnoble.com" + book.css("a").attribute("href").value
- # }
- # },
- :availability => book_page.css("button#pdp-marketplace-btn").text.chomp,
- :url => book[:url]
+ def self.create_from_collection(book_info_array)
+ book_info_array.each { |book_info_hash|
+ self.new(book_info_hash)
}
+ end
- book_info_hash.delete_if { |key, val| val.to_s.strip.empty? }
-
+ def add_book_info(book_info_hash)
+ book_info_hash.each { |key, val|
+ send "#{key}=", val
+ }
+ self
end
+
+ def self.all
+ @@all
+ end
+
end