Skip to content

Commit

Permalink
refactoring of xml eml builder
Browse files Browse the repository at this point in the history
  • Loading branch information
dimus committed Dec 30, 2013
1 parent 915be98 commit aa40735
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 49 deletions.
127 changes: 82 additions & 45 deletions lib/dwc-archive/generator_eml_xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,9 @@ def create
:'xmlns:res' => 'eml://ecoinformatics.org/resource-2.1.1',
:'xmlns:dc' => 'http://purl.org/dc/terms/',
:'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
:'xsi:schemaLocation' => 'eml_uri') do
xml.dataset(id: @data[:id]) do
xml.title(@data[:title])
xml.license(@data[:license])
contacts = []
@data[:authors].each_with_index do |a, i|
creator_id = i + 1
contacts << creator_id
xml.creator(id: creator_id, scope: 'document') do
xml.individualName do
xml.givenName(a[:first_name])
xml.surName(a[:last_name])
end
xml.organizationName(a[:organization]) if a[:organization]
xml.positionName(a[:position]) if a[:position]
xml.onlineUrl(a[:url]) if a[:url]
xml.electronicMailAddress(a[:email])
end
end
@data[:metadata_providers].each_with_index do |a, i|
xml.metadataProvider do
xml.individualName do
xml.givenName(a[:first_name])
xml.surName(a[:last_name])
end
xml.organizationName(a[:organization]) if a[:organization]
xml.positionName(a[:position]) if a[:position]
xml.onlineUrl(a[:url]) if a[:url]
xml.electronicMailAddress(a[:email])
end
end if @data[:metadata_providers]
xml.pubDate(Time.now.to_s)
xml.abstract() do
xml.para(@data[:abstract])
end
contacts.each do |contact|
xml.contact { xml.references(contact) }
end
end
xml.additionalMetadata do
xml.metadata do
xml.citation(@data[:citation])
xml.resourceLogoUrl(@data[:logo_url]) if @data[:logo_url]
end
end
:'xsi:schemaLocation' => 'eml_uri') do
build_dataset(xml)
build_additional_metadata(xml)
xml.parent.namespace = xml.parent.namespace_definitions.first
end
end
Expand All @@ -77,6 +35,85 @@ def create
end

private

def build_dataset(xml)
xml.dataset(id: @data[:id]) do
xml.title(@data[:title])
xml.license(@data[:license])
contacts = []
build_authors(xml, contacts)
build_metadata_providers(xml)
xml.pubDate(Time.now.to_s)
build_abstract(xml)
build_contacts(xml, contacts)
end
end

def build_abstract(xml)
xml.abstract() do
xml.para(@data[:abstract])
end
end

def build_contacts(xml, contacts)
contacts.each do |contact|
xml.contact { xml.references(contact) }
end
end

def build_metadata_providers(xml)
@data[:metadata_providers].each_with_index do |a, i|
xml.metadataProvider do
build_person(xml, a)
end
end if @data[:metadata_providers]
end

def build_authors(xml, contacts)
@data[:authors].each_with_index do |a, i|
creator_id = i + 1
contacts << creator_id
xml.creator(id: creator_id, scope: 'document') do
build_person(xml, a)
end
end
end

def build_additional_metadata(xml)
xml.additionalMetadata do
xml.metadata do
xml.citation(@data[:citation])
xml.resourceLogoUrl(@data[:logo_url]) if @data[:logo_url]
end
end
end

def build_header(xml)
xml.eml(packageId: "%s/%s" % [@data[:id], timestamp],
system: @data[:system] || 'http://globalnames.org',
:'xml:lang' => 'en',
:'xmlns:eml' => 'eml://ecoinformatics.org/eml-2.1.1',
:'xmlns:md' => 'eml://ecoinformatics.org/methods-2.1.1',
:'xmlns:proj' => 'eml://ecoinformatics.org/project-2.1.1',
:'xmlns:d' => 'eml://ecoinformatics.org/dataset-2.1.1',
:'xmlns:res' => 'eml://ecoinformatics.org/resource-2.1.1',
:'xmlns:dc' => 'http://purl.org/dc/terms/',
:'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
:'xsi:schemaLocation' => 'eml_uri')
end

def build_person(xml, data)
a = data
xml.individualName do
xml.givenName(a[:first_name])
xml.surName(a[:last_name])
end
xml.organizationName(a[:organization]) if a[:organization]
xml.positionName(a[:position]) if a[:position]
xml.onlineUrl(a[:url]) if a[:url]
xml.electronicMailAddress(a[:email])
end

