Skip to content

Commit a04fe4d

Browse files
committed
Stop using inflections #2
1 parent c0256da commit a04fe4d

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

lib/qbxml/hash.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ class Qbxml::Hash < ::Hash
1515
def self.from_hash(hash, opts = {}, &block)
1616
key_proc = \
1717
if opts[:camelize]
18-
lambda { |k| k.camelize }
18+
lambda { |k|
19+
# QB wants things like ListID, not ListId. Adding inflections then using camelize can accomplish
20+
# the same thing, but then the inflections will apply to everything the user does everywhere.
21+
k.camelize.gsub(Qbxml::Types::ACRONYM_REGEXP) { "#{$1}#{$2.upcase}" }
22+
}
1923
elsif opts[:underscore]
2024
lambda { |k| k.underscore }
2125
end

lib/qbxml/types.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ module Qbxml::Types
3030
"TIMEINTERVALTYPE" => STR_CAST
3131
}
3232

33+
# Strings in tag names that should be capitalized in QB's XML
3334
ACRONYMS = ['AP', 'AR', 'COGS', 'COM', 'UOM', 'QBXML', 'UI', 'AVS', 'ID',
3435
'PIN', 'SSN', 'COM', 'CLSID', 'FOB', 'EIN', 'UOM', 'PO', 'PIN', 'QB']
3536

36-
ActiveSupport::Inflector.inflections do |inflect|
37-
ACRONYMS.each { |a| inflect.acronym a }
38-
end
37+
# Based on the regexp in ActiveSupport::Inflector.camelize
38+
ACRONYM_REGEXP = Regexp.new("(?:(^|[a-z]|\\/))(#{ACRONYMS.map{|a| a.capitalize}.join("|")})")
3939

4040
end

test/unit/hash_to_xml_test.rb

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,47 @@
11
require 'minitest/autorun'
22
require 'qbxml'
33

4-
class VersionTest < Minitest::Test
4+
class HashToXmlTest < Minitest::Test
55

6-
def test_hash_to_xml
7-
assert_equal "<foo>\n <bar>baz</bar>\n</foo>\n", Qbxml::Hash.to_xml({:foo => {:bar => 'baz'}}, {skip_instruct: true})
6+
def test_hash_to_xml_customer_query
7+
qbxml = Qbxml.new
8+
assert_equal "<?qbxml version=\"7.0\"?>\n<QBXML>\n <QBXMLMsgsRq>\n <CustomerQueryRq>\n <ListID>GUID-GOES-HERE</ListID>\n </CustomerQueryRq>\n </QBXMLMsgsRq>\n</QBXML>\n", qbxml.to_qbxml({:qbxml => {:qbxml_msgs_rq => {:customer_query_rq => {:list_id => 'GUID-GOES-HERE'}}}})
9+
end
10+
11+
def test_hash_to_xml_invoice_mod
12+
qbxml = Qbxml.new
13+
xml = <<-EOF
14+
<?qbxml version="7.0"?>
15+
<QBXML>
16+
<QBXMLMsgsRq>
17+
<InvoiceModRq>
18+
<InvoiceMod>
19+
<TxnID>1929B9-1423150873</TxnID>
20+
<EditSequence/>
21+
<CustomerRef>
22+
<ListID>4A50001-1013529664</ListID>
23+
</CustomerRef>
24+
<TxnDate>2015-01-28</TxnDate>
25+
<RefNumber>12345678</RefNumber>
26+
<Memo></Memo>
27+
<InvoiceLineMod>
28+
<TxnLineID>-1</TxnLineID>
29+
<ItemRef>
30+
<FullName>Sales</FullName>
31+
</ItemRef>
32+
<Desc>Contract 123</Desc>
33+
<Quantity>23.44165</Quantity>
34+
<Rate>515.0</Rate>
35+
<SalesTaxCodeRef>
36+
<FullName>E</FullName>
37+
</SalesTaxCodeRef>
38+
</InvoiceLineMod>
39+
</InvoiceMod>
40+
</InvoiceModRq>
41+
</QBXMLMsgsRq>
42+
</QBXML>
43+
EOF
44+
assert_equal xml, qbxml.to_qbxml({:invoice_mod_rq=>{:invoice_mod=>{:txn_id=>"1929B9-1423150873", :edit_sequence=>nil, :customer_ref=>{:list_id=>"4A50001-1013529664"}, :txn_date=>"2015-01-28", :ref_number=>"12345678", :memo=>"", :invoice_line_mod=>[{:txn_line_id=>-1, :item_ref=>{:full_name=>"Sales"}, :desc=>"Contract 123", :quantity=>"23.44165", :rate=> 515.0, :sales_tax_code_ref=>{:full_name=>"E"}}]}}})
845
end
946

1047
def test_array_of_strings

test/unit/xml_to_hash_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'minitest/autorun'
2+
require 'qbxml'
3+
4+
class XmlToHashTest < Minitest::Test
5+
6+
def test_xml_to_hash
7+
qbxml = Qbxml.new
8+
h = {"qbxml"=>{"xml_attributes"=>{}, "qbxml_msgs_rq"=>{"xml_attributes"=>{}, "customer_query_rq"=>{"xml_attributes"=>{}, "list_id"=>"GUID-GOES-HERE"}}}}
9+
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")
10+
end
11+
12+
end
13+

0 commit comments

Comments
 (0)