Module: Commonmeta::Writers::DataciteWriter

Included in:
MetadataUtils
Defined in:
lib/commonmeta/writers/datacite_writer.rb

Instance Method Summary collapse

Instance Method Details

#dataciteObject



6
7
8
9
10
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/commonmeta/writers/datacite_writer.rb', line 6

def datacite
  resource_type_general = Commonmeta::Utils::CM_TO_DC_TRANSLATIONS.fetch(type, 'Other')
  types = { 'resourceTypeGeneral' => resource_type_general,
            'resourceType' => type == resource_type_general ? nil : type,
            'bibtex' => Commonmeta::Utils::CM_TO_BIB_TRANSLATIONS.fetch(type, 'misc'),
            'citeproc' => Commonmeta::Utils::CM_TO_CSL_TRANSLATIONS.fetch(type, 'article'),
            'ris' => Commonmeta::Utils::CM_TO_RIS_TRANSLATIONS.fetch(type, 'GEN'),
            'schemaOrg' => Commonmeta::Utils::CM_TO_SO_TRANSLATIONS.fetch(type,
                                                                          'CreativeWork') }.compact

  dates = get_dates_from_date(date)
  rights_list = spdx_to_hsh(license)
  related_identifiers = Array.wrap(references).map { |r| datacite_reference(r) }

  hsh = {
    'id' => id,
    'doi' => doi_from_url(id),
    'url' => url,
    'types' => types,
    'creators' => Array.wrap(creators).map { |c| datacite_contributor(c) },
    'titles' => titles,
    'publisher' => publisher.to_h['name'],
    'container' => container,
    'subjects' => subjects,
    'contributors' => Array.wrap(contributors).map { |c| datacite_contributor(c) },
    'dates' => dates,
    'language' => language,
    'alternate_identifiers' => alternate_identifiers,
    'sizes' => sizes,
    'formats' => formats,
    'version' => version,
    'rights_list' => rights_list,
    'descriptions' => descriptions,
    'geo_locations' => geo_locations,
    'funding_references' => funding_references,
    'related_identifiers' => related_identifiers,
    'schema_version' => schema_version,
    'provider_id' => provider_id,
    'client_id' => client_id,
    'agency' => provider,
    'state' => state
  }.compact

  JSON.pretty_generate hsh.transform_keys! { |key|
    key.camelcase(uppercase_first_letter = :lower)
  }
end

#datacite_contributor(contributor) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/commonmeta/writers/datacite_writer.rb', line 54

def datacite_contributor(contributor)
  type = contributor.fetch('type', nil)
  type += 'al' if type.present?

  if type == 'Personal'
    contributor['name'] = [contributor['familyName'], contributor['givenName']].join(', ')
  end
  contributor['nameIdentifiers'] = author_name_identifiers(contributor['id'])

  { 'name' => contributor.fetch('name', nil),
    'nameType' => type,
    'givenName' => contributor.fetch('givenName', nil),
    'familyName' => contributor.fetch('familyName', nil),
    'nameIdentifiers' => contributor.fetch('nameIdentifiers', nil).presence,
    'affiliation' => contributor.fetch('affiliation', nil).presence,
    'contributorType' => contributor.fetch('contributorType', nil) }.compact
end

#datacite_reference(reference) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/commonmeta/writers/datacite_writer.rb', line 72

def datacite_reference(reference)
  return nil unless reference.present? || !reference.is_a?(Hash)

  related_identifier = reference['doi'] || reference['url']

  return nil unless related_identifier.present?

  related_identifier_type = reference['doi'] ? 'DOI' : 'URL'

  { 'relatedIdentifier' => related_identifier,
    'relatedIdentifierType' => related_identifier_type,
    'relationType' => 'References' }.compact
end