Class: AuthorizeNet::DataObject
- Inherits:
-
Object
- Object
- AuthorizeNet::DataObject
- Defined in:
- lib/authorize_net/data_object.rb
Overview
*** NOTE *** Data objects must have a static ATTRIBUTES hash followed by these lines
self::ATTRIBUTES.keys.each do |attr|
attr_accessor attr
end
Direct Known Subclasses
Address, CreditCard, CustomerProfile, PaymentProfile, Transaction
Constant Summary collapse
- TYPE_ARRAY =
:type_array
- TYPE_OBJECT =
:type_object
- TYPE_OBJECT_ARRAY =
:type_object_array
Class Method Summary collapse
-
.parse(xml) ⇒ Object
Parses xml into a new instance of this class =============================================.
Instance Method Summary collapse
-
#parse(xml) ⇒ Object
Parses XML from the values in the ATTRIBUTES hash in to the attributes of this object.
-
#serialize ⇒ Object
Turns this object into a hash using the keys specified as the keys in ATTRIBUTES =======================================================.
-
#to_h(include_blanks = false) ⇒ Object
Turns this object into a hash using the keys specified as the values in ATTRIBUTES.
Class Method Details
.parse(xml) ⇒ Object
Parses xml into a new instance of this class
112 113 114 115 116 117 118 119 120 |
# File 'lib/authorize_net/data_object.rb', line 112 def parse(xml) if xml.nil? || !xml.respond_to?(:at_css) return end object = new object.parse(xml) return object end |
Instance Method Details
#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 |
#serialize ⇒ Object
Turns this object into a hash using the keys specified as the keys in ATTRIBUTES
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/authorize_net/data_object.rb', line 86 def serialize hash = {} self.class::ATTRIBUTES.keys.each do |attr| spec = self.class::ATTRIBUTES[attr].to_h type = spec[:type] value = send(attr) if value.nil? hash[attr] = nil elsif type == TYPE_OBJECT hash[attr] = value.serialize elsif type == TYPE_OBJECT_ARRAY hash[attr] = value.map{ |e| e.serialize } else hash[attr] = value end end return hash end |
#to_h(include_blanks = false) ⇒ Object
Turns this object into a hash using the keys specified as the values in ATTRIBUTES
If the value in ATTRIBUTES is nil, use the String version of the attribute itself
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/authorize_net/data_object.rb', line 58 def to_h(include_blanks=false) hash = {} self.class::ATTRIBUTES.keys.each do |attr| spec = self.class::ATTRIBUTES[attr].to_h key = spec[:key] || attr.to_s type = spec[:type] value = send(attr) if value.nil? if include_blanks hash[key] = nil end elsif type == TYPE_OBJECT hash[key] = value.to_h elsif type == TYPE_OBJECT_ARRAY hash[key] = value.map{ |e| e.to_h } else hash[key] = value end end return hash end |