Class: CampusBooks::Book

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

Constant Summary collapse

SUPPORTED_PARAMS =
[
  :isbn10,          # Ten-Digit ISBN for this book
  :isbn13,          # Thirteen-Digit ISBN for this book
  :title,           # Book Title
  :author,          # Book Author
  :binding,         # Book Binding
  :msrp,            # List price for the book
  :pages,           # Number of pages in the book
  :publisher,       # Book Publisher
  :published_date,  # Published Date
  :edition,         # Edition
  :description      # A text description for the book
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Book

Returns a new instance of Book.



40
41
42
43
44
45
46
# File 'lib/campusbooks/book.rb', line 40

def initialize(params = {})
  create_offers(params.delete('offers')) if params.key?('offers')

  SUPPORTED_PARAMS.each do |param|
    instance_variable_set("@#{param}", params[param.to_s]) if params.key?(param.to_s)
  end
end

Class Method Details

.find(isbn, opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/campusbooks/book.rb', line 21

def self.find(isbn, opts = {})
  isbn = isbn.to_s
  canonical_isbn = (ISBN_Tools.is_valid_isbn13?(isbn) ? isbn : ISBN_Tools.isbn10_to_isbn13(isbn)) or raise ArgumentError.new('isbn is invalid')
  
  include_prices = opts[:include] && [*opts[:include]].include?(:prices)

  raw_result = get_response(include_prices ? '/bookprices' : '/bookinfo', :query => { 
    :isbn => canonical_isbn, :key => CampusBooks.api_key 
  })
  
  book_params = include_prices ? raw_result['page']['book'] : raw_result['page']
  
  if raw_result['page']['offers']
    book_params['offers'] = flatten_offers(raw_result['page']['offers'])
  end
  
  new(book_params)
end

Instance Method Details

#offersObject



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

def offers
  if !@offers_loaded
    raw_offers = get_response('/prices', :query => { :isbn => @isbn, :key => CampusBooks.api_key })['page']
    create_offers(self.class.flatten_offers(raw_offers))
  end
  @offers
end

#published_dateObject



48
49
50
# File 'lib/campusbooks/book.rb', line 48

def published_date
  @parsed_published_date ||= Date.parse(@published_date)
end