require 'net/http' require 'rexml/document' # Pure ruby wrappers for TLC (http://www.tlcdelivers.com) web services # Author:: Justin Duewel-Zahniser (mailto:justin@justindz.org) # Copyright:: Copyright (c) 2006 Justin Duewel-Zahniser # License:: Distributes under the same terms as Ruby module TLC # The LibrarySolution class represents programmatic access to a # TLC Library.Solution v3.3.5 OPAC. # The new method takes two arguments: # * the web server domain - default "demo.tlcdelivers.com" # * the configuration name - default "pac" # e.g. - ls = TLC::LibrarySolution.new('demo.tlcdelivers.com', 'pac') class LibrarySolution attr_accessor(:web_server_address, :config) attr_reader(:availability_url, :availability_rn_url, :detail_url) def initialize(web_server_address='demo.tlcdelivers.com', config='pac') @avail_url = "/TLCScripts/interpac.dll?GetAvailability&IdList=" @detail_url = "/TLCScripts/interpac.dll?LabelDisplay&config=pac&recordnumber=" @web_server_address = web_server_address @config = config end # This method returns the raw XML for a request as a REXML::Document. # It takes three parameters: # * ids - either an ISBN or recordnumber or an array of ISBNs or recordnumbers # * type - :isbn or :rn # * detail - 0, 1 (default) or 2--you are unlikely to need to specify this # The &curbranch=Mail (e.g.) option is not currently supported. def get_avail(ids, type, detail=1) if ids.class == Array if type == :isbn ids.each_with_index do |isbn, i| ids[i] = clean(isbn) end end ids = ids.join(",") else if type == :isbn ids = clean(ids) end end qs = "#{@avail_url}#{ids}&detail=#{detail.to_s}" + if type == :isbn then "&type=isbn" else "" end xml = Net::HTTP.get(@web_server_address, qs) doc = REXML::Document.new(xml) return doc end # This method takes an ISBN and returns the internal recordnumber def get_recordnumber(isbn) return get_avail(isbn, :isbn, 0).root.elements['t'].attribute('rn') end # This method takes an ISBN and returns a URL to the title record display or nil def get_url(isbn) rn = get_recordnumber(isbn) if rn.nil? return nil else return "http://#{@web_server_address}#{@detail_url}#{rn.to_s}" end end # This method takes an ISBN and returns a formatted link to the title record display or nil def get_link(isbn) doc = get_avail(isbn, :isbn) rn = doc.root.elements['t'].attribute('rn') if rn.nil? return nil else ac = tc = 0 doc.elements.each("r/t/lo") { |lo| ac += lo.attributes.get_attribute('ac').value.to_i ; tc += lo.attributes.get_attribute('tc').value.to_i } return "#{ac} of #{tc} available" end end # This method takes an ISBN, checks the catalog for a matching record and returns true or false def has?(isbn) return !get_avail(isbn, :isbn, 0).root.elements['t'].attributes.get_attribute("rn").nil? end # This helper method just cleans up ISBNs def clean(isbn) isbn.gsub(/[^0-9X]/,'') end end end