Class: GoodreadsBooks::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/goodreads_books/book.rb

Constant Summary collapse

BASE_URL =
"https://www.goodreads.com"
@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#authorObject

– find_by_year –



31
32
33
# File 'lib/goodreads_books/book.rb', line 31

def author
  @author
end

#awards_yearObject

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_urlObject

Returns the value of attribute cate_url.



2
3
4
# File 'lib/goodreads_books/book.rb', line 2

def cate_url
  @cate_url
end

#categoryObject

Returns the value of attribute category.



2
3
4
# File 'lib/goodreads_books/book.rb', line 2

def category
  @category
end

#descriptionObject

– vote –



41
42
43
# File 'lib/goodreads_books/book.rb', line 41

def description
  @description
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/goodreads_books/book.rb', line 2

def title
  @title
end

#urlObject

– description –



46
47
48
# File 'lib/goodreads_books/book.rb', line 46

def url
  @url
end

#voteObject

– author –



36
37
38
# File 'lib/goodreads_books/book.rb', line 36

def vote
  @vote
end

Class Method Details

.allObject

– 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_detailsObject

– 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.author = 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

#saveObject

– self.all –



23
24
25
# File 'lib/goodreads_books/book.rb', line 23

def save
  self.class.all << self
end