Class: Goodreads::Book
- Inherits:
-
Object
- Object
- Goodreads::Book
- Defined in:
- lib/goodreads/book.rb
Overview
Represents a Book on goodreads.com
Instance Attribute Summary collapse
-
#isbn ⇒ Object
Returns the value of attribute isbn.
-
#query_url ⇒ Object
Returns the value of attribute query_url.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#authors ⇒ Object
Returns an array with the authors of the book.
-
#characters ⇒ Object
Returns an array with the main characters of the book.
-
#cover ⇒ Object
Returns a string containing the URL for the cover file.
-
#description ⇒ Object
Retursn a string with the book’s description.
-
#genres ⇒ Object
Returns an array with the genres of the book.
-
#goodreads_id ⇒ Object
Returns the id used by Goodreads (does not trigger lazy loading).
-
#initialize(isbn) ⇒ Book
constructor
Initialize a new Goodreads book object with its ISBN (as a String).
-
#number_of_pages ⇒ Object
Returns an integer with the number of pages.
-
#rating ⇒ Object
Returns a string with the current Goodreads rating.
-
#title ⇒ Object
Returns a string containing the title.
Constructor Details
#initialize(isbn) ⇒ Book
Initialize a new Goodreads book object with its ISBN (as a String)
book = Goodreads::Book.new("9780375403170")
Goodreads::Book objects are lazy loading, meaning that no HTTP request will be performed when a new object is created. Only when you use an accessor that needs the remote data, a HTTP request is made (once).
15 16 17 18 |
# File 'lib/goodreads/book.rb', line 15 def initialize(isbn) @isbn = isbn @query_url = Goodreads::Book.make_query_url(isbn) end |
Instance Attribute Details
#isbn ⇒ Object
Returns the value of attribute isbn.
5 6 7 |
# File 'lib/goodreads/book.rb', line 5 def isbn @isbn end |
#query_url ⇒ Object
Returns the value of attribute query_url.
5 6 7 |
# File 'lib/goodreads/book.rb', line 5 def query_url @query_url end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/goodreads/book.rb', line 5 def url @url end |
Instance Method Details
#authors ⇒ Object
Returns an array with the authors of the book
36 37 38 |
# File 'lib/goodreads/book.rb', line 36 def document.search("[@id='bookAuthors']/[@itemprop='author']/a/span").map { |elem| elem.innerHTML.strip } rescue [] end |
#characters ⇒ Object
Returns an array with the main characters of the book
51 52 53 |
# File 'lib/goodreads/book.rb', line 51 def characters document.search("[@id='bookDataBox']/*/[@class='infoBoxRowItem']/a").select {|elem| elem['href'] =~ /character/}.map {|elem| elem.innerHTML.strip} rescue [] end |
#cover ⇒ Object
Returns a string containing the URL for the cover file
31 32 33 |
# File 'lib/goodreads/book.rb', line 31 def cover document.at("img[@id='coverImage']")['src'] rescue nil end |
#description ⇒ Object
Retursn a string with the book’s description
46 47 48 |
# File 'lib/goodreads/book.rb', line 46 def description document.at("[@id='description']/span[1]").innerHTML..goodreads_strip_bad_description_content rescue nil end |
#genres ⇒ Object
Returns an array with the genres of the book
61 62 63 |
# File 'lib/goodreads/book.rb', line 61 def genres document.search("[@class='actionLinkLite']").select { |elem| elem['href'] =~ /genres/ }.map { |elem| elem.innerHTML.strip } rescue [] end |
#goodreads_id ⇒ Object
Returns the id used by Goodreads (does not trigger lazy loading)
21 22 23 |
# File 'lib/goodreads/book.rb', line 21 def goodreads_id @url.match(/\/(\d+)\./)[1] rescue nil end |
#number_of_pages ⇒ Object
Returns an integer with the number of pages
56 57 58 |
# File 'lib/goodreads/book.rb', line 56 def number_of_pages document.at("[@itemprop='numberOfPages']").innerHTML.scan(/\d+/).first.to_i rescue nil end |
#rating ⇒ Object
Returns a string with the current Goodreads rating
41 42 43 |
# File 'lib/goodreads/book.rb', line 41 def document.at("[@id='bookMeta']/*/[@itemprop='ratingValue']").innerHTML.strip rescue nil end |
#title ⇒ Object
Returns a string containing the title
26 27 28 |
# File 'lib/goodreads/book.rb', line 26 def title document.search(".bookTitle").innerHTML.strip rescue nil end |