Class: Ark

Inherits:
Object
  • Object
show all
Defined in:
app/models/ark.rb

Constant Summary collapse

EZID_TEST_SHOULDER =
"ark:/99999"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ezid) ⇒ Ark

Returns a new instance of Ark.

Raises:

  • (ArgumentError)


67
68
69
70
# File 'app/models/ark.rb', line 67

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

Class Method Details

.find(ezid) ⇒ Object



21
22
23
24
25
26
# File 'app/models/ark.rb', line 21

def self.find(ezid)
  Ezid::Identifier.find(format_ark(ezid))
rescue StandardError => error
  Rails.logger.error("Failed to find the EZID #{ezid}: #{error.class}: #{error.message}")
  nil
end

.format_ark(ark) ⇒ Object

Ezid::Identifier.find(ezid) only works if the ezid is formatted as an ark, e.g., “ark:/99999/abc123”



16
17
18
19
# File 'app/models/ark.rb', line 16

def self.format_ark(ark)
  return ark if ark.starts_with?("ark:/")
  "ark:/#{ark}"
end

.mintObject

Mints a new EZID identifier, returns the id (e.g. “ark:/99999/fk4tq65d6k”)



8
9
10
11
# File 'app/models/ark.rb', line 8

def self.mint
  identifier = Ezid::Identifier.mint
  identifier.id
end

.update(ezid, new_url, command_line: false) ⇒ Object

Note:

No testing coverage for StandardError but depends on API

Update the ARK to point to a new target. Sometimes we get an error: Net::HTTPServerException: 400 “Bad Request” If that happens, try again, up to 5 times, sleep 3 seconds between retries. If it still fails, send the error to honeybadger.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/ark.rb', line 34

def self.update(ezid, new_url, command_line: false)
  return if ezid.start_with?(EZID_TEST_SHOULDER)
  Retryable.retryable(tries: 5, sleep: 3, on: [Net::HTTPServerException]) do
    identifier = Ezid::Identifier.find(ezid)
    if identifier.target != new_url
      identifier.target = new_url
      identifier.save
    end
  end
rescue StandardError => error
  message = "Unable to update EZID #{ezid}: #{error.class}: #{error.message}"
  puts message if command_line
  Rails.logger.error(message)
  Honeybadger.notify(message)
end

.valid?(ezid) ⇒ Boolean

Determines whether or not a given EZID string is a valid ARK

Parameters:

  • [String] (ezid)

    the EZID being validated

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
# File 'app/models/ark.rb', line 53

def self.valid?(ezid)
  # Always consider test ARKs valid
  return true if ezid.start_with?(EZID_TEST_SHOULDER)
  # Try and retrieve the ARK
  new(ezid)
  true
rescue ArgumentError
  false
end

.valid_shoulder?(ezid) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/ark.rb', line 63

def self.valid_shoulder?(ezid)
  ezid.exclude?(self::EZID_TEST_SHOULDER)
end

Instance Method Details

#objectObject



72
73
74
# File 'app/models/ark.rb', line 72

def object
  @object ||= self.class.find(ezid)
end

#save!Object



86
87
88
# File 'app/models/ark.rb', line 86

def save!
  object.modify(id, )
end

#targetObject



78
79
80
# File 'app/models/ark.rb', line 78

def target
  [Ezid::Metadata::TARGET]
end

#target=(value) ⇒ Object



82
83
84
# File 'app/models/ark.rb', line 82

def target=(value)
  [Ezid::Metadata::TARGET] = value
end