Class: GoGoodreads::Book

Inherits:
Object
  • Object
show all
Extended by:
Request
Includes:
Resource
Defined in:
lib/go_goodreads/book.rb

Constant Summary

Constants included from Request

Request::BASE_URL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

request

Methods included from Resource

included, #initialize

Instance Attribute Details

#authorsObject (readonly)

Returns the value of attribute authors.



25
26
27
# File 'lib/go_goodreads/book.rb', line 25

def authors
  @authors
end

#current_pageObject

Returns the value of attribute current_page.



27
28
29
# File 'lib/go_goodreads/book.rb', line 27

def current_page
  @current_page
end

Class Method Details

.initialize_with_node(xml) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/go_goodreads/book.rb', line 51

def self.initialize_with_node(xml)
  attrs = to_attributes!(xml)
  attrs.delete_if { |k,v| v.respond_to?(:empty?) && v.empty? }

  book = new(attrs)
  book.initialize_authors_with_nodeset(xml.search('authors > author'))
  book.initialize_reviews_with_nodeset(xml.search('reviews > review'))
  book
end

.show_by_isbn(isbn, opts = {}) ⇒ GoGoodreads::Book

Parameters:

  • isbn (String)

    ISBN of the book

  • opts (Hash) (defaults to: {})

    Option passed to the API call, except the ISBN.

Options Hash (opts):

  • :page (Integer) — default: 1

    Page number

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/go_goodreads/book.rb', line 33

def self.show_by_isbn(isbn, opts = {})
  params = { :isbn => isbn }
  params.merge!(opts)

  request('/book/isbn', params) do |xml|
    book = initialize_with_node(xml.root.at('book'))
    book.current_page = params[:page] || 1
    book
  end

rescue ::RestClient::Exception => ex
  if ex.http_code == 404
    return nil
  else
    raise
  end
end

Instance Method Details

#initialize_authors_with_nodeset(xml) ⇒ Object



61
62
63
# File 'lib/go_goodreads/book.rb', line 61

def initialize_authors_with_nodeset(xml)
  @authors = xml.map {|n| GoGoodreads::Author.initialize_with_node(n) }
end

#initialize_reviews_with_nodeset(xml) ⇒ Object



65
66
67
# File 'lib/go_goodreads/book.rb', line 65

def initialize_reviews_with_nodeset(xml)
  @reviews = xml.map {|n| GoGoodreads::Review.initialize_with_node(n) }
end

#publication_dateObject



70
71
72
73
74
# File 'lib/go_goodreads/book.rb', line 70

def publication_date
  if publication_day && publication_year && publication_month
    Date.new(publication_year, publication_month, publication_day)
  end
end