Skip to content

Commit

Permalink
minor tweaks to cff reader and writer. #4
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Dec 7, 2021
1 parent 6e56dd3 commit 92df578
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 37 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:
briard (2.2)
briard (2.2.1)
activesupport (>= 4.2.5)
benchmark_methods (~> 0.7)
bibtex-ruby (>= 5.1.0)
Expand Down
20 changes: 18 additions & 2 deletions lib/briard/readers/cff_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def read_cff(string: nil, **options)
sum
end

titles = meta.fetch("title", nil).present? ? [{ "title" => meta.fetch("title", nil) }] : []
titles = meta.fetch("title", nil).present? ? [{ "title" => meta.fetch("title", nil) }] : []
related_identifiers = Array.wrap(cff_references(meta.fetch("references", nil)))
rights_list = meta.fetch("license", nil).present? ? [hsh_to_spdx("rightsIdentifier" => meta.fetch("license"))] : nil

{ "id" => id,
Expand All @@ -60,6 +61,7 @@ def read_cff(string: nil, **options)
"titles" => titles,
"creators" => creators,
"publisher" => publisher,
"related_identifiers" => related_identifiers,
"dates" => dates,
"publication_year" => publication_year,
"descriptions" => meta.fetch("abstract", nil).present? ? [{ "description" => sanitize(meta.fetch("abstract")), "descriptionType" => "Abstract" }] : nil,
Expand All @@ -73,7 +75,7 @@ def read_cff(string: nil, **options)
def cff_creators(creators)
Array.wrap(creators).map do |a|
name_identifiers = normalize_orcid(parse_attributes(a["orcid"])).present? ? [{ "nameIdentifier" => normalize_orcid(parse_attributes(a["orcid"])), "nameIdentifierScheme" => "ORCID", "schemeUri"=>"https://orcid.org" }] : nil
if a["given-names"].present? || name_identifiers.present?
if a["given-names"].present? || a["family-names"].present? || name_identifiers.present?
given_name = parse_attributes(a["given-names"])
family_name = parse_attributes(a["family-names"])
affiliation = Array.wrap(a["affiliation"]).map do |a|
Expand Down Expand Up @@ -102,6 +104,20 @@ def cff_creators(creators)
end
end
end

def cff_references(references)
Array.wrap(references).map do |r|
identifier = Array.wrap(r["identifiers"]).find { |i| i["type"] == "doi" }

if identifier.present?
{ "relatedIdentifier" => normalize_id(parse_attributes(identifier["value"])),
"relationType" => "References",
"relatedIdentifierType" => "DOI" }.compact
else
nil
end
end.compact.unwrap
end
end
end
end
2 changes: 1 addition & 1 deletion lib/briard/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Briard
VERSION = "2.2"
VERSION = "2.2.1"
end
31 changes: 29 additions & 2 deletions lib/briard/writers/cff_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,47 @@ def cff

# only use CFF for software
return nil unless types["resourceTypeGeneral"] == "Software"

title = parse_attributes(titles, content: "title", first: true)

hsh = {
"cff-version" => "1.2.0",
"message" => "If you use #{title} in your work, please cite it using the following metadata",
"doi" => normalize_doi(doi),
"repository-code" => url,
"title" => parse_attributes(titles, content: "title", first: true),
"authors" => creators,
"authors" => write_cff_creators(creators),
"abstract" => parse_attributes(descriptions, content: "description", first: true),
"version" => version_info,
"keywords" => subjects.present? ? Array.wrap(subjects).map { |k| parse_attributes(k, content: "subject", first: true) } : nil,
"date-released" => get_date(dates, "Issued") || publication_year,
"license" => Array.wrap(rights_list).map { |l| l["rightsUri"] }.compact.unwrap,
"license" => Array.wrap(rights_list).map { |l| l["rightsIdentifier"] }.compact.unwrap,
"references" => write_references(related_identifiers)
}.compact
hsh.to_yaml
end

def write_cff_creators(creators)
Array.wrap(creators).map do |a|
if a["givenName"].present? || a["nameIdentifiers"].present?
{ "given-names" => a["givenName"],
"family-names" => a["familyName"],
"orcid" => parse_attributes(a["nameIdentifiers"], content: "nameIdentifier", first: true),
"affiliation" => parse_attributes(a["affiliation"], content: "name", first: true) }.compact
else
{ "name" => a["name"] }
end
end
end

def write_references(related_identifiers)
{ "identifiers" =>
Array.wrap(related_identifiers).map do |r|
{
"type" => r["relatedIdentifierType"].downcase,
"value" => r["relatedIdentifierType"] == "DOI" ? doi_from_url(r["relatedIdentifier"]) : r["relatedIdentifier"] }
end }
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions spec/readers/cff_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"rightsIdentifierScheme"=>"SPDX",
"rightsUri"=>"http://www.apache.org/licenses/LICENSE-2.0",
"schemeUri"=>"https://spdx.org/licenses/"}])
expect(subject.related_identifiers).to eq([{"relatedIdentifier"=>"https://doi.org/10.5281/zenodo.1003149", "relatedIdentifierType"=>"DOI", "relationType"=>"References"}])
end

