class Yardi::DocumentParser::Base

Base class for parsing Yardi responses. Subclasses must implement parse_body and can optionally override validator_classes to add more validation than just the default

Constants

DEFAULT_VALIDATOR_CLASSES

Order matters here. The parsing logic for an ErrorResponse relies on the fact that the response is not empty. The MissingProperty error looks similar to the unknown error response, so we want to check that first in order to raise a configuration error if we can.

Public Instance Methods

parse(xml) click to toggle source

@param xml [String] the XML response from the request @return [Object] the parsed object(s) from the rsesponse @raise [Yardi::Error::Base] if the response is invalid

# File lib/yardi/document_parser/base.rb, line 31
def parse(xml)
  begin
    parsed = MultiXml.parse(xml)
  rescue MultiXml::ParseError => e
    raise unparsable_xml_error(xml)
  end

  # This is a temporary code to be able to see what Yardi response
  # looks like when envelope is empty.
  if !parsed.empty? && parsed['soap:Envelope'].nil?
    raise Yardi::Error::UnparsableResponse.new(xml)
  end

  [*DEFAULT_VALIDATOR_CLASSES, *validator_classes].each do |klass|
    klass.new(action: action, parsed_response: parsed).validate!
  end

  parse_body(parsed['soap:Envelope']['soap:Body'])
end

Private Instance Methods

action() click to toggle source
# File lib/yardi/document_parser/base.rb, line 66
def action
  self.class::SOAP_ACTION
end
parse_body(body) click to toggle source

@param body [Hash<String, Object>] the body of the XML response parsed

into a Hash

@return [Object] the parsed object(s) from the rsesponse @raise [Yardi::Error::Base] if the response is invalid

# File lib/yardi/document_parser/base.rb, line 61
def parse_body(body)
  raise NotImplementedError,
        "#{self.class.name} must implement #{__method__}"
end
result_node() click to toggle source
# File lib/yardi/document_parser/base.rb, line 53
def result_node
  body["#{action}Response"]["#{action}Result"]
end
unparsable_xml_error(response) click to toggle source
# File lib/yardi/document_parser/base.rb, line 74
def unparsable_xml_error(response)
  if !response.nil? && response =~ /Service Unavailable/
    Yardi::Error::ServiceUnavailable.new(response)
  elsif !response.nil? && response =~ /!DOCTYPE html/
    Yardi::Error::ErrorResponse.new(response)
  else
    Yardi::Error::UnparsableResponse.new(response)
  end
end
validator_classes() click to toggle source
# File lib/yardi/document_parser/base.rb, line 70
def validator_classes
  []
end