module ZCC #main search method def ZCC.zoomer(search_term, server, port, database ) puts "\n-------------------------------------\n#{server} #{port} #{database} \n" conn = ZOOM::Connection.new conn.connect(server, port) #do |conn| conn.set_option('charset', 'UTF-8') conn.preferred_record_syntax = 'MARC21' conn.database_name = database # Determines kind of search and creates query full_search = '' if search_term =~ /\d+-\d+/ search_term = ZCC.lccn_conversion(search_term) puts "converted lccn to #{search_term}" full_search = "@attr 1=9 #{search_term}" elsif search_term =~ /\d+[X||\d]/ and !(search_term.match( /[a-wyz]/i )) puts "Searching for ISBN: #{search_term}" full_search = "@attr 1=7 #{search_term}" elsif ( search_term[/[a-z]/i] ) #-- This check for a string could be better! puts "searching for title:#{search_term}" search_term = "\'#{search_term}\'" full_search = "@attr 1=4 \"#{search_term}\"" else puts "What is this search? Does not compute." exit end #conducts search rset = conn.search(full_search) if ( rset.length == 0) return nil end puts "Number of results: #{rset.length}" #Grabs only as many records as are set to be shown rset_recs = rset[0,TO_SHOW] # Turns result set into MARC::Record objects which are returned xmlrecs = '' rset_recs.each do |rsetrec| #puts rsetrec xmlrec = ZCC.convert_char(rsetrec) xmlrecs += xmlrec end return records = MARC::XMLReader.new(StringIO.new(string=xmlrecs)) rescue Exception => except puts "Error! #{except}" return nil end # Tests leader of MARC21 records for character encoding # If leader position 9 is ' ' then convert to UTF-8. # If leader position 9 is 'a' then the record is already UTF-8. # Finally converts to xml def ZCC.convert_char rsetrec rec = MARC::Record.new_from_marc(rsetrec.raw) ldr9 = rec.leader[9, 1] return_rec = '' if ldr9 == ' ' return_rec = rsetrec.xml('MARC-8', 'UTF-8') elsif ldr9 == 'a' return_rec = rsetrec.xml else raise "Invalid value in leader 9 for MARC21" end return_rec end end