Sha256: d2a47b188c208f99890aee8d6d0ec20519bc64219eb5199f88ce4bfcfb1cec41

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module HydraPbcore::Methods

  def remove_node(type, index = 0)
    self.find_by_terms(type.to_sym).slice(index.to_i).remove
  end

  # Returns a new Nokogiri::XML object with the contents of self reordered and repackaged as a 
  # valid pbcore xml document.
  #
  # The original xml from the datastream is copied to a new Nokogiri object, then each node
  # is added--in correct order--to a new blank, valid pbcore xml document.
  def to_pbcore_xml
    original = Nokogiri::XML(self.to_xml)
    new_doc = HydraPbcore.blank
    HydraPbcore::DocumentNodes.each do |node|
      original.search(node).each do |n|
        new_doc.root.add_child(n)
      end
    end
    return new_doc
  end

  # Validates a PBCore document against an xsd
  # Returns an array of errors -- an empty array means it's valid
  def valid?
    xsd = Nokogiri::XML::Schema(open("http://pbcore.org/xsd/pbcore-2.0.xsd"))
    xsd.validate(self.to_pbcore_xml)
  end

  # Returns the 4-digit year from a string
  def get_year(s)
    begin
      return DateTime.parse(s).year.to_s
    rescue
      if s.match(/^\d\d\d\d$/)
        return s.to_s
      elsif s.match(/^(\d\d\d\d)-\d\d$/)
        return $1.to_s
      else
        return nil
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hydra-pbcore-1.1.2 lib/hydra-pbcore/methods.rb