Class: Dmp::DmpIdHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/dmp/dmp_id_handler.rb

Overview

Methods that handle PK generation

Constant Summary collapse

DOI_REGEX =
%r{[0-9]{2}\.[0-9]{5}/[a-zA-Z0-9/_.]+}.freeze

Class Method Summary collapse

Class Method Details

.dmp_id_base_urlObject



12
13
14
# File 'lib/dmp/dmp_id_handler.rb', line 12

def dmp_id_base_url
  ENV['DMP_ID_BASE_URL']&.end_with?('/') ? ENV['DMP_ID_BASE_URL'] : "#{ENV['DMP_ID_BASE_URL']}/"
end

.dmp_id_to_pk(json:) ⇒ Object

Append the :PK prefix to the :dmp_id



29
30
31
32
33
34
35
36
37
# File 'lib/dmp/dmp_id_handler.rb', line 29

def dmp_id_to_pk(json:)
  return nil if json.nil? || json['identifier'].nil?

  # If it's a DOI format it correctly
  dmp_id = format_dmp_id(value: json['identifier'].to_s)
  return nil if dmp_id.nil? || dmp_id == ''

  Dmp::MetadataHandler.append_pk_prefix(dmp: dmp_id)
end

.format_dmp_id(value:) ⇒ Object

Format the DMP ID in the way we want it



17
18
19
20
21
22
23
24
25
26
# File 'lib/dmp/dmp_id_handler.rb', line 17

def format_dmp_id(value:)
  dmp_id = value.match(DOI_REGEX).to_s
  return nil if dmp_id.nil? || dmp_id == ''
  # If it's already a URL, return it as is
  return value if value.start_with?('http')

  dmp_id = dmp_id.gsub('doi:', '')
  dmp_id = dmp_id.start_with?('/') ? dmp_id[1..dmp_id.length] : dmp_id
  "#{dmp_id_base_url}#{dmp_id}"
end

.pk_to_dmp_id(p_key:) ⇒ Object

Derive the DMP ID by removing the :PK prefix



40
41
42
43
44
# File 'lib/dmp/dmp_id_handler.rb', line 40

def pk_to_dmp_id(p_key:)
  return nil if p_key.nil?

  { type: 'doi', identifier: Dmp::MetadataHandler.remove_pk_prefix(dmp: p_key) }
end