require 'kosher' require 'barnie/helpers' module Barnie class Response include Helpers include Enumerable attr_reader :page def initialize(page) raise Error.new('Blank page') if page.body.empty? && page.code == 200 @page = page end def each(&block) page.search('#prod-container').each { |html| block.call(parse(html)) } end private def parse(html) price = extract_price(html.search('.price strong').text) hours = extract_ships_in(html.search('.availability').text) || 999 title = title(html) link = extract_link(title) isbn = extract_isbn(link) Kosher::Book.new( 'bn.com', isbn, nil, price > 0 ? 1 : 0, price > 0 ? [Kosher::Offer.new( nil, 'bn.com', Kosher::Item.new(price, 'USD', 1, Kosher::Condition.new(1), Kosher::Description.new('')), Kosher::Seller.new(nil, 'Barnes & Noble.com', nil, Kosher::Location.new('US')), Kosher::Shipping.new(0, 'USD', Kosher::Availability.new(hours)) )] : []) end end end