Skip to content

Commit 84e93b6

Browse files
committed
Handle arrays of strings in from_qbxml #11
1 parent c799a3d commit 84e93b6

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

lib/qbxml/hash.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ def self.xml_to_hash(node, hash = {}, opts = {})
8787

8888
# Insert node hash into parent hash correctly.
8989
case hash[name]
90-
when Array then hash[name] << node_hash
91-
when Hash then hash[name] = [hash[name], node_hash]
92-
else hash[name] = node_hash
90+
when Array
91+
hash[name] << node_hash
92+
when Hash, String
93+
hash[name] = [hash[name], node_hash]
94+
else
95+
hash[name] = node_hash
9396
end
9497

9598
# Handle child elements
@@ -112,12 +115,14 @@ def self.xml_to_hash(node, hash = {}, opts = {})
112115
node_hash.delete(CONTENT_ROOT)
113116
elsif node_hash[CONTENT_ROOT].present?
114117
node_hash.delete(ATTR_ROOT)
115-
hash[name] = \
116-
if schema
117-
typecast(schema, node.path, node_hash[CONTENT_ROOT], opts[:typecast_cache])
118-
else
119-
node_hash[CONTENT_ROOT]
120-
end
118+
v = schema ? typecast(schema, node.path, node_hash[CONTENT_ROOT], opts[:typecast_cache]) : node_hash[CONTENT_ROOT]
119+
# We only updated the last element
120+
if hash[name].is_a?(Array)
121+
hash[name].pop
122+
hash[name] << v
123+
else
124+
hash[name] = v
125+
end
121126
else
122127
hash[name] = node_hash[CONTENT_ROOT]
123128
end

test/unit/xml_to_hash_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,22 @@ def test_xml_to_hash
99
assert_equal h, qbxml.from_qbxml("<?qbxml version=\"7.0\"?>\n<QBXML>\n <QBXMLMsgsRq>\n <CustomerQueryRq>\n <ListID>GUID-GOES-HERE</ListID>\n </CustomerQueryRq>\n </QBXMLMsgsRq>\n</QBXML>\n")
1010
end
1111

12+
def test_array_of_strings
13+
qbxml = Qbxml.new
14+
h = {
15+
"qbxml" => {
16+
"xml_attributes" => {},
17+
"qbxml_msgs_rq" => {
18+
"xml_attributes" => {},
19+
'invoice_query_rq' => {
20+
"xml_attributes" => {},
21+
'include_ret_element' => ['TxnID', 'RefNumber']
22+
}
23+
}
24+
}
25+
}
26+
assert_equal h, qbxml.from_qbxml("<?qbxml version=\"7.0\"?>\n<QBXML>\n <QBXMLMsgsRq>\n <InvoiceQueryRq>\n <IncludeRetElement>TxnID</IncludeRetElement>\n <IncludeRetElement>RefNumber</IncludeRetElement>\n </InvoiceQueryRq>\n </QBXMLMsgsRq>\n</QBXML>\n")
27+
end
28+
1229
end
1330

0 commit comments

Comments
 (0)