Skip to content
Snippets Groups Projects
Commit 10d4fe12 authored by Anusha Ranganathan's avatar Anusha Ranganathan
Browse files

Merge branch 'refector-doi-registration' into 'develop'

Refector DOI registration

See merge request FDM/rdm-system/rdms!117
parents f8ee078b ae887f40
No related branches found
Tags v0.3.4
1 merge request!117Refector DOI registration
Pipeline #6139 failed
......@@ -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
......
# 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment