Class: Goodreads::Book

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

Overview

Represents a Book on goodreads.com

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#isbnObject

Returns the value of attribute isbn.



5
6
7
# File 'lib/goodreads/book.rb', line 5

def isbn
  @isbn
end

#query_urlObject

Returns the value of attribute query_url.



5
6
7
# File 'lib/goodreads/book.rb', line 5

def query_url
  @query_url
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/goodreads/book.rb', line 5

def url
  @url
end

Instance Method Details

#authorsObject

Returns an array with the authors of the book



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

def authors
  document.search("[@id='bookAuthors']/[@itemprop='author']/a/span").map { |elem| elem.innerHTML.strip } rescue []
end

#charactersObject

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

#coverObject

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

#descriptionObject

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_tags.goodreads_strip_bad_description_content rescue nil
end

#genresObject

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_idObject

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_pagesObject

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

#ratingObject

Returns a string with the current Goodreads rating



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

def rating
  document.at("[@id='bookMeta']/*/[@itemprop='ratingValue']").innerHTML.strip rescue nil
end

#titleObject

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