-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #344 from datacite/feature_event_updat_job
updates registrant IDs for events that have not been updated.
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
class EventRegistrantUpdateByIdJob < ActiveJob::Base | ||
queue_as :lupo_background | ||
|
||
|
||
rescue_from ActiveJob::DeserializationError, Elasticsearch::Transport::Transport::Errors::BadRequest do |error| | ||
logger = Logger.new(STDOUT) | ||
logger.error error.message | ||
end | ||
|
||
def perform(id, options={}) | ||
logger = Logger.new(STDOUT) | ||
|
||
item = Event.where(uuid: id).first | ||
return false unless item.present? | ||
logger.info "djdjdj" | ||
logger.info id | ||
logger.info item.source_id | ||
|
||
|
||
case item.source_id | ||
when "datacite-crossref" | ||
registrant_id = get_crossref_member_id(item.obj_id) if get_doi_ra(item.obj_id) == "Crossref" | ||
logger.info registrant_id | ||
|
||
obj = item.obj.merge("registrant_id" => registrant_id) unless registrant_id.nil? | ||
logger.info obj | ||
item.update_attributes(obj: obj) if obj.present? | ||
when "crossref" | ||
registrant_id = get_crossref_member_id(item.subj) if get_doi_ra(item.subj) == "Crossref" | ||
logger.info registrant_id | ||
|
||
subj = item.subj.merge("registrant_id" => registrant_id) unless registrant_id.nil? | ||
logger.info subj | ||
item.update_attributes(subj: subj) if subj.present? | ||
end | ||
|
||
logger.error item.errors.full_messages.map { |message| { title: message } } if item.errors.any? | ||
logger.info "#{item.uuid} Updated" if item.errors.blank? && registrant_id | ||
end | ||
|
||
def get_crossref_member_id(id, options={}) | ||
logger = Logger.new(STDOUT) | ||
doi = doi_from_url(id) | ||
# return "crossref.citations" unless doi.present? | ||
|
||
url = "https://api.crossref.org/works/#{Addressable::URI.encode(doi)}[email protected]" | ||
sleep(0.01) # to avoid crossref rate limitting | ||
response = Maremma.get(url, host: true) | ||
logger.info "[Crossref Response] [#{response.status}] for DOI #{doi} metadata" | ||
return "" if response.status == 404 ### for cases when DOI is not in the crossreaf api | ||
return "crossref.citations" if response.status != 200 ### for cases any other errors | ||
|
||
message = response.body.dig("data", "message") | ||
|
||
"crossref.#{message["member"]}" | ||
end | ||
|
||
def get_doi_ra(doi) | ||
prefix = validate_prefix(doi) | ||
return nil if prefix.blank? | ||
|
||
url = "https://doi.org/ra/#{prefix}" | ||
result = Maremma.get(url) | ||
|
||
result.body.dig("data", 0, "RA") | ||
end | ||
|
||
def validate_prefix(doi) | ||
Array(/\A(?:(http|https):\/(\/)?(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}).*\z/.match(doi)).last | ||
end | ||
|
||
def doi_from_url(url) | ||
if /\A(?:(http|https):\/\/(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}\/.+)\z/.match(url) | ||
uri = Addressable::URI.parse(url) | ||
uri.path.gsub(/^\//, '').downcase | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class EventRegistrantUpdateJob < ActiveJob::Base | ||
queue_as :lupo_background | ||
|
||
def perform(ids, options={}) | ||
ids.each { |id| EventRegistrantUpdateByIdJob.perform_later(id, options) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'rails_helper' | ||
|
||
describe EventUpdateByIdJob, type: :job do | ||
let(:event) { create(:event) } | ||
subject(:job) { EventUpdateByIdJob.perform_later(event.uuid) } | ||
|
||
it 'queues the job' do | ||
expect { job }.to have_enqueued_job(EventUpdateByIdJob) | ||
.on_queue("test_lupo_background") | ||
end | ||
|
||
after do | ||
clear_enqueued_jobs | ||
clear_performed_jobs | ||
end | ||
end |