class Ezid::Metadata

EZID metadata collection for an identifier.

@api private

Constants

ANVL_SEPARATOR

EZID metadata field/value separator

COMMENT_RE

A comment line

COOWNERS

EZID reserved metadata elements

@see ezid.cdlib.org/doc/apidoc.html#internal-metadata

CREATED
DATACENTER
ELEMENT_VALUE_SEPARATOR

EZID metadata field value separator

ESCAPE_NAMES_RE

Characters to escape in element names on output to EZID @see ezid.cdlib.org/doc/apidoc.html#request-response-bodies

ESCAPE_VALUES_RE

Characters to escape in element values on output to EZID @see ezid.cdlib.org/doc/apidoc.html#request-response-bodies

EXPORT
LINE_CONTINUATION_RE

A line continuation

LINE_ENDING_RE

A line ending

OWNER
OWNERGROUP
PROFILE
READONLY
RESERVED
SHADOWEDBY
SHADOWS
STATUS
TARGET
UNESCAPE_RE

Character sequence to unescape from EZID @see ezid.cdlib.org/doc/apidoc.html#request-response-bodies

UPDATED

Public Class Methods

new(data=nil, default=nil) click to toggle source

@param data [String, Hash, Ezid::Metadata] the initial data @param default [Object] DO NOT USE!

This param is included for compatibility with Hashie::Mash
and will raise a NotImplementedError if passed a non-nil value.
Calls superclass method
# File lib/ezid/metadata.rb, line 62
def initialize(data=nil, default=nil)
  unless default.nil?
    raise ::NotImplementedError, "ezid-client does not support default metadata values."
  end
  super()
  update(data) if data
end

Public Instance Methods

created() click to toggle source
# File lib/ezid/metadata.rb, line 76
def created
  to_time(_created)
end
elements() click to toggle source
# File lib/ezid/metadata.rb, line 70
def elements
  warn "[DEPRECATION] `Ezid::Metadata#elements` is deprecated and will be removed in ezid-client 2.0." \
       " Use the `Ezid::Metadata` instance itself instead. (called from #{caller.first})"
  self
end
replace(data) click to toggle source
Calls superclass method
# File lib/ezid/metadata.rb, line 88
def replace(data)
  hsh = coerce(data)

  # Perform additional profile transforms
  MetadataTransformDatacite.inverse(hsh) if hsh["_profile"] == "datacite"

  super hsh
end
to_anvl(include_readonly = true) click to toggle source

Output metadata in EZID ANVL format @see ezid.cdlib.org/doc/apidoc.html#request-response-bodies @return [String] the ANVL output

# File lib/ezid/metadata.rb, line 100
def to_anvl(include_readonly = true)
  hsh = to_h
  hsh.reject! { |k, v| READONLY.include?(k) } unless include_readonly

  # Perform additional profile transforms
  MetadataTransformDatacite.transform(hsh) if profile == "datacite"

  lines = hsh.map do |name, value|
    element = [escape(ESCAPE_NAMES_RE, name), escape(ESCAPE_VALUES_RE, value)]
    element.join(ANVL_SEPARATOR)
  end
  lines.join("\n").force_encoding(Encoding::UTF_8)
end
to_s() click to toggle source
# File lib/ezid/metadata.rb, line 114
def to_s
  to_anvl
end
update(data) click to toggle source
Calls superclass method
# File lib/ezid/metadata.rb, line 84
def update(data)
  super coerce(data)
end
updated() click to toggle source
# File lib/ezid/metadata.rb, line 80
def updated
  to_time(_updated)
end

Protected Instance Methods

convert_key(key) click to toggle source

Overrides Hashie::Mash

Calls superclass method
# File lib/ezid/metadata.rb, line 121
def convert_key(key)
  converted = super
  if RESERVED.include?("_#{converted}")
    "_#{converted}"
  elsif converted =~ /\A(dc|datacite|erc)_/
    converted.sub(/_/, ".")
  else
    converted
  end
end
convert_value(value, duping=false) click to toggle source

Overrides Hashie::Mash

# File lib/ezid/metadata.rb, line 133
def convert_value(value, duping=false)
  if [self.class, Hash, Array].include?(value.class)
    raise Error, "ezid-client does not support instances of #{value.class} as metadata values." \
                 " Convert an enumerable such as an array to an appropriate string representation first."
  end
  value.to_s
end

Private Instance Methods

coerce(data) click to toggle source

Coerce data into a Hash of elements

# File lib/ezid/metadata.rb, line 149
def coerce(data)
  data.respond_to?(:to_h) ? data.to_h : coerce_string(data)
end
coerce_string(data) click to toggle source

Coerce a string of metadata (e.g., from EZID host) into a Hash @note EZID host does not send comments or line continuations.

# File lib/ezid/metadata.rb, line 165
def coerce_string(data)
  data.gsub!(COMMENT_RE, "")
  data.gsub!(LINE_CONTINUATION_RE, " ")
  data.split(LINE_ENDING_RE).each_with_object({}) do |line, memo|
    element, value = line.split(ANVL_SEPARATOR, 2)
    memo[unescape(element.strip)] = unescape(value.strip)
  end
end
escape(regexp, value) click to toggle source

Escape string for sending to EZID host

# File lib/ezid/metadata.rb, line 154
def escape(regexp, value)
  value.gsub(regexp) { |m| URI.encode_www_form_component(m.force_encoding(Encoding::UTF_8)) }
end
to_time(value) click to toggle source
# File lib/ezid/metadata.rb, line 143
def to_time(value)
  time = value.to_i
  (time == 0) ? nil : Time.at(time).utc
end
unescape(value) click to toggle source

Unescape value from EZID host (or other source)

# File lib/ezid/metadata.rb, line 159
def unescape(value)
  value.gsub(UNESCAPE_RE) { |m| URI.decode_www_form_component(m) }
end