it "cff-converter-python" do
Expand Down Expand Up @@ -101,6 +102,7 @@
"rightsIdentifierScheme"=>"SPDX",
"rightsUri"=>"http://www.apache.org/licenses/LICENSE-2.0",
"schemeUri"=>"https://spdx.org/licenses/"}])
expect(subject.related_identifiers).to eq([{"relatedIdentifier"=>"https://doi.org/10.5281/zenodo.1310751", "relatedIdentifierType"=>"DOI", "relationType"=>"References"}])
end

it "ruby-cff" do
Expand Down Expand Up @@ -131,6 +133,7 @@
"rightsIdentifierScheme"=>"SPDX",
"rightsUri"=>"http://www.apache.org/licenses/LICENSE-2.0",
"schemeUri"=>"https://spdx.org/licenses/"}])
expect(subject.related_identifiers).to eq([{"relatedIdentifier"=>"https://doi.org/10.5281/zenodo.1003149", "relatedIdentifierType"=>"DOI", "relationType"=>"References"}])
end

it "ruby-cff repository url" do
Expand Down Expand Up @@ -163,6 +166,7 @@
"rightsIdentifierScheme"=>"SPDX",
"rightsUri"=>"http://www.apache.org/licenses/LICENSE-2.0",
"schemeUri"=>"https://spdx.org/licenses/"}])
expect(subject.related_identifiers).to eq([{"relatedIdentifier"=>"https://doi.org/10.5281/zenodo.1003149", "relatedIdentifierType"=>"DOI", "relationType"=>"References"}])
end
end
end
75 changes: 44 additions & 31 deletions spec/writers/cff_writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@
expect(subject.valid?).to be true
json = Psych.safe_load(subject.cff, permitted_classes: [Date])
expect(json["doi"]).to eq("https://doi.org/10.5281/zenodo.10164")
expect(json["authors"]).to eq([{"affiliation"=>[{"name"=>"Juelich Supercomputing Centre, Jülich, Germany"}],
"familyName"=>"Klatt",
"givenName"=>"Torbjörn",
"name"=>"Klatt, Torbjörn"},
{"affiliation"=>[{"name"=>"Juelich Supercomputing Centre, Jülich, Germany"}],
"familyName"=>"Moser",
"givenName"=>"Dieter",
"name"=>"Moser, Dieter"},
{"affiliation"=>[{"name"=>"Juelich Supercomputing Centre, Jülich, Germany"}],
"familyName"=>"Speck",
"givenName"=>"Robert",
"name"=>"Speck, Robert"}])
expect(json["authors"]).to eq([{"affiliation"=>"Juelich Supercomputing Centre, Jülich, Germany",
"family-names"=>"Klatt",
"given-names"=>"Torbjörn"},
{"affiliation"=>"Juelich Supercomputing Centre, Jülich, Germany",
"family-names"=>"Moser",
"given-names"=>"Dieter"},
{"affiliation"=>"Juelich Supercomputing Centre, Jülich, Germany",
"family-names"=>"Speck",
"given-names"=>"Robert"}])
expect(json["title"]).to eq("Pypint -- Python Framework For Parallel-In-Time Methods")
expect(json["abstract"]).to eq("<em>PyPinT</em> is a framework for Parallel-in-Time integration routines. The main purpose of <em>PyPinT</em> is to provide a framework for educational use and prototyping new parallel-in-time algorithms. As well it will aid in developing a high-performance C++ implementation for massively parallel computers providing the benefits of parallel-in-time routines to a zoo of time integrators in various applications.")
expect(json["date-released"]).to eq("2014-05-27")
expect(json["repository-code"]).to eq("https://zenodo.org/record/10164")
expect(json["keywords"]).to eq(["Parallel-in-Time Integration", "Spectral Deferred Corrections", "Multigrid", "Multi-Level Spectral Deferred Corrections", "Python Framework"])
expect(json["license"]).to eq(["http://www.opensource.org/licenses/MIT", "info:eu-repo/semantics/openAccess"])
expect(json["license"]).to be_nil
expect(json["references"]).to eq("identifiers"=>[{"type"=>"url", "value"=>"https://github.com/Parallel-in-Time/PyPinT/tree/release-v0.0.4"}])
end

