diff --git a/hyrax/app/services/hyrax/workflow/register_doi.rb b/hyrax/app/services/hyrax/workflow/register_doi.rb index bdef119a74a4f698c728cc9989ca022f3e17be01..ffb030c6cbedd21982444c9881d0b630a8f702f7 100644 --- a/hyrax/app/services/hyrax/workflow/register_doi.rb +++ b/hyrax/app/services/hyrax/workflow/register_doi.rb @@ -11,61 +11,9 @@ module Hyrax # Genrate DOI for dataset module RegisterDoi def self.call(target:, **) - return true if ENV.fetch('REGISTER_DOI', 'true') == 'false' + RegisterDoiWorker.perform_async(target.id, target.class.name) if ENV.fetch('REGISTER_DOI', 'true') == 'true' - @dataset = target - - url = URI(ENV['DOI_URL']) - - http = Net::HTTP.new(url.host, url.port) - http.use_ssl = true - creators_hash = @dataset.complex_person.map do |person| - { name: "#{person.last_name.first},#{person.first_name.first}" } - end.compact - - request = Net::HTTP::Post.new(url) - request['content-type'] = 'application/vnd.api+json' - request.basic_auth ENV['DOI_USERNAME'], ENV['DOI_PASSWORD'] - - body = { - "data": { - "id": "#{ENV['DOI_PREFIX']}/#{@dataset.id}", - "type": 'dois', - "attributes": { - "doi": "#{ENV['DOI_PREFIX']}/#{@dataset.id}", - "creators": creators_hash, - "titles": [{ title: @dataset.title&.first }], - "publisher": @dataset.publisher&.first || 'Ruhr-University Bochum', - "publicationYear": Date.today.year, - "types": { - "resourceTypeGeneral": @dataset.resource_type&.first - } - } - } - } - request.body = body.to_json - response = http.request(request) - - return unless response.is_a?(Net::HTTPSuccess) - - parsed_response = JSON.parse(response.body) - - attributes = parsed_response['data']['attributes'] - - return unless attributes.present? - - complex_identifier = ComplexIdentifier.new(RDF::Node.new, ActiveTriples::Resource.new) - complex_date = ComplexDate.new(RDF::Node.new, ActiveTriples::Resource.new) - - complex_identifier.identifier = attributes['doi'] - complex_identifier.scheme = 'DOI' - - complex_date.date = attributes['created'].to_date.strftime('%d/%m/%Y') - complex_date.description = 'Published' - - @dataset.update(complex_date: [complex_date], complex_identifier: [complex_identifier]) - - @dataset.save + return true end end end diff --git a/hyrax/app/workers/register_doi_worker.rb b/hyrax/app/workers/register_doi_worker.rb new file mode 100644 index 0000000000000000000000000000000000000000..c15e767fd5ee93cbb40026984935f1f9c8c3fb35 --- /dev/null +++ b/hyrax/app/workers/register_doi_worker.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true +class RegisterDoiWorker + include Sidekiq::Worker + sidekiq_options retry: 0 + + def perform(work_id, work_type) + + @dataset = work_type.constantize.find(work_id) + + url = URI(ENV['DOI_URL']) + + http = Net::HTTP.new(url.host, url.port) + http.use_ssl = true + creators_hash = @dataset.complex_person.map do |person| + { name: "#{person.last_name.first},#{person.first_name.first}" } + end.compact + + request = Net::HTTP::Post.new(url) + request['content-type'] = 'application/vnd.api+json' + request.basic_auth ENV['DOI_USERNAME'], ENV['DOI_PASSWORD'] + + body = { + "data": { + "id": "#{ENV['DOI_PREFIX']}/#{@dataset.id}", + "type": 'dois', + "attributes": { + "doi": "#{ENV['DOI_PREFIX']}/#{@dataset.id}", + "creators": creators_hash, + "titles": [{ title: @dataset.title&.first }], + "publisher": @dataset.publisher&.first || 'Ruhr-University Bochum', + "publicationYear": Date.today.year, + "types": { + "resourceTypeGeneral": @dataset.resource_type&.first + } + } + } + } + request.body = body.to_json + response = http.request(request) + + raise Net::HTTPFatalError.new response.message, response unless response.is_a?(Net::HTTPSuccess) + + parsed_response = JSON.parse(response.body) + + attributes = parsed_response['data']['attributes'] + + raise Net::HTTPFatalError.new "No Attributes Found", response unless attributes.present? + + complex_identifier = ComplexIdentifier.new(RDF::Node.new, ActiveTriples::Resource.new) + complex_date = ComplexDate.new(RDF::Node.new, ActiveTriples::Resource.new) + + complex_identifier.identifier = attributes['doi'] + complex_identifier.scheme = 'DOI' + + complex_date.date = attributes['created'].to_date.strftime('%d/%m/%Y') + complex_date.description = 'Published' + + @dataset.update(complex_date: [complex_date], complex_identifier: [complex_identifier]) + + @dataset.save + end +end \ No newline at end of file