Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not send events to the event busy unless datacite_crossref #191

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions app/models/related_identifier.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class RelatedIdentifier < Base
LICENSE = "https://creativecommons.org/publicdomain/zero/1.0/".freeze
DATACITE_CROSSREF = "datacite_crossref"

include Helpable
include Cacheable
Expand Down Expand Up @@ -58,23 +59,24 @@ def push_data(result, _options = {})
def self.push_item(item)
attributes = item.fetch("attributes", {})
doi = attributes.fetch("doi", nil)

return nil unless doi.present? && cached_doi_ra(doi) == "DataCite"

pid = normalize_doi(doi)
related_doi_identifiers = Array.wrap(attributes.fetch("relatedIdentifiers",
nil)).select do |r|

related_doi_identifiers = Array.wrap(attributes.fetch("relatedIdentifiers", nil)).select do |r|
r["relatedIdentifierType"] == "DOI"
end

registration_agencies = {}

push_items = Array.wrap(related_doi_identifiers).reduce([]) do |ssum, iitem|
related_identifier = iitem.fetch("relatedIdentifier",
nil).to_s.strip.downcase
related_identifier = iitem.fetch("relatedIdentifier", nil).to_s.strip.downcase
obj_id = normalize_doi(related_identifier)
prefix = validate_prefix(related_identifier)

unless registration_agencies[prefix]
registration_agencies[prefix] =
cached_doi_ra(related_identifier)
registration_agencies[prefix] = cached_doi_ra(related_identifier)
end

if registration_agencies[prefix].nil?
Expand All @@ -87,7 +89,7 @@ def self.push_item(item)
source_token = ENV["DATACITE_RELATED_SOURCE_TOKEN"]
obj = cached_datacite_response(obj_id)
elsif registration_agencies[prefix] == "Crossref"
source_id = "datacite_crossref"
source_id = DATACITE_CROSSREF
source_token = ENV["DATACITE_CROSSREF_SOURCE_TOKEN"]
obj = cached_crossref_response(obj_id)
elsif registration_agencies[prefix].present?
Expand All @@ -112,7 +114,6 @@ def self.push_item(item)
"subj" => subj,
"obj" => obj }
end

ssum
end

Expand Down Expand Up @@ -158,7 +159,8 @@ def self.push_item(item)
end

# send to Event Data Bus
if ENV["EVENTDATA_TOKEN"].present?
# we only send datacite_crossref events to the bus
if ENV["EVENTDATA_TOKEN"].present? && iiitem['source_id'] == DATACITE_CROSSREF
iiitem = set_event_for_bus(iiitem)

host = ENV["EVENTDATA_URL"]
Expand Down
29 changes: 23 additions & 6 deletions spec/models/related_identifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,28 @@
allow(Rails.logger).to receive(:info)

expect(RelatedIdentifier.push_item(item)).to eq(1)
expect(Maremma).to have_received(:post).twice
expect(Maremma).to have_received(:post).with("https://fake.lagattino.com/events", anything).once

expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related pushed to Event Data service.")
end

it "does push the event to the event data bus when source_id is datacite_crossref" do
related_identifier = RelatedIdentifier.new
allow(ENV).to receive(:[]).with("DATACITE_CROSSREF_SOURCE_TOKEN").and_return("fake-token")
allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return("normalized_doi")
allow(related_identifier).to receive(:normalize_doi).with(valid_related_identifier).and_return("normalized_related_identifier")
allow(related_identifier).to receive(:validate_prefix).with(valid_related_identifier).and_return("datacite")
allow(RelatedIdentifier).to receive(:cached_doi_ra).with("https://doi.org/10.5678/related").and_return("Crossref")
allow(RelatedIdentifier).to receive(:cached_doi_ra).with("https://doi.org/10.1234/example").and_return("DataCite")
allow(RelatedIdentifier).to receive(:cached_crossref_response).and_return({})
allow(RelatedIdentifier).to receive(:cached_datacite_response).and_return({})
allow(related_identifier).to receive(:set_event_for_bus).and_return({})
allow(Rails.logger).to receive(:info)

expect(RelatedIdentifier.push_item(item)).to eq(1)
expect(Maremma).to have_received(:post).with("https://fake.lagattino.com/events", anything).once
expect(Maremma).to have_received(:post).with("https://fake.eventdataurl.com/events", anything).once
expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related pushed to Event Data service.")
expect(Rails.logger).to have_received(:info).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related pushed to Event Data service.")
end
end
Expand Down Expand Up @@ -135,10 +154,9 @@
allow(Rails.logger).to receive(:info)

expect(RelatedIdentifier.push_item(item)).to eq(1)
expect(Maremma).to have_received(:post).twice
expect(Maremma).to have_received(:post).with("https://fake.lagattino.com/events", anything).once

expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related already pushed to Event Data service.")
expect(Rails.logger).to have_received(:info).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related already pushed to Event Data service.")
end
end

Expand Down Expand Up @@ -169,10 +187,9 @@
allow(Rails.logger).to receive(:error)

expect(RelatedIdentifier.push_item(item)).to eq(1)
expect(Maremma).to have_received(:post).twice
expect(Maremma).to have_received(:post).with("https://fake.lagattino.com/events", anything).once

expect(Rails.logger).to have_received(:error).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related had an error: An error occurred during the put request.")
expect(Rails.logger).to have_received(:error).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related had an error:")
end

it "Does not sent an event to Event Data Bus." do
Expand All @@ -187,7 +204,7 @@
allow(Rails.logger).to receive(:info)

expect(RelatedIdentifier.push_item(item)).to eq(1)
expect(Maremma).to have_received(:post).once
expect(Maremma).to have_received(:post).with("https://fake.lagattino.com/events", anything).once

expect(Rails.logger).to have_received(:info).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related was not sent to Event Data Bus.")
end
Expand Down
Loading