Skip to content

Commit

Permalink
fix schema_org type
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jul 22, 2020
1 parent a97649a commit c80f779
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Doi < ActiveRecord::Base
before_validation :update_language, if: :language?
before_validation :update_rights_list, if: :rights_list?
before_validation :update_identifiers
before_validation :update_types
before_save :set_defaults, :save_metadata
before_create { self.created = Time.zone.now.utc.iso8601 }

Expand Down Expand Up @@ -1963,6 +1964,20 @@ def update_identifiers
self.identifiers = Array.wrap(identifiers).select { |i| i["identifierType"] != "DOI" }
end

def update_types
return nil unless types.is_a?(Hash)

res = types["resourceType"].to_s.underscore.camelcase
resgen = types["resourceTypeGeneral"].to_s.dasherize
schema_org = Bolognese::Utils::CR_TO_SO_TRANSLATIONS[res] || Bolognese::Utils::DC_TO_SO_TRANSLATIONS[resgen] || "CreativeWork"

self.types = types.reverse_merge(
"schemaOrg" => schema_org,
"citeproc" => Bolognese::Utils::CR_TO_CP_TRANSLATIONS[res] || Bolognese::Utils::SO_TO_CP_TRANSLATIONS[schema_org] || "article",
"bibtex" => Bolognese::Utils::CR_TO_BIB_TRANSLATIONS[res] || Bolognese::Utils::SO_TO_BIB_TRANSLATIONS[schema_org] || "misc",
"ris" => Bolognese::Utils::CR_TO_RIS_TRANSLATIONS[res] || Bolognese::Utils::DC_TO_RIS_TRANSLATIONS[resgen] || "GEN").compact
end

def self.repair_landing_page(id: nil)
if id.blank?
Rails.logger.error "[Error] No id provided."
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ namespace :doi do
Doi.loop_through_dois(options)
end

desc "Set types"
task set_types: :environment do
options = {
query: "types.resourceTypeGeneral:* AND -types.schemaOrg:*",
label: "[SetTypes]",
job_name: "UpdateDoiJob",
}
Doi.loop_through_dois(options)
end

desc 'Convert affiliations to new format'
task :convert_affiliations => :environment do
from_id = (ENV['FROM_ID'] || Doi.minimum(:id)).to_i
Expand Down
38 changes: 38 additions & 0 deletions spec/models/doi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,44 @@
end
end

describe "types" do
let(:doi) { build(:doi) }

it "string" do
doi.types = "Dataset"
expect(doi.save).to be false
expect(doi.errors.details).to eq(:types=>[{:error=>"Types 'Dataset' should be an object instead of a string."}])
end

it "only resource_type_general" do
doi.types = { "resourceTypeGeneral" => "Dataset" }
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
expect(doi.types).to eq("bibtex"=>"misc", "citeproc"=>"dataset", "resourceTypeGeneral"=>"Dataset", "ris"=>"DATA", "schemaOrg"=>"Dataset")
end

it "resource_type and resource_type_general" do
doi.types = { "resourceTypeGeneral" => "Dataset", "resourceType" => "EEG data"}
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
expect(doi.types).to eq("bibtex"=>"misc", "citeproc"=>"dataset", "resourceTypeGeneral"=>"Dataset", "resourceType" => "EEG data", "ris"=>"DATA", "schemaOrg"=>"Dataset")
end

it "resource_type_general and different schema_org" do
doi.types = { "resourceTypeGeneral" => "Dataset", "schemaOrg" => "JournalArticle" }
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
expect(doi.types).to eq("bibtex"=>"misc", "citeproc"=>"dataset", "resourceTypeGeneral"=>"Dataset", "ris"=>"DATA", "schemaOrg"=>"JournalArticle")
end

it "resource_type_general and different ris" do
doi.types = { "resourceTypeGeneral" => "Dataset", "ris" => "GEN" }
expect(doi.save).to be true
expect(doi.errors.details).to be_empty
expect(doi.types).to eq("bibtex"=>"misc", "citeproc"=>"dataset", "resourceTypeGeneral"=>"Dataset", "ris"=>"GEN", "schemaOrg"=>"Dataset")
end
end

# describe "related_identifiers" do
# it "has part" do
# subject = build(:doi, related_identifiers: [ {
Expand Down

0 comments on commit c80f779

Please sign in to comment.