Skip to content

Commit

Permalink
fix tests for datacite schema 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Jan 26, 2024
1 parent c9984a8 commit b94736a
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
commonmeta-ruby (3.10.0)
commonmeta-ruby (3.11.0)
activesupport (>= 4.2.5, < 8.0)
addressable (~> 2.8.1, < 2.8.2)
base32-url (>= 0.7.0, < 1)
Expand Down
14 changes: 9 additions & 5 deletions lib/commonmeta/author_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,22 @@ def get_one_author(author)
id = parse_attributes(author.fetch("id", nil), first: true) ||
parse_attributes(author.fetch("identifier", nil), first: true) ||
parse_attributes(author.fetch("sameAs", nil), first: true)

id = normalize_orcid(id) || normalize_ror(id) if id.present?

# DataCite metadata
if id.nil? && author["nameIdentifiers"].present?
id = Array.wrap(author.dig("nameIdentifiers")).find do |ni|
ni["nameIdentifierScheme"] == "ORCID"
normalize_name_identifier(ni).present?
end
id = id["nameIdentifier"] if id.present?
# Crossref metadata
id = normalize_name_identifier(id) if id.present?
# Crossref metadata
elsif id.nil? && author["ORCID"].present?
id = author.fetch("ORCID")
id = normalize_orcid(id)
# JSON Feed metadata
elsif id.nil? && author["url"].present?
id = author.fetch("url")
end
id = normalize_orcid(id) || normalize_ror(id)

