class Experian::Response

Attributes

xml[R]

Public Class Methods

new(xml) click to toggle source
# File lib/experian/response.rb, line 8
def initialize(xml)
  @xml = xml
  @response = parse_xml_response
end

Public Instance Methods

completion_code() click to toggle source
# File lib/experian/response.rb, line 21
def completion_code
  @response["CompletionCode"]
end
completion_message() click to toggle source
# File lib/experian/response.rb, line 25
def completion_message
  Experian::COMPLETION_CODES[completion_code]
end
error?() click to toggle source
# File lib/experian/response.rb, line 61
def error?
  completion_code != "0000" || !error_segment.nil?
end
error_action_indicator() click to toggle source
# File lib/experian/response.rb, line 76
def error_action_indicator
  return unless error_segment
  error_message_segment[9]
end
error_action_indicator_message() click to toggle source
# File lib/experian/response.rb, line 81
def error_action_indicator_message
  Experian::ERROR_ACTION_INDICATORS[error_action_indicator]
end
error_code() click to toggle source
# File lib/experian/response.rb, line 71
def error_code
  return unless error_segment
  error_message_segment[6..8].to_i
end
error_message() click to toggle source
# File lib/experian/response.rb, line 13
def error_message
  @response["ErrorMessage"] || (Experian::Error.message(error_code) if error_code)
end
error_message_segment() click to toggle source

The error message segment is embedded in the error segment :(

# File lib/experian/response.rb, line 66
def error_message_segment
  return unless error_segment
  error_segment[error_segment.index("200")..-1]
end
error_segment() click to toggle source

error_segment returns the entire host response (segments 100, 200, 900) since error responses do not separate segments with “@”.

# File lib/experian/response.rb, line 53
def error_segment
  segment(100)
end
header_segment() click to toggle source
# File lib/experian/response.rb, line 47
def header_segment
  segment(110)
end
host_response() click to toggle source
# File lib/experian/response.rb, line 17
def host_response
  @response["HostResponse"]
end
segment(segment_id) click to toggle source
# File lib/experian/response.rb, line 43
def segment(segment_id)
  segments(segment_id).first
end
segments(segment_id = nil) click to toggle source
# File lib/experian/response.rb, line 33
def segments(segment_id = nil)
  @segments ||= host_response ? host_response.split("@") : []

  if segment_id
    @segments.select { |segment| segment.length >= 3 ? segment[0..2].to_i == segment_id : false }
  else
    @segments
  end
end
success?() click to toggle source
# File lib/experian/response.rb, line 57
def success?
  completion_code == "0000" && !header_segment.nil?
end
transaction_id() click to toggle source
# File lib/experian/response.rb, line 29
def transaction_id
  @response["TransactionId"]
end

Private Instance Methods

parse_element(node) click to toggle source

parse xml node elements recursively into hash

# File lib/experian/response.rb, line 98
def parse_element(node)
  if node.has_elements?
    response = {}
    node.elements.each do |e|
      key = e.name
      value = parse_element(e)
      if response.has_key?(key)
        if response[key].is_a?(Array)
          response[key].push(value)
        else
          response[key] = [response[key], value]
        end
      else
        response[key] = parse_element(e)
      end
    end
  else
    response = node.text
  end
  response
end
parse_xml_response() click to toggle source
# File lib/experian/response.rb, line 87
def parse_xml_response
  xml = REXML::Document.new(@xml)
  root = REXML::XPath.first(xml, "//NetConnectResponse")
  if root
    parse_element(root)
  else
    raise Experian::ClientError, "Invalid xml response from Experian"
  end
end