• 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

53.49
/app/models/ark.rb
1
# frozen_string_literal: true
2
require "ezid-client"
1✔
3

4
class Ark
1✔
5
  EZID_TEST_SHOULDER = "ark:/99999"
1✔
6

7
  # Mints a new EZID identifier, returns the id (e.g. "ark:/99999/fk4tq65d6k")
8
  def self.mint
1✔
UNCOV
9
    identifier = Ezid::Identifier.mint
×
UNCOV
10
    identifier.id
×
11
  end
12

13
  ##
14
  # Ezid::Identifier.find(ezid) only works if the ezid is formatted as an ark,
15
  # e.g., "ark:/99999/abc123"
16
  def self.format_ark(ark)
1✔
17
    return ark if ark.starts_with?("ark:/")
17✔
UNCOV
18
    "ark:/#{ark}"
×
19
  end
20

21
  def self.find(ezid)
1✔
22
    Ezid::Identifier.find(format_ark(ezid))
17✔
23
  rescue StandardError => error
UNCOV
24
    Rails.logger.error("Failed to find the EZID #{ezid}: #{error.class}: #{error.message}")
×
UNCOV
25
    nil
×
26
  end
27

28
  # Update the ARK to point to a new target.
29
  # Sometimes we get an error:
30
  # Net::HTTPServerException: 400 "Bad Request"
31
  # If that happens, try again, up to 5 times, sleep 3 seconds between retries.
32
  # If it still fails, send the error to honeybadger.
33
  def self.update(ezid, new_url, command_line: false)
1✔
UNCOV
34
    return if ezid.start_with?(EZID_TEST_SHOULDER)
×
UNCOV
35
    Retryable.retryable(tries: 5, sleep: 3, on: [Net::HTTPServerException]) do
×
UNCOV
36
      identifier = Ezid::Identifier.find(ezid)
×
UNCOV
37
      if identifier.target != new_url
×
UNCOV
38
        identifier.target = new_url
×
UNCOV
39
        identifier.save
×
40
      end
41
    end
42
  rescue StandardError => error
43
    message = "Unable to update EZID #{ezid}: #{error.class}: #{error.message}"
×
44
    puts message if command_line
×
45
    Rails.logger.error(message)
×
46
    Honeybadger.notify(message)
×
47
  end
48

49
  # Determines whether or not a given EZID string is a valid ARK
50
  # @param [ezid] [String] the EZID being validated
51
  # @return [Boolean]
52
  def self.valid?(ezid)
1✔
53
    # Always consider test ARKs valid
54
    return true if ezid.start_with?(EZID_TEST_SHOULDER)
17✔
55
    # Try and retrieve the ARK
56
    new(ezid)
17✔
57
    true
17✔
58
  rescue ArgumentError
UNCOV
59
    false
×
60
  end
61

62
  def self.valid_shoulder?(ezid)
1✔
63
    ezid.exclude?(self::EZID_TEST_SHOULDER)
17✔
64
  end
65

66
  def initialize(ezid)
1✔
67
    @object = self.class.find(ezid)
17✔
68
    raise(ArgumentError, "Invalid EZID provided for an ARK: #{ezid}") if @object.nil? || !self.class.valid_shoulder?(ezid)
17✔
69
  end
70

71
  def object
1✔
UNCOV
72
    @object ||= self.class.find(ezid)
×
73
  end
74

75
  delegate :id, :metadata, to: :object
1✔
76

77
  def target
1✔
UNCOV
78
    metadata[Ezid::Metadata::TARGET]
×
79
  end
80

81
  def target=(value)
1✔
UNCOV
82
    metadata[Ezid::Metadata::TARGET] = value
×
83
  end
84

85
  def save!
1✔
UNCOV
86
    object.modify(id, metadata)
×
87
  end
88

89
  # ======================
90
  # If in the future we want to update the information of the ARK we can
91
  # implement a few methods as follow:
92
  #
93
  # Update the target URL of the ARK to point to a new URL
94
  # def self.update_target(id, new_url)
95
  #   identifier = Ezid::Identifier.find(id)
96
  #   identifier.target = new_url
97
  #   identifier.save
98
  # end
99
  #
100
  # Update the metadata for an ARK. See https://ezid.cdlib.org/doc/apidoc.html#metadata-profiles
101
  # for details on the profiles supported.
102
  #
103
  # def self.update_metadata(id)
104
  #   metadata = {
105
  #     profile: 'dc',
106
  #     dc_creator: 'somebody',
107
  #     dc_title: 'some title',
108
  #     dc_date: '2022-04-08',
109
  #   }
110
  #   identifier = Ezid::Identifier.modify(id, metadata)
111
  # end
112
  # ======================
113
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