# parse author type, i.e. "Person", "Organization" or not specified
type = author.fetch("type", nil)
Expand Down
2 changes: 1 addition & 1 deletion lib/commonmeta/readers/commonmeta_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def read_commonmeta(string: nil, **options)
read_options = ActiveSupport::HashWithIndifferentAccess.new(options.except(:doi, :id, :url,
:sandbox, :validate, :ra))
meta = string.present? ? JSON.parse(string) : {}
meta["schema_version"] = "https://commonmeta.org/commonmeta_v0.10.5.json"
meta["schema_version"] = "https://commonmeta.org/commonmeta_v0.10"
meta.compact.merge(read_options)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/commonmeta/schema_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module Commonmeta
module SchemaUtils
COMMONMETA = File.read(File.expand_path("../../resources/commonmeta_v0.10.5.json",
COMMONMETA = File.read(File.expand_path("../../resources/commonmeta_v0.10.6.json",
__dir__))

def json_schema_errors
Expand Down
37 changes: 37 additions & 0 deletions lib/commonmeta/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,43 @@ def normalize_ror(ror)
"https://ror.org/" + Addressable::URI.encode(ror)
end

def normalize_name_identifier(hsh)
return nil unless hsh.present? && hsh.is_a?(Hash)

name_identifier = hsh["nameIdentifier"]
name_identifier_scheme = hsh["nameIdentifierScheme"]
scheme_uri = hsh["schemeURI"] || hsh["schemeUri"]
return nil unless name_identifier.present?

if name_identifier_scheme == "ORCID" || scheme_uri == "https://orcid.org"
return normalize_orcid(name_identifier)
elsif name_identifier_scheme == "ROR" || scheme_uri == "https://ror.org"
return normalize_ror(name_identifier)
elsif name_identifier_scheme == "ISNI" || scheme_uri == "https://isni.org"
return normalize_isni(name_identifier)
elsif validate_url(name_identifier) == "URL"
return name_identifier
elsif scheme_uri.present?
return scheme_uri + Addressable::URI.encode(name_identifier)
end

return nil
end

def validate_isni(isni)
isni = Array(%r{\A(?:(?:http|https)://)?(isni\.org/isni/)?(\d{4}[[:space:]-]\d{4}[[:space:]-]\d{4}[[:space:]-]\d{3}[0-9X]+)\z}.match(isni)).last
isni.gsub(/[[:space:]]/, "-") if isni.present?
end

def normalize_isni(isni)
# TODO fix validation
# isni = validate_isni(isni)
return nil unless isni.present?

# turn ISNI ID into URL
"https://isni.org/isni/" + Addressable::URI.encode(isni)
end

# pick electronic issn if there are multiple
# format issn as xxxx-xxxx
def normalize_issn(input, options = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/commonmeta/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Commonmeta
VERSION = '3.10.0'
VERSION = '3.11.0'
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://commonmeta.org/commonmeta_v0.10.5.json",
"title": "Commonmeta v0.10.5",
"$id": "https://commonmeta.org/commonmeta_v0.10.6.json",
"title": "Commonmeta v0.10.6",
"description": "JSON representation of the Commonmeta schema.",
"additionalProperties": false,
"definitions": {
Expand Down Expand Up @@ -125,6 +125,7 @@
"ReportSeries",
"Report",
"Software",
"StudyRegistration",
"Other"
]
},
Expand Down Expand Up @@ -508,7 +509,7 @@
"description": "The schema version of the resource.",
"type": "string",
"enum": [
"https://commonmeta.org/commonmeta_v0.10.5.json",
"https://commonmeta.org/commonmeta_v0.10",
"http://datacite.org/schema/kernel-3",
"http://datacite.org/schema/kernel-4"
]
Expand All @@ -523,7 +524,14 @@
"type": "array",
"items": {
"type": "string",
"enum": ["CLOCKSS", "LOCKSS", "Portico", "KB", "Internet Archive", "DWT"]
"enum": [
"CLOCKSS",
"LOCKSS",
"Portico",
"KB",
"Internet Archive",
"DWT"
]
},
"uniqueItems": true
}
Expand Down
32 changes: 16 additions & 16 deletions resources/datacite-v4.json → resources/datacite-v45.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"id": "datacite-v4.json",
"title": "DataCite v4",
"description": "JSON representation of the DataCite v4 schema.",
"id": "datacite-v45.json",
"title": "DataCite v4.5",
"description": "JSON representation of the DataCite v4.5 schema.",
"additionalProperties": true,
"definitions": {
"nameType": {
Expand Down Expand Up @@ -283,21 +283,21 @@
"type": ["object", "string"],
"anyOf": [
{
"type": "object",
"properties": {
"name": { "type": "string" },
"publisherIdentifier": { "type": "string", "format": "uri" },
"publisherIdentifierScheme": { "type": "string" },
"schemeUri": { "type": "string", "format": "uri" },
"lang": { "type": "string" }
},
"required": ["name"]
},
"type": "object",
"properties": {
"name": { "type": "string" },
"publisherIdentifier": { "type": "string", "format": "uri" },
"publisherIdentifierScheme": { "type": "string" },
"schemeUri": { "type": "string", "format": "uri" },
"lang": { "type": "string" }
},
"required": ["name"]
},
{
"type": "string",
"minLength": 1
"type": "string",
"minLength": 1
}
]
]
},
"publication_year": {
"type": "integer"
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/commonmeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,6 @@
],
"subjects": [],
"provider": "Crossref",
"schema_version": "https://commonmeta.org/commonmeta_v0.10.5.json",
"schema_version": "https://commonmeta.org/commonmeta_v0.10",
"state": "findable"
}
2 changes: 1 addition & 1 deletion spec/readers/commonmeta_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
context 'read commonmeta metadata' do
it "default" do
expect(subject.valid?).to be true
expect(subject.schema_version).to eq("https://commonmeta.org/commonmeta_v0.10.5.json")
expect(subject.schema_version).to eq("https://commonmeta.org/commonmeta_v0.10")
expect(subject.id).to eq("https://doi.org/10.7554/elife.01567")
expect(subject.type).to eq("JournalArticle")
expect(subject.url).to eq("https://elifesciences.org/articles/01567")
Expand Down
2 changes: 1 addition & 1 deletion spec/readers/datacite_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
expect(subject.id).to eq("https://doi.org/10.82433/08qf-ee96")
expect(subject.type).to eq("Instrument")
expect(subject.contributors.length).to eq(2)
expect(subject.contributors.first).to eq("contributorRoles" => ["Author"], "name" => "DECTRIS", "type" => "Organization")
expect(subject.contributors.first).to eq("contributorRoles" => ["Author"], "name" => "DECTRIS", "type" => "Organization", "id" => "https://www.wikidata.org/wiki/Q107529885")
expect(subject.date).to eq("created" => "2022-10-20", "published" => "2022", "registered" => "2022-10-20", "updated" => "2024-01-02")
expect(subject.publisher).to eq("name" => "Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences")
expect(subject.license).to be_nil
Expand Down
34 changes: 30 additions & 4 deletions spec/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@
links = [{ "rel" => "self", "type" => "application/atom+xml", "href" => "https://syldavia-gazette.org/atom/" },
{ "rel" => "alternate", "type" => "text/html", "href" => "https://syldavia-gazette.org" },
{ "rel" => "license", "type" => "text/html", "href" => "https://creativecommons.org/licenses/by/4.0/legalcode" }]

it "url" do
response = subject.get_link(links, "self")
expect(response).to eq("https://syldavia-gazette.org/atom/")
Expand Down Expand Up @@ -721,7 +721,7 @@
it "decode doi to uuid" do
doi = "https://doi.org/10.53731/6315bn4-aqg82ja-4a9wxdt-29f7279"
response = subject.decode_doi(doi, uuid: true)
expect(response).to eq('255d48ab-c102-9288-a4f3-add092f388e9')
expect(response).to eq("255d48ab-c102-9288-a4f3-add092f388e9")
end
end

Expand All @@ -745,10 +745,36 @@
end
end

context 'json_feed_unregistered_url' do
it 'all posts' do
context "json_feed_unregistered_url" do
it "all posts" do
response = subject.json_feed_unregistered_url
expect(response).to eq("https://api.rogue-scholar.org/posts/unregistered")
end
end

context "normalize_name_identifier" do
it "ORCID" do
hsh = {"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-1419-2405", "nameIdentifierScheme"=>"ORCID"}
response = subject.normalize_name_identifier(hsh)
expect(response).to eq("https://orcid.org/0000-0003-1419-2405")
end

it "ROR" do
hsh = { "schemeUri" => "https://ror.org", "nameIdentifier" => "https://ror.org/02aj13c28", "nameIdentifierScheme" => "ROR" }
response = subject.normalize_name_identifier(hsh)
expect(response).to eq("https://ror.org/02aj13c28")
end

it "ISNI" do
hsh = { "schemeUri" => "http://isni.org/isni/", "nameIdentifier" => "0000000134596520", "nameIdentifierScheme" => "ISNI" }
response = subject.normalize_name_identifier(hsh)
expect(response).to eq("https://isni.org/isni/0000000134596520")
end

it "Wikidata" do
hsh = {"schemeUri"=>"https://www.wikidata.org/wiki/", "nameIdentifier"=>"Q107529885", "nameIdentifierScheme"=>"Wikidata"}
response = subject.normalize_name_identifier(hsh)
expect(response).to eq("https://www.wikidata.org/wiki/Q107529885")
end
end
end

0 comments on commit b94736a

Please sign in to comment.