Sha256: 2b25be3b6c2a18f4e1bc86c5e469ab14166ec57f3cb2c53df0d2853f1f8d32a4

Contents?: true

Size: 1.28 KB

Versions: 6

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

class DarwinCore
  # Deals with handling DarwinCoreArchive file, and provides meta information
  # and files information about archive
  class Archive
    attr_reader :meta, :eml

    def initialize(archive_path, tmp_dir)
      @archive_path = archive_path
      @tmp_dir = tmp_dir
      @expander = DarwinCore::Expander.new(@archive_path, @tmp_dir)
      @expander.unpack
      prepare_metadata
    end

    def valid?
      valid = true
      valid = valid && @expander.path && FileTest.exists?(@expander.path)
      valid && files && files.include?("meta.xml")
    end

    def files
      @expander.files
    end

    def files_path
      @expander.path
    end

    def clean
      @expander.clean
    end

    private

    def prepare_metadata
      if valid?
        prepare_meta_file
        prepare_eml_file
      else
        clean
        raise InvalidArchiveError
      end
    end

    def prepare_meta_file
      meta_file = File.open(File.join(@expander.path, "meta.xml"))
      @meta = DarwinCore::XmlReader.from_xml(meta_file)
    end

    def prepare_eml_file
      @eml = nil
      return unless files.include?("eml.xml")

      eml_file = File.open(File.join(@expander.path, "eml.xml"))
      @eml = DarwinCore::XmlReader.from_xml(eml_file)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dwc-archive-1.1.7 lib/dwc_archive/archive.rb
dwc-archive-1.1.6 lib/dwc_archive/archive.rb
dwc-archive-1.1.5 lib/dwc_archive/archive.rb
dwc-archive-1.1.4 lib/dwc_archive/archive.rb
dwc-archive-1.1.3 lib/dwc_archive/archive.rb
dwc-archive-1.1.2 lib/dwc_archive/archive.rb