it "SoftwareSourceCode also Zenodo" do
Expand All @@ -37,29 +35,44 @@
json = Psych.safe_load(subject.cff, permitted_classes: [Date])
expect(json["doi"]).to eq("https://doi.org/10.5281/zenodo.15497")
expect(json["authors"]).to eq([{"affiliation"=>
[{"name"=>
"Instituut voor Kern- en Stralingsfysica, KU Leuven, 3001 Leuven, België"}],
"familyName"=>"Gins",
"givenName"=>"Wouter",
"name"=>"Gins, Wouter"},
{"affiliation"=>
[{"name"=>
"Instituut voor Kern- en Stralingsfysica, KU Leuven, 3001 Leuven, België"}],
"familyName"=>"de Groote",
"givenName"=>"Ruben",
"name"=>"de Groote, Ruben"},
{"affiliation"=>
[{"name"=>
"Instituut voor Kern- en Stralingsfysica, KU Leuven, 3001 Leuven, België"}],
"familyName"=>"Heylen",
"givenName"=>"Hanne",
"name"=>"Heylen, Hanne"}])
"Instituut voor Kern- en Stralingsfysica, KU Leuven, 3001 Leuven, België",
"family-names"=>"Gins",
"given-names"=>"Wouter"},
{"affiliation"=>
"Instituut voor Kern- en Stralingsfysica, KU Leuven, 3001 Leuven, België",
"family-names"=>"de Groote",
"given-names"=>"Ruben"},
{"affiliation"=>
"Instituut voor Kern- en Stralingsfysica, KU Leuven, 3001 Leuven, België",
"family-names"=>"Heylen",
"given-names"=>"Hanne"}])
expect(json["title"]).to eq("Satlas: Simulation And Analysis Toolbox For Laser Spectroscopy And Nmr Experiments")
expect(json["abstract"]).to eq("Initial release of the satlas Python package for the analysis and simulation for laser spectroscopy experiments. For the documentation, see http://woutergins.github.io/satlas/")
expect(json["date-released"]).to eq("2015-02-18")
expect(json["repository-code"]).to eq("https://zenodo.org/record/15497")
expect(json["keywords"]).to be_nil
expect(json["license"]).to eq(["http://www.opensource.org/licenses/MIT", "info:eu-repo/semantics/openAccess"])
expect(json["license"]).to be_nil
expect(json["references"]).to eq("identifiers"=>[{"type"=>"url", "value"=>"https://github.com/woutergins/satlas/tree/v1.0.0"}])
end

it "ruby-cff" do
input = "https://github.com/citation-file-format/ruby-cff"
subject = Briard::Metadata.new(input: input)
expect(subject.valid?).to be true
json = Psych.safe_load(subject.cff, permitted_classes: [Date])
expect(json["doi"]).to eq("https://doi.org/10.5281/zenodo.1184077")
expect(json["authors"]).to eq([{"affiliation"=>"The University of Manchester, UK",
"family-names"=>"Haines",
"given-names"=>"Robert",
"orcid"=>"https://orcid.org/0000-0002-9538-7919"},
{"name"=>"The Ruby Citation File Format Developers"}])
expect(json["title"]).to eq("Ruby CFF Library")
expect(json["abstract"]).to eq("This library provides a Ruby interface to manipulate Citation File Format files")
expect(json["date-released"]).to eq("2021-08-18")
expect(json["repository-code"]).to eq("https://github.com/citation-file-format/ruby-cff")
expect(json["keywords"]).to eq(["ruby", "credit", "software citation", "research software", "software sustainability", "metadata", "citation file format", "CFF"])
expect(json["license"]).to eq("apache-2.0")
expect(json["references"]).to eq("identifiers"=>[{"type"=>"doi", "value"=>"10.5281/zenodo.1003149"}])
end
end
end

0 comments on commit 92df578

Please sign in to comment.