lib/ezid/metadata.rb in ezid-client-0.2.0 vs lib/ezid/metadata.rb in ezid-client-0.3.0

- old
+ new

@@ -35,14 +35,10 @@ # EZID internal writable metadata elements INTERNAL_READWRITE_ELEMENTS = %w( _coowners _target _profile _status _export _crossref ).freeze # EZID internal metadata elements INTERNAL_ELEMENTS = (INTERNAL_READONLY_ELEMENTS + INTERNAL_READWRITE_ELEMENTS).freeze - - # Internal metadata element which are datetime values - # @note EZID outputs datetime info as epoch seconds. - DATETIME_ELEMENTS = %w( _created _updated ).freeze # EZID metadata field/value separator ANVL_SEPARATOR = ": " # Characters to escape in element values on output to EZID @@ -68,12 +64,11 @@ # Output metadata in EZID ANVL format # @see http://ezid.cdlib.org/doc/apidoc.html#request-response-bodies # @return [String] the ANVL output def to_anvl - lines = escape_keys.zip(escape_values).map { |e| e.join(ANVL_SEPARATOR) } - lines.join("\n").force_encoding(Encoding::UTF_8) + escape_keys.zip(escape_values).map { |e| e.join(ANVL_SEPARATOR) }.join("\n") end def to_s to_anvl end @@ -84,27 +79,49 @@ def update(data) elements.update(coerce(data)) self end - # method_missing is used to provide internal element readers and writers - def method_missing(name, *args) - if INTERNAL_ELEMENTS.include?(element = "_#{name}") - reader(element) - elsif name.to_s.end_with?("=") && INTERNAL_READWRITE_ELEMENTS.include?(element = "_#{name}".sub("=", "")) - writer(element, args.first) - else - super - end + # Identifier status + # @return [String] the status + def status + reader("_status") end + # The time the identifier was created + # @return [Time] the time + def created + value = reader("_created") + return Time.at(value.to_i) if value + value + end + + # The time the identifier was last updated + # @return [Time] the time + def updated + value = reader("_updated") + return Time.at(value.to_i) if value + value + end + + # The identifier's preferred metadata profile + # @see http://ezid.cdlib.org/doc/apidoc.html#metadata-profiles + # @return [String] the profile + def profile + reader("_profile") + end + + # The identifier's target URL + # @return [String] the URL + def target + reader("_target") + end + private def reader(element) - value = self[element] - return Time.at(value.to_i) if DATETIME_ELEMENTS.include?(element) && !value.nil? - value + self[element] end def writer(element, value) self[element] = value end @@ -134,10 +151,10 @@ # @see http://ezid.cdlib.org/doc/apidoc.html#request-response-bodies # @param re [Regexp] the regular expression to match for escaping # @param value [String] the value to escape # @return [String] the escaped value def escape(re, value) - value.gsub(re) { |m| URI.encode_www_form_component(m) } + value.gsub(re) { |m| URI.encode_www_form_component(m, Encoding::UTF_8) } end # Unescape value from EZID host (or other source) # @see http://ezid.cdlib.org/doc/apidoc.html#request-response-bodies # @param value [String] the value to unescape