Class: DarwinCore::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/dwc-archive/generator.rb,
lib/dwc-archive/generator_eml_xml.rb,
lib/dwc-archive/generator_meta_xml.rb

Defined Under Namespace

Classes: EmlXml, MetaXml

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dwc_path, tmp_dir = DEFAULT_TMP_DIR) ⇒ Generator

TODO refactor – for now copying expander methods



6
7
8
9
10
11
12
13
# File 'lib/dwc-archive/generator.rb', line 6

def initialize(dwc_path, tmp_dir = DEFAULT_TMP_DIR)
  @dwc_path = dwc_path
  @path = File.join(tmp_dir, 'dwc_' + rand(10000000000).to_s)
  FileUtils.mkdir(@path)
  @meta_xml_data = {:extensions => []}
  @eml_xml_data = {:id => nil, :title => nil, :authors => [], :abstract => nil, :citation => nil, :url => nil}
  @write = 'w:utf-8'
end

Instance Attribute Details

#eml_xml_dataObject (readonly)

Returns the value of attribute eml_xml_data.



3
4
5
# File 'lib/dwc-archive/generator.rb', line 3

def eml_xml_data
  @eml_xml_data
end

Instance Method Details

#add_core(data, file_name, keep_headers = true) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dwc-archive/generator.rb', line 20

def add_core(data, file_name, keep_headers = true)
  c = CSV.open(File.join(@path,file_name), @write)
  header = data.shift
  fields = header.map do |f|
    f.strip!
    raise DarwinCore::GeneratorError.new("No header in core data, or header fields are not urls") unless f.match(/^http:\/\//)
    f.split("/")[-1]
  end
  data.unshift(fields) if keep_headers
  ignore_header_lines = keep_headers ? 1 : 0
  @meta_xml_data[:core] = {:fields => header, :ignoreHeaderLines => ignore_header_lines, :location => file_name}
  data.each {|d| c << d}
  c.close
end

#add_eml_xml(data) ⇒ Object



55
56
57
58
59
# File 'lib/dwc-archive/generator.rb', line 55

def add_eml_xml(data)
  @eml_xml_data = data
  eml = DarwinCore::Generator::EmlXml.new(@eml_xml_data, @path)
  eml.create
end

#add_extension(data, file_name, keep_headers = true, row_type = "http://rs.tdwg.org/dwc/terms/Taxon") ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dwc-archive/generator.rb', line 35

def add_extension(data, file_name, keep_headers = true, row_type = "http://rs.tdwg.org/dwc/terms/Taxon")
  c = CSV.open(File.join(@path,file_name), @write)
  header = data.shift
  fields = header.map do |f|
    f.strip!
    raise DarwinCore::GeneratorError.new("No header in core data, or header fields are not urls") unless f.match(/^http:\/\//)
    f.split("/")[-1]
  end
  data.unshift(fields) if keep_headers
  ignore_header_lines = keep_headers ? 1 : 0
  @meta_xml_data[:extensions] << { :fields => header, :ignoreHeaderLines => ignore_header_lines, :location => file_name, :rowType => row_type }
  data.each { |d| c << d }
  c.close
end

#add_meta_xmlObject



50
51
52
53
# File 'lib/dwc-archive/generator.rb', line 50

def add_meta_xml
  meta = DarwinCore::Generator::MetaXml.new(@meta_xml_data, @path)
  meta.create
end

#cleanObject

TODO refactor!



16
17
18
# File 'lib/dwc-archive/generator.rb', line 16

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

#filesObject



65
66
67
68
# File 'lib/dwc-archive/generator.rb', line 65

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

#packObject



70
71
72
73
# File 'lib/dwc-archive/generator.rb', line 70

def pack
  a = "cd #{@path}; tar -zcf #{@dwc_path} *"
  system(a)
end

#pathObject



61
62
63
# File 'lib/dwc-archive/generator.rb', line 61

def path
  @path
end