Sha256: 873b2fc5cc1a36759fc1325708395e4c4facab8b50e37d68b946455018a4c7a2

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'nokogiri'
module Amzwish
  class Wishlist
    include Enumerable
    def initialize(email, website = Services::WebsiteWrapper.new)
      @email = email
      @website = website
    end                 
    
    def books
      to_a
    end
    
    def each
       lists = @website.find_for(@email)
       each_page(lists[0][:id]){|p| p.books.each{|b| yield b}}
    end
    alias_method :each_book, :each
    
    private
    def each_page(wishlist_id)
      has_more_pages = true
      page_num = 1
      while(has_more_pages) do
        page =  Page.new(@website.get_page(wishlist_id, page_num))
        yield page 
        has_more_pages = page.has_next?
        page_num += 1                      
      end
    end 
    
    class Page
      def initialize(html)
        @page = Nokogiri::HTML(html)
      end
      
      def has_next?
        # look and see if the 'Next' at the end of the page is an active link
        @page.xpath('//div[@class="pagDiv"]/span[@class="pagSide"]/a/span/text()').any?{|t| t.to_s.include?("Next")}
      end
      
      def books
        @page.xpath('//tbody[@class="itemWrapper"]').collect do |e| 
          title = e.xpath('.//a[1]/text()').to_s
          asin = e.xpath("@name").to_s.split('.').last
          Book.new( title, asin)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
amzwish-0.0.0 lib/amzwish/wishlist.rb