require 'multi_xml' require 'amsi/validator/prospect_event_validator' require 'amsi/validator/resident_event_validator' module Amsi module DocumentParser # Base class for parsing AMSI responses. Subclasses must implement # #parse_body and can optionally override #validator_classes to add more # validation than just the default class Base def initialize(params = {}) @params = params end # @param xml [String] the XML response from the request # @return [Object] the parsed object(s) from the response # @raise [Amsi::Error::Base] if the response is invalid def parse(xml) begin parsed_response = MultiXml.parse(xml) rescue MultiXml::ParseError => e raise Amsi::Error::UnparsableResponse, xml end validate_response!(parsed_response) parse_body(parsed_response['soap:Envelope']['soap:Body']) end private def validate_response!(response_hash) [*validator_classes].each do |klass| klass.new(response_hash).validate! end end # @param body [Hash] the body of the XML response parsed # into a Hash # @return [Object] the parsed object(s) from the response # @raise [Amsi::Error::Base] if the response is invalid def parse_body(_body) raise NotImplementedError, "#{self.class.name} must implement #{__method__}" end def validator_classes [Validator::RequestErrors, Validator::RequestFault, Validator::ResidentEventValidator, Validator::ProspectEventValidator] end end end end