Class: PULDatacite

Inherits:
Object
  • Object
show all
Defined in:
app/services/pul_datacite.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(work) ⇒ PULDatacite

Returns a new instance of PULDatacite.



14
15
16
17
18
19
20
# File 'app/services/pul_datacite.rb', line 14

def initialize(work)
  @datacite_connection = Datacite::Client.new(username: Rails.configuration.datacite.user,
                                              password: Rails.configuration.datacite.password,
                                              host: Rails.configuration.datacite.host)
  @work = work
  @metadata = work.
end

Instance Attribute Details

#datacite_connectionObject (readonly)

Returns the value of attribute datacite_connection.



12
13
14
# File 'app/services/pul_datacite.rb', line 12

def datacite_connection
  @datacite_connection
end

#metadataObject (readonly)

Returns the value of attribute metadata.



12
13
14
# File 'app/services/pul_datacite.rb', line 12

def 
  @metadata
end

#workObject (readonly)

Returns the value of attribute work.



12
13
14
# File 'app/services/pul_datacite.rb', line 12

def work
  @work
end

Class Method Details

.publish_test_doi?Boolean

Determines whether or not a test DOI should be referenced (this avoids requests to the DOI API endpoint for non-production deployments)

Returns:

  • (Boolean)


7
8
9
# File 'app/services/pul_datacite.rb', line 7

def publish_test_doi?
  (Rails.env.development? || Rails.env.test?) && Rails.configuration.datacite.user.blank?
end

Instance Method Details

#curator_or_current_uid(user) ⇒ Object



60
61
62
63
64
65
66
67
# File 'app/services/pul_datacite.rb', line 60

def curator_or_current_uid(user)
  persisted = if work.curator.nil?
                user
              else
                work.curator
              end
  persisted.uid
end

#doi_attribute_urlObject

This is the url that should be used for ARK and DOI redirection. It will search the index for the DOI and redirect the use appropriately.



56
57
58
# File 'app/services/pul_datacite.rb', line 56

def doi_attribute_url
  "https://datacommons.princeton.edu/discovery/doi/#{work.doi}"
end

#draft_doiObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/pul_datacite.rb', line 22

def draft_doi
  if PULDatacite.publish_test_doi?
    Rails.logger.info "Using hard-coded test DOI during development."
    "10.34770/tbd"
  else
    result = datacite_connection.autogenerate_doi(prefix: Rails.configuration.datacite.prefix)
    if result.success?
      result.success.doi
    else
      raise("Error generating DOI. #{result.failure.status} / #{result.failure.reason_phrase}")
    end
  end
end

#publish_doi(user) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/pul_datacite.rb', line 36

def publish_doi(user)
  return Rails.logger.info("Publishing hard-coded test DOI during development.") if PULDatacite.publish_test_doi?

  if work.doi&.starts_with?(Rails.configuration.datacite.prefix)
    result = datacite_connection.update(id: work.doi, attributes: doi_attributes)
    if result.failure?
      resolved_user = curator_or_current_uid(user)
      message = "@#{resolved_user} Error publishing DOI. #{result.failure.status} / #{result.failure.reason_phrase}"
      WorkActivity.add_work_activity(work.id, message, user.id, activity_type: WorkActivity::DATACITE_ERROR)
    end
  elsif work.ark.blank? # we can not update the url anywhere
    Honeybadger.notify("Publishing for a DOI we do not own and no ARK is present: #{work.doi}")
  end
rescue Faraday::ConnectionFailed
  sleep 1
  retry
end