Skip to content

Commit

Permalink
Refactor export_as_oai_dc_xml into helper methods to reduce cognitive…
Browse files Browse the repository at this point in the history
… complexity
  • Loading branch information
bbpennel committed Oct 30, 2023
1 parent 3af56e7 commit cb9c355
Showing 1 changed file with 48 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,15 @@ def export_as_oai_dc_xml
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation' => %(http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd)) do
to_semantic_values.select { |field, _values| dublin_core_field_name? field }.each do |field, values|
source = []
# sort people by index value
array_of_values = []
if Array.wrap(values).first.match('index:')
array_of_values = sort_people_by_index(values)
array_of_values.map! { |value| value.gsub(/\Aindex:\d*\|\|/, '') }
field_s = field.to_s
if field_s == 'thumbnail'
add_thumbnail_to_xml(xml, values)
else
array_of_values = Array.wrap(values)
end
array_of_values.each do |v|
if field.to_s == 'creator'
xml.tag! "dc:#{field}", v.to_s.split('||').first
elsif field.to_s == 'contributor'
xml.tag! "dc:#{field}", v.to_s.split('||').first
# display journal values as comma separated string (journal values come from single-valued fields)
elsif field.to_s == 'source'
source << v.to_s
elsif field.to_s == 'thumbnail'
if doi.blank?
record_url = URI.join(ENV['HYRAX_HOST'], "concern/#{first('has_model_ssim').tableize}/#{id}").to_s
xml.tag! 'dc:identifier', record_url
end
thumb_download = (values.first)
xml.tag! 'dc:identifier', "#{ENV['HYRAX_HOST']}#{thumb_download}"
file_download = thumb_download.split('?').first
xml.tag! 'dc:identifier', "#{ENV['HYRAX_HOST']}#{file_download}"
array_of_values = values_as_array(values)
if field_s == 'source'
add_source_to_xml(xml, array_of_values)
else
xml.tag! "dc:#{field}", v
end
end
unless source.blank?
# Based on tests in blacklight, the journal information should always be returned in the order listed in app/models/solr_document.rb
if source.count == 3
xml.tag! 'dc:source', "#{source[0]}, #{source[1]}(#{source[2]})"
else
xml.tag! 'dc:source', source.join(', ')
array_of_values.each { |v| add_field_to_xml(xml, field_s, v) }
end
end
end
Expand All @@ -63,6 +36,47 @@ def export_as_oai_dc_xml
xml.target!
end

def values_as_array(values)
# sort people by index value
if Array.wrap(values).first.match('index:')
array_of_values = sort_people_by_index(values)
array_of_values.map! { |value| value.gsub(/\Aindex:\d*\|\|/, '') }
else
array_of_values = Array.wrap(values)
end
end

def add_field_to_xml(xml, field, v)
if field == 'creator' || field == 'contributor'
xml.tag! "dc:#{field}", v.to_s.split('||').first
else
xml.tag! "dc:#{field}", v
end
end

def add_thumbnail_to_xml(xml, values)
if doi.blank?
record_url = URI.join(ENV['HYRAX_HOST'], "concern/#{first('has_model_ssim').tableize}/#{id}").to_s
xml.tag! 'dc:identifier', record_url
end
thumb_download = values.first
xml.tag! 'dc:identifier', "#{ENV['HYRAX_HOST']}#{thumb_download}"
file_download = thumb_download.split('?').first
xml.tag! 'dc:identifier', "#{ENV['HYRAX_HOST']}#{file_download}"
end

def add_source_to_xml(xml, array_of_values)
return unless array_of_values.blank?
source = array_of_values.map { |v| v.to_s }

# Based on tests in blacklight, the journal information should always be returned in the order listed in app/models/solr_document.rb
if source.count == 3
xml.tag! 'dc:source', "#{source[0]}, #{source[1]}(#{source[2]})"
else
xml.tag! 'dc:source', source.join(', ')
end
end

# [hyc-override] Used by ruby-oai gem to determine if a status=deleted header should be added.
# See OAI::Provider::Response::RecordResponse
def deleted?
Expand Down

0 comments on commit cb9c355

Please sign in to comment.