Sha256: 91d5a89d642602f3aaabc504e982e341e69781e5aab99c97c011b5d42d5c710f

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

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.new(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<String, Object>] 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
amsi-1.0.1 lib/amsi/document_parser/base.rb
amsi-1.0.0 lib/amsi/document_parser/base.rb