lib/exlibris/aleph/record.rb in exlibris-aleph-0.1.6 vs lib/exlibris/aleph/record.rb in exlibris-aleph-1.0.0

- old
+ new

@@ -1,64 +1,58 @@ module Exlibris module Aleph + require 'marc' # ==Overview # Provides access to the Aleph Record REST API. - class Record < Rest - attr_reader :bib_library, :record_id + class Record < Rest::Base + attr_accessor :bib_library, :record_id - # Creates an instance of Exlibris::Aleph::Record for the given :bib_library and :record_id - def initialize(bib_library, record_id, uri) - @record_id = record_id - raise "Initialization error in #{self.class}. Missing record id." if @record_id.nil? - @bib_library = bib_library - raise "Initialization error in #{self.class}. Missing bib library." if @bib_library.nil? - super(uri) - @uri = @uri+ "/record/#{bib_library}#{record_id}" - # Format :xml parses response as a hash. - # Eventually I'd like this to be the default since it raises exceptions for invalid XML. - # self.class.format :xml - # Format :html does no parsing, just passes back raw XML for parsing by client - self.class.format :html + def initialize(*args) + super end - # Returns an XML string representation of a bib. + # Returns a MARC::Record that contains the bib data # Every method call refreshes the data from the underlying API. - # Raises and exception if there are errors. - # Returns a HTTParty::Response. + # Raises an exception if the response is not valid XML or there are errors. def bib - @response = self.class.get(@uri+ "?view=full") - raise "Error getting bib from Aleph REST APIs. #{error}" unless error.nil? - return @response + self.response = self.class.get("#{record_url}?view=full") + raise_error_if("Error getting bib from Aleph REST APIs.") { + (response.parsed_response["get_record"].nil? or response.parsed_response["get_record"]["record"].nil?) + } + MARC::XMLReader.new(StringIO.new(xml(xml: response.body).at_xpath("get-record/record").to_xml(xml_options).strip)).first end - # Returns an array of items. Each item is represented as an HTTParty hash. + # Returns an array of items. Each item is represented as a Hash. # Every method call refreshes the data from the underlying API. # Raises an exception if the response is not valid XML or there are errors. - # Returns a HTTParty::Response. def items - @items = [] - self.class.format :xml # Since we're parsing xml, this will raise an error # if the response isn't xml. - @response = self.class.get(@uri+ "/items?view=full") - self.class.format :html - raise "Error getting items from Aleph REST APIs. #{error}" if not error.nil? or - @response.nil? or @response["get_item_list"].nil? or @response["get_item_list"]["items"].nil? - item_list = @response["get_item_list"]["items"]["item"] - @items.push(item_list) if item_list.instance_of?(Hash) - item_list.each {|item|@items.push(item)} if item_list.instance_of?(Array) - # Rails.logger.warn("No items returned from Aleph in #{self.class}.") if @items.empty? - return @items + self.response = self.class.get("#{record_url}/items?view=full") + raise_error_if("Error getting items from Aleph REST APIs.") { + (response.parsed_response["get_item_list"].nil? or response.parsed_response["get_item_list"]["items"].nil?) + } + [response.parsed_response["get_item_list"]["items"]["item"]].flatten end - # Returns an XML string representation of holdings + # Returns an array of holdings. Each holding is represented as a MARC::Record. # Every method call refreshes the data from the underlying API. - # Raises and exception if there are errors. - # Returns a HTTParty::Response. + # Raises an exception if there are errors. def holdings - @response = self.class.get(@uri+ "/holdings?view=full") - raise "Error getting holdings from Aleph REST APIs. #{error}" unless error.nil? - return @response + self.response = self.class.get("#{record_url}/holdings?view=full") + raise_error_if("Error getting holdings from Aleph REST APIs.") { + (response.parsed_response["get_hol_list"].nil? or response.parsed_response["get_hol_list"]["holdings"].nil?) + } + xml(xml: response.body).xpath("get-hol-list/holdings/holding").collect{ |holding| + # Change the tag name to record so that the MARC::XMLReader can parse it. + holding.name = "record" + MARC::XMLReader.new(StringIO.new(holding.to_xml(xml_options).strip)).first + } end + + def record_url + @record_url ||= "#{rest_url}/record/#{bib_library}#{record_id}" + end + private :record_url end end end \ No newline at end of file