def timestamp
t = Time.now.getutc.to_a[0..5].reverse
t[0..2].join('-') + '::' + t[-3..-1].join(':')
Expand Down
47 changes: 47 additions & 0 deletions spec/files/generator_eml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0"?>
<eml:eml xmlns:eml="eml://ecoinformatics.org/eml-2.1.1" xmlns:md="eml://ecoinformatics.org/methods-2.1.1" xmlns:proj="eml://ecoinformatics.org/project-2.1.1" xmlns:d="eml://ecoinformatics.org/dataset-2.1.1" xmlns:res="eml://ecoinformatics.org/resource-2.1.1" xmlns:dc="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" packageId="1234/2013-12-30::19:45:33" system="http://globalnames.org" xml:lang="en" xsi:schemaLocation="eml_uri">
<dataset id="1234">
<title>Test Classification</title>
<license>http://creativecommons.org/licenses/by-sa/3.0/</license>
<creator id="1" scope="document">
<individualName>
<givenName>John</givenName>
<surName>Doe</surName>
</individualName>
<organizationName>Example</organizationName>
<positionName>Assistant Professor</positionName>
<onlineUrl>http://example.org</onlineUrl>
<electronicMailAddress>jdoe@example.com</electronicMailAddress>
</creator>
<creator id="2" scope="document">
<individualName>
<givenName>Jane</givenName>
<surName>Doe</surName>
</individualName>
<electronicMailAddress>jane@example.com</electronicMailAddress>
</creator>
<metadataProvider>
<individualName>
<givenName>Jim</givenName>
<surName>Doe</surName>
</individualName>
<onlineUrl>http://aggregator.example.org</onlineUrl>
<electronicMailAddress>jimdoe@example.com</electronicMailAddress>
</metadataProvider>
<pubDate>2013-12-30 14:45:33 -0500</pubDate>
<abstract>
<para>test classification</para>
</abstract>
<contact>
<references>1</references>
</contact>
<contact>
<references>2</references>
</contact>
</dataset>
<additionalMetadata>
<metadata>
<citation>Test classification: Doe John, Doe Jane, Taxnonmy, 10, 1, 2010</citation>
</metadata>
</additionalMetadata>
</eml:eml>
19 changes: 19 additions & 0 deletions spec/files/generator_meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<archive xmlns="http://rs.tdwg.org/dwc/text/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://rs.tdwg.org/dwc/terms/xsd/archive/ http://darwincore.googlecode.com/svn/trunk/text/tdwg_dwc_text.xsd">
<core encoding="UTF-8" fieldsTerminatedBy="," fieldsEnclosedBy="&quot;" linesTerminatedBy="&#10;" rowType="http://rs.tdwg.org/dwc/terms/Taxon" ignoreHeaderLines="1">
<files>
<location>core.csv</location>
</files>
<id index="0"/>
<field term="http://rs.tdwg.org/dwc/terms/parentNameUsageID" index="1"/>
<field term="http://rs.tdwg.org/dwc/terms/scientificName" index="2"/>
<field term="http://rs.tdwg.org/dwc/terms/taxonRank" index="3"/>
</core>
<extension encoding="UTF-8" fieldsTerminatedBy="," fieldsEnclosedBy="&quot;" linesTerminatedBy="&#10;" rowType="http://rs.gbif.org/terms/1.0/VernacularName" ignoreHeaderLines="1">
<files>
<location>vern.csv</location>
</files>
<coreid index="0"/>
<field term="http://rs.tdwg.org/dwc/terms/vernacularName" index="1"/>
</extension>
</archive>
17 changes: 13 additions & 4 deletions spec/lib/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,25 @@
'http://rs.gbif.org/terms/1.0/VernacularName')

gen.add_meta_xml
meta = File.read(File.join(gen.path, 'meta.xml'))
expect(meta).to match %r|<location>core.csv</location>|
meta = File.read(File.join(gen.path, 'meta.xml')).strip
meta_from_file= File.read(File.expand_path(
'../../files/generator_meta.xml',
__FILE__)).strip
expect(meta).to eq meta_from_file
end
end

describe '#add_eml_data' do
it 'adds eml data' do
gen.add_eml_xml(EML_DATA)
eml = File.read(File.join(gen.path, 'eml.xml'))
expect(eml).to match /jdoe@example.com/
eml = File.read(File.join(gen.path, 'eml.xml')).strip
eml.gsub!(%r|(<pubDate>).*?(</pubDate>)|, '\12013-12-30 14:45:33 -0500\2')
eml.gsub!(/(packageId=").*?"/, '\11234/2013-12-30::19:45:33"')

eml_from_file = File.read(File.expand_path(
'../../files/generator_eml.xml',
__FILE__)).strip
expect(eml.strip).to eq eml_from_file.strip
end
end

Expand Down

0 comments on commit aa40735

Please sign in to comment.