Class: Assembly::ContentMetadata::NokogiriBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/assembly-objectfile/content_metadata/nokogiri_builder.rb

Overview

Builds a nokogiri representation of the content metadata

Class Method Summary collapse

Class Method Details

.build(filesets:, druid:, common_path:, config:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/assembly-objectfile/content_metadata/nokogiri_builder.rb', line 11

def self.build(filesets:, druid:, common_path:, config:)
  # a counter to use when creating auto-labels for resources, with incremenets for each type
  resource_type_counters = Hash.new(0)
  pid = druid.gsub('druid:', '') # remove druid prefix when creating IDs

  Nokogiri::XML::Builder.new do |xml|
    xml.(objectId: druid.to_s, type: config.type) do
      filesets.each_with_index do |fileset, index| # iterate over all the resources
        # start a new resource element
        sequence = index + 1

        resource_type_counters[fileset.resource_type_description] += 1 # each resource type description gets its own incrementing counter

        xml.resource(id: "#{pid}_#{sequence}", sequence: sequence, type: fileset.resource_type_description) do
          # create a generic resource label if needed
          default_label = config.auto_labels ? "#{fileset.resource_type_description.capitalize} #{resource_type_counters[fileset.resource_type_description]}" : ''

          # but if one of the files has a label, use it instead
          resource_label = fileset.label_from_file(default: default_label)

          xml.label(resource_label) unless resource_label.empty?
          fileset.files.each do |obj| # iterate over all the files in a resource
            xml_file_params = { id: obj.file_id(common_path: common_path, flatten_folder_structure: config.flatten_folder_structure) }
            xml_file_params.merge!(obj.file_attributes(config.file_attributes)) if config.add_file_attributes
            xml_file_params.merge!(mimetype: obj.mimetype, size: obj.filesize) if config.add_exif

            xml.file(xml_file_params) do
              if config.add_exif # add exif info if the user requested it
                xml.checksum(obj.sha1, type: 'sha1')
                xml.checksum(obj.md5, type: 'md5')
                xml.imageData(obj.image_data) if obj.image? # add image data for an image
              elsif obj.provider_md5 || obj.provider_sha1 # if we did not add exif info, see if there are user supplied checksums to add
                xml.checksum(obj.provider_sha1, type: 'sha1') if obj.provider_sha1
                xml.checksum(obj.provider_md5, type: 'md5') if obj.provider_md5
              end
            end
          end
        end
      end
    end
  end
end