From cb9c3550943361ef7c657a36878979fe2a774eca Mon Sep 17 00:00:00 2001 From: Ben Pennell Date: Mon, 30 Oct 2023 11:49:08 -0400 Subject: [PATCH] Refactor export_as_oai_dc_xml into helper methods to reduce cognitive complexity --- .../document/dublin_core_override.rb | 82 +++++++++++-------- 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/app/overrides/models/concerns/blacklight/document/dublin_core_override.rb b/app/overrides/models/concerns/blacklight/document/dublin_core_override.rb index 56a77cfa6..602d4b956 100644 --- a/app/overrides/models/concerns/blacklight/document/dublin_core_override.rb +++ b/app/overrides/models/concerns/blacklight/document/dublin_core_override.rb @@ -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 @@ -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?