Class: DarwinCore

Inherits:
Object
  • Object
show all
Defined in:
lib/dwc_archive.rb,
lib/dwc_archive/core.rb,
lib/dwc_archive/errors.rb,
lib/dwc_archive/archive.rb,
lib/dwc_archive/version.rb,
lib/dwc_archive/expander.rb,
lib/dwc_archive/ingester.rb,
lib/dwc_archive/metadata.rb,
lib/dwc_archive/extension.rb,
lib/dwc_archive/generator.rb,
lib/dwc_archive/gnub_taxon.rb,
lib/dwc_archive/xml_reader.rb,
lib/dwc_archive/taxon_normalized.rb,
lib/dwc_archive/generator_eml_xml.rb,
lib/dwc_archive/generator_meta_xml.rb,
lib/dwc_archive/classification_normalizer.rb

Overview

Version constant of the class

Defined Under Namespace

Modules: Ingester, XmlReader Classes: Archive, ClassificationNormalizer, Core, CoreFileError, EncodingError, Error, Expander, Extension, ExtensionFileError, FileNotFoundError, Generator, GeneratorError, GnubTaxon, InvalidArchiveError, Metadata, ParentNotCurrentError, SynonymNormalized, TaxonNormalized, UnpackingError, VernacularNormalized

Constant Summary collapse

DEFAULT_TMP_DIR =
"/tmp"
VERSION =
"1.0.1"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dwc_path, tmp_dir = DEFAULT_TMP_DIR) ⇒ DarwinCore

Returns a new instance of DarwinCore


79
80
81
82
83
84
85
# File 'lib/dwc_archive.rb', line 79

def initialize(dwc_path, tmp_dir = DEFAULT_TMP_DIR)
  @dwc_path = dwc_path
  @archive = DarwinCore::Archive.new(@dwc_path, tmp_dir)
  @core = DarwinCore::Core.new(self)
  @metadata = DarwinCore::Metadata.new(@archive)
  extensions
end

Class Attribute Details

.loggerObject


67
68
69
# File 'lib/dwc_archive.rb', line 67

def self.logger
  @logger ||= Logger.new(nil)
end

Instance Attribute Details

#archiveObject (readonly)

Returns the value of attribute archive


50
51
52
# File 'lib/dwc_archive.rb', line 50

def archive
  @archive
end

#classification_normalizerObject (readonly)

Returns the value of attribute classification_normalizer


50
51
52
# File 'lib/dwc_archive.rb', line 50

def classification_normalizer
  @classification_normalizer
end

#coreObject (readonly)

Returns the value of attribute core


50
51
52
# File 'lib/dwc_archive.rb', line 50

def core
  @core
end

#metadataObject (readonly) Also known as: eml

Returns the value of attribute metadata


50
51
52
# File 'lib/dwc_archive.rb', line 50

def 
  @metadata
end

Class Method Details

.clean(path) ⇒ Object


36
37
38
# File 'lib/dwc_archive.rb', line 36

def clean(path)
  FileUtils.rm_rf(path) if FileTest.exists?(path)
end

.clean_all(tmp_dir = DEFAULT_TMP_DIR) ⇒ Object


58
59
60
61
62
63
64
65
# File 'lib/dwc_archive.rb', line 58

def self.clean_all(tmp_dir = DEFAULT_TMP_DIR)
  Dir.entries(tmp_dir).each do |entry|
    path = File.join(tmp_dir, entry)
    if FileTest.directory?(path) && entry.match(/^dwc_[\d]+$/)
      FileUtils.rm_rf(path)
    end
  end
end

.files(path) ⇒ Object


40
41
42
43
# File 'lib/dwc_archive.rb', line 40

def files(path)
  return nil unless path && FileTest.exists?(path)
  Dir.entries(path).reject { |e| e.match(/[\.]{1,2}$/) }.sort
end

.logger_resetObject


71
72
73
# File 'lib/dwc_archive.rb', line 71

def self.logger_reset
  self.logger = Logger.new(nil)
end

.logger_write(obj_id, message, method = :info) ⇒ Object


75
76
77
# File 'lib/dwc_archive.rb', line 75

def self.logger_write(obj_id, message, method = :info)
  logger.send(method, "|#{obj_id}|#{message}|")
end

.nil_field?(field) ⇒ Boolean

Returns:

  • (Boolean)

53
54
55
56
# File 'lib/dwc_archive.rb', line 53

def self.nil_field?(field)
  return true if [nil, "", "/N"].include?(field)
  false
end

.random_path(tmp_dir) ⇒ Object


45
46
47
# File 'lib/dwc_archive.rb', line 45

def random_path(tmp_dir)
  File.join(tmp_dir, "dwc_" + rand(10_000_000_000).to_s)
end

Instance Method Details

#checksumObject


109
110
111
# File 'lib/dwc_archive.rb', line 109

def checksum
  Digest::SHA1.hexdigest(File.read(@dwc_path))
end

#extensionsObject


113
114
115
116
117
118
119
120
# File 'lib/dwc_archive.rb', line 113

def extensions
  return @extensions if @extensions
  root_key = @archive.meta.keys[0]
  ext = @archive.meta[root_key][:extension]
  return @extensions = [] unless ext
  ext = [ext] if ext.class != Array
  @extensions = ext.map { |e| DarwinCore::Extension.new(self, e) }
end

#file_nameObject


87
88
89
# File 'lib/dwc_archive.rb', line 87

def file_name
  File.split(@dwc_path).last
end

#normalize_classificationObject

generates a hash from a classification data with path to each node, list of synonyms and vernacular names.


97
98
99
100
101
102
# File 'lib/dwc_archive.rb', line 97

def normalize_classification
  return nil unless parent_id?
  @classification_normalizer ||=
    DarwinCore::ClassificationNormalizer.new(self)
  @classification_normalizer.normalize
end

#parent_id?Boolean

Returns:

  • (Boolean)

104
105
106
107
# File 'lib/dwc_archive.rb', line 104

def parent_id?
  !@core.fields.join("|").
    downcase.match(/highertaxonid|parentnameusageid/).nil?
end

#pathObject


91
92
93
# File 'lib/dwc_archive.rb', line 91

def path
  File.expand_path(@dwc_path)
end