Skip to content

Commit

Permalink
import metadata for one doi
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Dec 15, 2018
1 parent 3fc7a9a commit 46a4581
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,22 @@ def self.find_by_id(id, options={})
})
end

def self.import_one(doi: nil)
doi = Doi.where(doi: doi).first
return nil unless doi.present?

string = doi.current_metadata.present? ? doi.current_metadata.xml : nil
meta = doi.read_datacite(string: string, sandbox: doi.sandbox)
attrs = %w(creators contributors titles publisher publication_year types descriptions container sizes formats language dates identifiers related_identifiers funding_references geo_locations rights_list subjects content_url).map do |a|
[a.to_sym, meta[a]]
end.to_h.merge(schema_version: meta["schema_version"] || "http://datacite.org/schema/kernel-4", version_info: meta["version"], xml: string)

doi.update_attributes(attrs)
logger.info "[MySQL] Imported metadata for DOI " + doi.doi + "."
rescue TypeError, NoMethodError => error
logger.error "[MySQL] Error importing metadata for " + doi.doi + ": " + error.message
end

def self.import_all(options={})
from_date = options[:from_date].present? ? Date.parse(options[:from_date]) : Date.current
until_date = options[:until_date].present? ? Date.parse(options[:until_date]) : Date.current
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ namespace :doi do
puts "DOIs created on #{from_date} imported."
end

desc 'Import one DOI'
task :import_one => :environment do
if ENV['DOI'].nil?
puts "ENV['DOI'] is required"
exit
end

Doi.import_one(doi: ENV['DOI'])
end

desc 'Index all DOIs'
task :index => :environment do
if ENV['YEAR'].present?
Expand Down

0 comments on commit 46a4581

Please sign in to comment.