Method: AuthorizeNet::DataObject#parse

Defined in:
lib/authorize_net/data_object.rb

#parse(xml) ⇒ Object

Parses XML from the values in the ATTRIBUTES hash in to the attributes of this object.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/authorize_net/data_object.rb', line 18

def parse(xml)
  if xml.nil? || !xml.respond_to?(:at_css)
    return
  end

  self.class::ATTRIBUTES.keys.each do |attr|
    spec = self.class::ATTRIBUTES[attr].to_h
    xml_key = spec[:key] || attr.to_s
    type = spec[:type]
    type_class = spec[:class]

    if (type == TYPE_OBJECT or type == TYPE_OBJECT_ARRAY) and type_class.nil?
      raise "DataObject=#{self.class} Attribute=#{attr} of type #{type} must specify a class"
    end

    if type == TYPE_OBJECT
      obj_xml = xml.at_css(xml_key)
      send("#{attr}=", type_class.parse(obj_xml))

    elsif type == TYPE_OBJECT_ARRAY
      array_xml = xml.css(xml_key)
      send("#{attr}=", array_xml.map{ |x| type_class.parse(x) })

    elsif type == TYPE_ARRAY
      array_xml = xml.css(xml_key)
      send("#{attr}=", array_xml.map{ |x| x.inner_text })

    else
      send("#{attr}=", AuthorizeNet::Util.getXmlValue(xml, xml_key))
    end
  end
end