ruby-oai 0.0.3 — thanks ed

Ed posted revisions to the ruby-oai package.  It includes the ability to utilize libxml as the xml parser. 

 

For those interested, this can now be coupled with the following class to parse a normal dublin core unqualified record into an object version of the record. BTW, I realize that this code makes some use of the eval function — probably one that you want to general avoid — but is a very powerful function when one needs to execute dynamic code.


require 'xml/libxml'
require 'oai'

  class OaiDc
    attr_accessor :title, :creator, :subject, :description, :publisher,
                  :relation, :date, :type, :format, :contributor,
                  :identifier, :source, :language, :coverage, :rights

    def parse_metadata(element)

      labels = self.metadata_list()

      if element == nil: return nil end
      labels.each do |item|
        x = 0
        tmp_element = element.metadata.find("./oai_dc:dc/" + item, ["oai_dc:http://www.openarchives.org/OAI/2.0/oai_dc/","dc:http://purl.org/dc/elements/1.1/"]) 
        item = item.gsub('dc:','')
        eval("@" + item + " = []")
        tmp_element.each do |i|
          s = i.content
          if s != nil
            eval("@" + item + "[" + x.to_s + '] = ' +  s.dump )
            x += 1
          end
        end

        if x==0: eval('@' + item + '[' + x.to_s + '] = nil') end
      end
    end

    def metadata_list()
      labels = ['dc:title','dc:creator',
                'dc:subject','dc:description',
                'dc:publisher','dc:relation',
                'dc:date','dc:type','dc:format',
                'dc:contributor','dc:identifier',
                'dc:source','dc:language',
                'dc:coverage','dc:rights']
    end
  end


Posted

in

by

Tags: