Class: AuthorizeNet::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/authorize_net/util.rb

Class Method Summary collapse

Class Method Details

.buildXmlFromObject(obj, parent_tag = nil) ⇒ Object

Builds XML from Ruby Hashes/Arrays/Primitives



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
50
51
52
# File 'lib/authorize_net/util.rb', line 24

def buildXmlFromObject(obj, parent_tag=nil)
  xml = ""
  has_parent = !parent_tag.nil?

  # Arrays are formatted with the parent tag
  # wrapping each of the array elements for some
  # reason
  if obj.is_a? Array
    obj.each do |e|
      xml += has_parent ? "<#{parent_tag}>" : ""
      xml += buildXmlFromObject(e)
      xml += has_parent ? "</#{parent_tag}>" : ""
    end

  elsif obj.is_a? Hash
    xml += has_parent ? "<#{parent_tag}>" : ""
    obj.keys.each do |key|
      xml += buildXmlFromObject(obj[key], key.to_s)
    end
    xml += has_parent ? "</#{parent_tag}>" : ""

  elsif !obj.nil?
    xml += has_parent ? "<#{parent_tag}>" : ""
    xml += obj.to_s
    xml += has_parent ? "</#{parent_tag}>" : ""
  end

  return xml
end

.getXmlValue(xml, attr_string) ⇒ Object

A wrapper for safely getting the inner value of an XML attribute only if it exists

If multiple instances exist, return the first one



10
11
12
13
14
15
16
17
18
19
# File 'lib/authorize_net/util.rb', line 10

def getXmlValue(xml, attr_string)
  if !xml.respond_to? :at_css || attr_string.nil?
    return nil
  end

  attr = xml.at_css(attr_string)
  if !attr.nil?
    return attr.inner_text
  end
end