Sha256: e2f19829f865fbb344c1182f42f73cd550ae44d5ae9ab1c39f0a612fab78793f

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module EPUBInfo
  class Parser
    attr_accessor :path, :metadata_document

    def self.parse(path_io)
      epubinfo = EPUBInfo::Parser.new
      epubinfo.path =  path_io.is_a?(IO) ? path_io.path : path_io
      epubinfo
    end

    def metadata_document
      @metadata_document ||= load_metadata_file
    end

    def drm_protected?
      @drm_protected ||= !!zip_file.find_entry('META-INF/rights.xml')
    end

    def zip_file
      begin
        @zip_file ||= Zip::File.open(@path)
      rescue Zip::Error => e
        raise NotAnEPUBFileError.new(e)
      end
    end

    def metadata_path
      @metadata_path ||= begin
        root_document.remove_namespaces!
        root_document.css('container rootfiles rootfile:first-child').attribute('full-path').content
      end
    end

    def metadata_type
      @metadata_type ||= begin
        root_document.remove_namespaces!
        root_document.css('container rootfiles rootfile:first-child').attribute('media-type').content
      end
    end

    private

    def root_document
      begin
        @root_document ||= Nokogiri::XML(zip_file.read('META-INF/container.xml'))
      rescue => e
        raise NotAnEPUBFileError.new(e)
      end
    end

    def load_metadata_file
      Nokogiri::XML(zip_file.read(metadata_path))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
epubinfo_with_toc-0.5.7 lib/epubinfo/parser.rb
epubinfo_with_toc-0.5.6 lib/epubinfo/parser.rb