Class: Uc3DmpId::Augmenter

Inherits:
Object
  • Object
show all
Defined in:
lib/uc3-dmp-id/augmenter.rb

Overview

Class that adds items to the :dmphub_modifications array or directly to the :dmpraodmap_related_identifiers array if the confidence level was ‘Absolute’

Constant Summary collapse

MSG_MISSING_ANNOTATIONS =
'DMP must have its DMPHub specific annotations!'
MSG_MISSING_AUGMENTER =
'No Augmenter specified!'
MSG_MISSING_DMP =
'No DMP or the DMP did not contain enough information to use.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Augmenter

rubocop:disable Metrics/AbcSize

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/uc3-dmp-id/augmenter.rb', line 21

def initialize(**args)
  @logger = args[:logger]
  @augmenter = args[:augmenter]
  @run_id = args.fetch(:run_id, 'None')
  raise AugmenterError, MSG_MISSING_AUGMENTER unless @augmenter.is_a?(Hash) && !@augmenter['PK'].nil?

  @dmp = args.fetch(:dmp, {})['dmp'].nil? ? args[:dmp] : args.fetch(:dmp, {})['dmp']
  raise AugmenterError, MSG_MISSING_DMP if @dmp.nil? || @dmp['dmp_id'].nil?
  raise AugmenterError, MSG_MISSING_ANNOTATIONS if @dmp['PK'].nil?

  _extract_known
end

Instance Attribute Details

#augmenterObject

Returns the value of attribute augmenter.



14
15
16
# File 'lib/uc3-dmp-id/augmenter.rb', line 14

def augmenter
  @augmenter
end

#dmpObject

Returns the value of attribute dmp.



14
15
16
# File 'lib/uc3-dmp-id/augmenter.rb', line 14

def dmp
  @dmp
end

#known_awardsObject

Returns the value of attribute known_awards.



14
15
16
# File 'lib/uc3-dmp-id/augmenter.rb', line 14

def known_awards
  @known_awards
end

#known_modsObject

Returns the value of attribute known_mods.



14
15
16
# File 'lib/uc3-dmp-id/augmenter.rb', line 14

def known_mods
  @known_mods
end

#known_worksObject

Returns the value of attribute known_works.



14
15
16
# File 'lib/uc3-dmp-id/augmenter.rb', line 14

def known_works
  @known_works
end

#loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/uc3-dmp-id/augmenter.rb', line 14

def logger
  @logger
end

#run_idObject

Returns the value of attribute run_id.



14
15
16
# File 'lib/uc3-dmp-id/augmenter.rb', line 14

def run_id
  @run_id
end

Instance Method Details

#add_modifications(works:) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/uc3-dmp-id/augmenter.rb', line 36

def add_modifications(works:)
  mod_hash = _generate_mod_header

  %w[publications datasets softwares].each do |work_type|
    works.fetch(work_type, []).each do |work|
      next if @known_works.include?(work['id'])

      work_hash = _work_to_mod_entry(type: work_type[0..work_type.length - 2], work:)
      mod_hash.fetch('dmproadmap_related_identifiers', []) << work_hash unless work_hash.nil?
      fundings = work.fetch('fundingReferences', [])
      next unless fundings.any?

      award_hash = fundings.map { |funding| _funding_to_mod_entry(work:, funding:) }
      mod_hash.fetch('funding', []) << award_hash unless award_hash.nil?
    end
  end
  return 0 unless mod_hash['dmproadmap_related_identifiers'].any? || mod_hash.fetch('funding', []).any?

  # A single work can have multiple fundingReferences, so flatten the array
  mod_hash['funding'] = mod_hash.fetch('funding', []).flatten.compact.uniq

  # Save the DMP
  @logger.debug(message: 'New mods:', details: mod_hash)
  @dmp['dmphub_modifications'] = (@known_mods.nil? ? [] : @known_mods) << mod_hash
  client = Uc3DmpDynamo::Client.new
  resp = client.put_item(json: @dmp, logger:)
  raise AugmenterError, Helper::MSG_DMP_NO_DMP_ID if resp.nil?

  # Return the number of modifications added to the DMP
  mod_hash.fetch('dmproadmap_related_identifiers', []).length + mod_hash.fetch('funding', []).length
end