Class: Toccatore::DataciteRelated

Inherits:
Base
  • Object
show all
Defined in:
lib/toccatore/datacite_related.rb

Constant Summary collapse

LICENSE =
"https://creativecommons.org/publicdomain/zero/1.0/"

Constants inherited from Base

Base::ICON_URL

Instance Method Summary collapse

Methods inherited from Base

#cleanup_author, #get_authors, #get_data, #get_doi_ra, #get_hashed_authors, #get_name_identifier, #get_one_author, #get_one_hashed_author, #get_query_url, #get_total, #is_personal_name?, #job_batch_size, #name_detector, #normalize_doi, #orcid_as_url, #orcid_from_url, #process_data, #push_data, #queue_jobs, #send_notification_to_slack, #timeout, #unfreeze, #url, #validate_doi, #validate_orcid, #validate_prefix

Instance Method Details

#parse_data(result, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/toccatore/datacite_related.rb', line 15

def parse_data(result, options={})
  return result.body.fetch("errors") if result.body.fetch("errors", nil).present?

  items = result.body.fetch("data", {}).fetch('response', {}).fetch('docs', nil)
  registration_agencies = {}

  Array.wrap(items).reduce([]) do |sum, item|
    doi = item.fetch("doi")
    pid = normalize_doi(doi)
    related_doi_identifiers = item.fetch('relatedIdentifier', []).select { |id| id =~ /:DOI:.+/ }

    # don't generate event if there is a DOI for identical content with same prefix
    skip_doi = related_doi_identifiers.any? do |related_identifier|
      ["IsIdenticalTo"].include?(related_identifier.split(':', 3).first) &&
      related_identifier.split(':', 3).last.to_s.starts_with?(validate_prefix(doi))
    end

    unless skip_doi
      sum += Array(related_doi_identifiers).reduce([]) do |ssum, iitem|
        raw_relation_type, _related_identifier_type, related_identifier = iitem.split(':', 3)
        related_identifier = related_identifier.strip.downcase
        prefix = validate_prefix(related_identifier)
        registration_agencies[prefix] = get_doi_ra(prefix) unless registration_agencies[prefix]

        # check whether this is a DataCite DOI
        if %w(Crossref).include?(registration_agencies[prefix])
          ssum << { "id" => SecureRandom.uuid,
                    "message_action" => "create",
                    "subj_id" => pid,
                    "obj_id" => normalize_doi(related_identifier),
                    "relation_type_id" => raw_relation_type.underscore,
                    "source_id" => "datacite",
                    "source_token" => options[:source_token],
                    "occurred_at" => item.fetch("minted"),
                    "license" => LICENSE }
        else
          ssum
        end
      end
    end

    sum
  end
end

#push_item(item, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/toccatore/datacite_related.rb', line 60

def push_item(item, options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Access token missing." }] }) if options[:access_token].blank?

  host = options[:push_url].presence || "https://bus.eventdata.crossref.org"
  push_url = host + "/events"

  if options[:jsonapi]
    data = { "data" => {
               "id" => item["id"],
               "type" => "events",
               "attributes" => {
                 "message-action" => item["message_action"],
                 "subj-id" => item["subj_id"],
                 "obj-id" => item["obj_id"],
                 "relation-type-id" => item["relation_type_id"],
                 "source-id" => "datacite-crossref",
                 "source-token" => item["source_token"],
                 "occurred-at" => item["occurred_at"],
                 "license" => item["license"] } }}

    response = Maremma.post(push_url, data: data.to_json,
                                      bearer: options[:access_token],
                                      content_type: 'json',
                                      host: host)
  else
    response = Maremma.post(push_url, data: item.to_json,
                                      bearer: options[:access_token],
                                      content_type: 'json',
                                      host: host)
  end

  # return 0 if successful, 1 if error
  if response.status == 201
    puts "#{item['subj_id']} #{item['relation_type_id']} #{item['obj_id']} pushed to Event Data service."
    0
  elsif response.body["errors"].present?
    puts "#{item['subj_id']} #{item['relation_type_id']} #{item['obj_id']} had an error:"
    puts "#{response.body['errors'].first['title']}"
    1
  end
end

#queryObject



11
12
13
# File 'lib/toccatore/datacite_related.rb', line 11

def query
  "relatedIdentifier:DOI\\:*"
end

#source_idObject



7
8
9
# File 'lib/toccatore/datacite_related.rb', line 7

def source_id
  "datacite_related"
end