class Stash::Merritt::SubmissionPackage::StashWrapperBuilder

Attributes

dcs_resource[R]
embargo_end_date[R]
uploads[R]
version_number[R]

Public Class Methods

new(dcs_resource:, version_number:, uploads:, embargo_end_date:) click to toggle source
Calls superclass method
# File lib/stash/merritt/submission_package/stash_wrapper_builder.rb, line 14
def initialize(dcs_resource:, version_number:, uploads:, embargo_end_date:)
  super(file_name: 'stash-wrapper.xml')
  @dcs_resource = dcs_resource
  @version_number = version_number
  @uploads = uploads
  @embargo_end_date = date_or_nil(embargo_end_date)
end

Public Instance Methods

contents() click to toggle source
# File lib/stash/merritt/submission_package/stash_wrapper_builder.rb, line 22
def contents # rubocop:disable Metrics/AbcSize
  StashWrapper.new(
    identifier: to_sw_identifier(dcs_resource.identifier),
    version: Version.new(number: version_number, date: Date.today),
    license: to_sw_license(dcs_resource.rights_list),
    inventory: to_sw_inventory(uploads),
    descriptive_elements: [dcs_resource.save_to_xml],
    embargo: to_sw_embargo(embargo_end_date)
  ).write_xml
end

Private Instance Methods

date_or_nil(embargo_end_date) click to toggle source
# File lib/stash/merritt/submission_package/stash_wrapper_builder.rb, line 35
def date_or_nil(embargo_end_date)
  return unless embargo_end_date
  return embargo_end_date if embargo_end_date.is_a?(Date)
  return embargo_end_date.utc.to_date if embargo_end_date.respond_to?(:utc)
  raise ArgumentError, "Specified end date #{embargo_end_date} does not appear to be a date or time"
end
default_start_date(embargo_end_date) click to toggle source
# File lib/stash/merritt/submission_package/stash_wrapper_builder.rb, line 48
def default_start_date(embargo_end_date)
  start_date = Date.today
  return start_date if start_date < embargo_end_date
  embargo_end_date
end
to_stash_file(upload) click to toggle source
# File lib/stash/merritt/submission_package/stash_wrapper_builder.rb, line 71
def to_stash_file(upload)
  StashFile.new(
    pathname: upload.upload_file_name,
    size_bytes: upload.upload_file_size,
    mime_type: upload.upload_content_type
  )
end
to_sw_embargo(embargo_end_date) click to toggle source
# File lib/stash/merritt/submission_package/stash_wrapper_builder.rb, line 42
def to_sw_embargo(embargo_end_date)
  return unless embargo_end_date
  start_date = default_start_date(embargo_end_date)
  Embargo.new(type: EmbargoType::DOWNLOAD, period: EmbargoType::NONE.value, start_date: start_date, end_date: embargo_end_date)
end
to_sw_identifier(dcs_identifier) click to toggle source
# File lib/stash/merritt/submission_package/stash_wrapper_builder.rb, line 54
def to_sw_identifier(dcs_identifier)
  return unless dcs_identifier
  raise "Invalid identifier type; expected DOI, was #{dcs_identifier.identifier_type}" unless dcs_identifier.identifier_type == 'DOI'
  Identifier.new(type: IdentifierType::DOI, value: dcs_identifier.value)
end
to_sw_inventory(uploads) click to toggle source
# File lib/stash/merritt/submission_package/stash_wrapper_builder.rb, line 66
def to_sw_inventory(uploads)
  return unless uploads
  Inventory.new(files: uploads.map { |upload| to_stash_file(upload) })
end
to_sw_license(dcs_rights_list) click to toggle source
# File lib/stash/merritt/submission_package/stash_wrapper_builder.rb, line 60
def to_sw_license(dcs_rights_list)
  return unless dcs_rights_list && !dcs_rights_list.empty?
  dcs_rights = dcs_rights_list[0]
  License.new(name: dcs_rights.value, uri: dcs_rights.uri)
end