Class: GoodreadsBooks::Book
- Inherits:
-
Object
- Object
- GoodreadsBooks::Book
- Defined in:
- lib/goodreads_books/book.rb
Constant Summary collapse
- BASE_URL =
"https://www.goodreads.com"- @@all =
[]
Instance Attribute Summary collapse
-
#author ⇒ Object
– find_by_year –.
-
#awards_year ⇒ Object
Returns the value of attribute awards_year.
-
#cate_url ⇒ Object
Returns the value of attribute cate_url.
-
#category ⇒ Object
Returns the value of attribute category.
-
#description ⇒ Object
– vote –.
-
#title ⇒ Object
Returns the value of attribute title.
-
#url ⇒ Object
– description –.
-
#vote ⇒ Object
– author –.
Class Method Summary collapse
-
.all ⇒ Object
– self.new_from_web_page –.
-
.all_by_year(awards_year) ⇒ Object
– save –.
-
.new_from_web_page(book_hash) ⇒ Object
– initialize –.
-
.populate_book_details(award_year) ⇒ Object
– get_book_details –.
Instance Method Summary collapse
-
#get_book_details ⇒ Object
– url –.
-
#initialize(attributes) ⇒ Book
constructor
A new instance of Book.
-
#save ⇒ Object
– self.all –.
Constructor Details
#initialize(attributes) ⇒ Book
8 9 10 11 12 |
# File 'lib/goodreads_books/book.rb', line 8 def initialize(attributes) attributes.each do |attr_name, attr_value| self.send("#{attr_name}=", attr_value) end end |
Instance Attribute Details
#author ⇒ Object
– find_by_year –
31 32 33 |
# File 'lib/goodreads_books/book.rb', line 31 def @author end |
#awards_year ⇒ Object
Returns the value of attribute awards_year.
2 3 4 |
# File 'lib/goodreads_books/book.rb', line 2 def awards_year @awards_year end |
#cate_url ⇒ Object
Returns the value of attribute cate_url.
2 3 4 |
# File 'lib/goodreads_books/book.rb', line 2 def cate_url @cate_url end |
#category ⇒ Object
Returns the value of attribute category.
2 3 4 |
# File 'lib/goodreads_books/book.rb', line 2 def category @category end |
#description ⇒ Object
– vote –
41 42 43 |
# File 'lib/goodreads_books/book.rb', line 41 def description @description end |
#title ⇒ Object
Returns the value of attribute title.
2 3 4 |
# File 'lib/goodreads_books/book.rb', line 2 def title @title end |
#url ⇒ Object
– description –
46 47 48 |
# File 'lib/goodreads_books/book.rb', line 46 def url @url end |
#vote ⇒ Object
– author –
36 37 38 |
# File 'lib/goodreads_books/book.rb', line 36 def vote @vote end |
Class Method Details
.all ⇒ Object
– self.new_from_web_page –
19 20 21 |
# File 'lib/goodreads_books/book.rb', line 19 def self.all @@all end |
.all_by_year(awards_year) ⇒ Object
– save –
27 28 29 |
# File 'lib/goodreads_books/book.rb', line 27 def self.all_by_year(awards_year) all.select { |book| book.awards_year == awards_year } end |
.new_from_web_page(book_hash) ⇒ Object
– initialize –
14 15 16 17 |
# File 'lib/goodreads_books/book.rb', line 14 def self.new_from_web_page(book_hash) book = new(book_hash) book.save end |
.populate_book_details(award_year) ⇒ Object
– get_book_details –
70 71 72 73 74 75 |
# File 'lib/goodreads_books/book.rb', line 70 def self.populate_book_details(award_year) all_by_year(awards_year).each do |book| book.get_book_details end #binding.pry end |
Instance Method Details
#get_book_details ⇒ Object
– url –
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/goodreads_books/book.rb', line 51 def get_book_details # Next level of scraping (get details of best book within each category_url) book_doc = Nokogiri::HTML(open(self.cate_url)) self.vote = book_doc.css(".gcaRightContainer .gcaWinnerHeader").text.split(" ")[1] self. = book_doc.css(".gcaRightContainer h3 .gcaAuthor a.authorName").text self.url = "#{BASE_URL}#{book_doc.css(".gcaRightContainer h3 a.winningTitle").attr("href").text}" # goodreads description is encoded, so need to add .encode("ISO-8859-1") to print the special characters eg. â\u0080\u0099s in printable character of ' # if self.awards_year < 2017, use the span tag, else there's no span tag so don't check for it descript = book_doc.css(".gcaRightContainer .readable.stacked span")[1] if descript self.description = book_doc.css(".gcaRightContainer .readable.stacked span")[1].text.encode("ISO-8859-1") else self.description = book_doc.css(".gcaRightContainer .readable.stacked").text.encode("ISO-8859-1") end #binding.pry end |
#save ⇒ Object
– self.all –
23 24 25 |
# File 'lib/goodreads_books/book.rb', line 23 def save self.class.all << self end |