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

pulibrary / pdc_describe / cace366a-ffad-45f1-9b60-678e607fa527

14 May 2024 02:21PM UTC coverage: 60.862% (-35.0%) from 95.908%
cace366a-ffad-45f1-9b60-678e607fa527

push

circleci

jrgriffiniii
wip

1 of 3 new or added lines in 2 files covered. (33.33%)

1194 existing lines in 57 files now uncovered.

2076 of 3411 relevant lines covered (60.86%)

22.71 hits per line

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

93.18
/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?
8✔
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,
8✔
16
                                                password: Rails.configuration.datacite.password,
17
                                                host: Rails.configuration.datacite.host)
18
    @work = work
8✔
19
    @metadata = work.metadata
8✔
20
  end
21

22
  def draft_doi
1✔
23
    if PULDatacite.publish_test_doi?
4✔
24
      Rails.logger.info "Using hard-coded test DOI during development."
1✔
25
      "10.34770/tbd"
1✔
26
    else
27
      result = datacite_connection.autogenerate_doi(prefix: Rails.configuration.datacite.prefix)
3✔
28
      if result.success?
3✔
29
        result.success.doi
2✔
30
      else
31
        raise("Error generating DOI. #{result.failure.status} / #{result.failure.reason_phrase}")
1✔
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?
4✔
38

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

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

69
  private
1✔
70

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

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

80
    def doi_attributes
1✔
81
      {
82
        "event" => "publish",
2✔
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