• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / 9091a1ae-29be-458c-984a-339d213919c4

12 Dec 2024 07:41PM UTC coverage: 26.434% (-69.7%) from 96.113%
9091a1ae-29be-458c-984a-339d213919c4

Pull #2000

circleci

jrgriffiniii
Removing integration with ActiveStorage
Pull Request #2000: Bump actionpack from 7.2.1.1 to 7.2.2.1

945 of 3575 relevant lines covered (26.43%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.55
/app/services/pul_datacite.rb
1
# frozen_string_literal: true
2
class PULDatacite
1✔
3
  class << self
1✔
4
    # Determines whether or not a test DOI should be referenced
5
    # (this avoids requests to the DOI API endpoint for non-production deployments)
6
    # @return [Boolean]
7
    def publish_test_doi?
1✔
8
      (Rails.env.development? || Rails.env.test?) && Rails.configuration.datacite.user.blank?
×
9
    end
10
  end
11

12
  attr_reader :datacite_connection, :work, :metadata
1✔
13

14
  def initialize(work)
1✔
15
    @datacite_connection = Datacite::Client.new(username: Rails.configuration.datacite.user,
×
16
                                                password: Rails.configuration.datacite.password,
17
                                                host: Rails.configuration.datacite.host)
18
    @work = work
×
19
    @metadata = work.metadata
×
20
  end
21

22
  def draft_doi
1✔
23
    if PULDatacite.publish_test_doi?
×
24
      Rails.logger.info "Using hard-coded test DOI during development."
×
25
      "10.34770/tbd"
×
26
    else
27
      result = datacite_connection.autogenerate_doi(prefix: Rails.configuration.datacite.prefix)
×
28
      if result.success?
×
29
        result.success.doi
×
30
      else
31
        raise("Error generating DOI. #{result.failure.status} / #{result.failure.reason_phrase}")
×
32
      end
33
    end
34
  end
35

36
  def publish_doi(user)
1✔
37
    return Rails.logger.info("Publishing hard-coded test DOI during development.") if PULDatacite.publish_test_doi?
×
38

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

54
  # This is the url that should be used for ARK and DOI redirection. It will search the
55
  # index for the DOI and redirect the use appropriately.
56
  def doi_attribute_url
1✔
57
    "https://datacommons.princeton.edu/discovery/doi/#{work.doi}"
×
58
  end
59

60
  def curator_or_current_uid(user)
1✔
61
    persisted = if work.curator.nil?
×
62
                  user
×
63
                else
64
                  work.curator
×
65
                end
66
    persisted.uid
×
67
  end
68

69
  private
1✔
70

71
    def doi_attribute_resource
1✔
72
      PDCMetadata::Resource.new_from_jsonb(metadata)
×
73
    end
74

75
    def doi_attribute_xml
1✔
76
      unencoded = doi_attribute_resource.to_xml
×
77
      Base64.encode64(unencoded)
×
78
    end
79

80
    def doi_attributes
1✔
81
      {
82
        "event" => "publish",
×
83
        "xml" => doi_attribute_xml,
84
        "url" => doi_attribute_url
85
      }
86
    end
87
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc