Sha256: 48c4a138338bdf0b3ff5640c81f00d0acd4c9e7559e91efdafc40d480a4de21c

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module KindleManager
  class ListParser
    class BookRow
      def initialize(node)
        @node = node
      end

      def inspect
        "#<#{self.class.name}:#{self.object_id} #{self.to_hash}>"
      end

      def asin
        @_asin ||= @node['name'].gsub(/\AcontentTabList_/, '')
      end

      def title
        @_title ||= @node.css("div[id^='title']").text
      end

      def tag
        @_tag ||= @node.css("div[id^='listViewTitleTag']").css('.myx-text-bold').first.text.strip
      end

      def author
        @_author ||= @node.css("div[id^='author']").text
      end

      def date
        @_date ||= begin
          m = @node.css("div[id^='date']").text.match(/\A(?<year>\d{4})年(?<month>\d{1,2})月(?<day>\d{1,2})日\z/)
          Date.new(m[:year].to_i, m[:month].to_i, m[:day].to_i)
        end
      end

      def collection_count
        @_collection_count ||= @node.css(".collectionsCount .myx-collection-count").first.text.strip.to_i
      end

      def to_hash
        hash = {}
        %w[asin title tag author date collection_count].each do |f|
          hash[f] = send(f)
        end
        hash
      end
    end

    def initialize(filepath, options = {})
      @filepath = filepath
    end

    def book_list
      @book_list ||= doc.css("div[id^='contentTabList_']").map{|e| BookRow.new(e) }
    end

    def doc
      @doc ||= Nokogiri::HTML(body)
    end

    def body
      @body ||= File.read(@filepath)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kindle_manager-0.1.0 lib/kindle_manager/list_parser.rb