Class: PDCSerialization::Datacite

Inherits:
Object
  • Object
show all
Defined in:
app/models/pdc_serialization/datacite.rb

Overview

The common use for this class is:

work = Work.find(123)
datacite_xml = PDCSerialization::Datacite.new_from_work(work).to_xml

For testing purposes you can also quickly get an XML serialization with the helper:

datacite_xml = PDCSerialization::Datacite.skeleton_datacite_xml(...)

You can also pass a PDCMetadata::Resource which is useful to test with a more complex dataset without saving the work to the database:

jsonb_hash = JSON.parse({...}.to_json)
resource = PDCMetadata::Resource.new_from_jsonb(jsonb_hash)
datacite_xml = PDCSerialization::Datacite.new_from_work_resource(resource).to_xml

For information

Datacite schema: https://support.datacite.org/docs/datacite-metadata-schema-v44-properties-overview
Datacite mapping gem: https://github.com/CDLUC3/datacite-mapping

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping) ⇒ Datacite

Returns a new instance of Datacite.

[View source]

27
28
29
30
# File 'app/models/pdc_serialization/datacite.rb', line 27

def initialize(mapping)
  @mapping = mapping
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.


25
26
27
# File 'app/models/pdc_serialization/datacite.rb', line 25

def errors
  @errors
end

#mappingObject (readonly)

Returns the value of attribute mapping.


25
26
27
# File 'app/models/pdc_serialization/datacite.rb', line 25

def mapping
  @mapping
end

Class Method Details

.datacite_contributor_type(key) ⇒ Object

[View source]

113
114
115
# File 'app/models/pdc_serialization/datacite.rb', line 113

def datacite_contributor_type(key)
  ::Datacite::Mapping::ContributorType.find_by_key(key.upcase.to_sym)
end

.datacite_resource_type(resource_type_general, value) ⇒ Object

[View source]

108
109
110
111
# File 'app/models/pdc_serialization/datacite.rb', line 108

def datacite_resource_type(resource_type_general, value)
  resource_type = ::Datacite::Mapping::ResourceTypeGeneral.find_by_value(resource_type_general)
  ::Datacite::Mapping::ResourceType.new(resource_type_general: resource_type, value:)
end

.new_from_work(work) ⇒ Object

Creates a PDCSerialization::Datacite object from a Work

[View source]

80
81
82
# File 'app/models/pdc_serialization/datacite.rb', line 80

def self.new_from_work(work)
  new_from_work_resource(work.resource)
end

.new_from_work_resource(resource) ⇒ Object

Creates a PDCSerialization::Datacite object from a PDCMetadata::Resource rubocop:disable Metrics/MethodLength

[View source]

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/pdc_serialization/datacite.rb', line 87

def self.new_from_work_resource(resource)
  mapping = ::Datacite::Mapping::Resource.new(
    identifier: ::Datacite::Mapping::Identifier.new(value: resource.doi),
    creators: creators_from_work_resource(resource.creators),
    contributors: individual_contributors_from_work_resource(resource.individual_contributors) +
      organizational_contributors_from_work_resource(resource.organizational_contributors),
    descriptions: descriptions_from_work_resource(resource.description),
    titles: titles_from_work_resource(resource.titles),
    publisher: ::Datacite::Mapping::Publisher.new(value: resource.publisher),
    publication_year: resource.publication_year,
    resource_type: datacite_resource_type(resource.resource_type_general, resource.resource_type),
    related_identifiers: related_identifiers_from_work_resource(resource),
    rights_list: rights_from_work_resource(resource),
    version: resource.version_number,
    funding_references: funding_references_from_work_resource(resource)
  )
  Datacite.new(mapping)
end

.skeleton_datacite_xml(identifier:, title:, creator:, publisher:, publication_year:, resource_type:, resource_type_general:) ⇒ Object

Returns the XML serialization for a valid Datacite skeleton record based on a few required values. Useful for early in the workflow when we don’t have much data yet and for testing.

Parameters:

  • identifier, (String)

    e.g “10.1234/567”

  • title (String)
  • creator (String)
  • publisher (String)
  • publication_year (String)
  • resource_type (String)
[View source]

66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/pdc_serialization/datacite.rb', line 66

def self.skeleton_datacite_xml(identifier:, title:, creator:, publisher:, publication_year:, resource_type:, resource_type_general:)
  mapping = ::Datacite::Mapping::Resource.new(
    identifier: ::Datacite::Mapping::Identifier.new(value: identifier),
    creators: [] << ::Datacite::Mapping::Creator.new(name: creator),
    titles: [] << ::Datacite::Mapping::Title.new(value: title),
    publisher: ::Datacite::Mapping::Publisher.new(value: publisher),
    publication_year:,
    resource_type: datacite_resource_type(resource_type_general, resource_type)
  )
  mapping.write_xml
end

Instance Method Details

#to_xmlObject

Returns the XML serialization for the Datacite record Note that the actual XML serialization is done by the datacite-mapping gem.

[View source]

34
35
36
# File 'app/models/pdc_serialization/datacite.rb', line 34

def to_xml
  @mapping.write_xml
end

#valid?Boolean

Validate this DataCite XML serialization against the official DataCite schema By default we validate against DataCite 4.4. This will shift over time as new versions of the datacite schema are released.

Returns:

  • (Boolean)
[View source]

42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/pdc_serialization/datacite.rb', line 42

def valid?
  @errors = []
  datacite_xml = Nokogiri::XML(to_xml)
  schema_location = Rails.root.join("config", "schema")
  Dir.chdir(schema_location) do
    xsd = Nokogiri::XML::Schema(File.read("datacite_4_4.xsd"))
    xsd.validate(datacite_xml).each do |error|
      @errors << error
    end
  end
  return true if @errors.empty?
  false
end