Skip to content

Commit c799a3d

Browse files
committed
Fix acronym regexp to not match for example COM against CompanyName #2
1 parent a04fe4d commit c799a3d

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

lib/qbxml/hash.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def self.from_hash(hash, opts = {}, &block)
1818
lambda { |k|
1919
# QB wants things like ListID, not ListId. Adding inflections then using camelize can accomplish
2020
# 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}" }
21+
k.camelize.gsub(Qbxml::Types::ACRONYM_REGEXP) { "#{$1}#{$2.upcase}#{$3}" }
2222
}
2323
elsif opts[:underscore]
2424
lambda { |k| k.underscore }

lib/qbxml/types.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ module Qbxml::Types
3535
'PIN', 'SSN', 'COM', 'CLSID', 'FOB', 'EIN', 'UOM', 'PO', 'PIN', 'QB']
3636

3737
# Based on the regexp in ActiveSupport::Inflector.camelize
38-
ACRONYM_REGEXP = Regexp.new("(?:(^|[a-z]|\\/))(#{ACRONYMS.map{|a| a.capitalize}.join("|")})")
38+
# Substring 1: Start of string, lower case letter, or slash
39+
# Substring 2: One of the acronyms above, In Capitalized Casing
40+
# Substring 3: End of string or capital letter
41+
ACRONYM_REGEXP = Regexp.new("(?:(^|[a-z]|\\/))(#{ACRONYMS.map{|a| a.capitalize}.join("|")})([A-Z]|$)")
3942

4043
end

test/unit/hash_to_xml_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,31 @@ def test_hash_to_xml_invoice_mod
4444
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"}}]}}})
4545
end
4646

47+
def test_hash_to_xml_customer_add
48+
qbxml = Qbxml.new
49+
xml = <<-EOF
50+
<?qbxml version="7.0"?>
51+
<QBXML>
52+
<QBXMLMsgsRq>
53+
<CustomerAddRq>
54+
<CustomerAdd>
55+
<Name>Joe Blow</Name>
56+
<CompanyName>Joe Blow Inc.</CompanyName>
57+
<BillAddress>
58+
<Addr1>123 Fake Street</Addr1>
59+
<City>Springfield</City>
60+
<State>Texachussets</State>
61+
<PostalCode>99999</PostalCode>
62+
<Country>USA</Country>
63+
</BillAddress>
64+
</CustomerAdd>
65+
</CustomerAddRq>
66+
</QBXMLMsgsRq>
67+
</QBXML>
68+
EOF
69+
assert_equal xml, qbxml.to_qbxml({:customer_add_rq=>{:customer_add=>{:name=>"Joe Blow", :company_name=>"Joe Blow Inc.", :bill_address=>{:addr1=>"123 Fake Street", :city=>"Springfield", :state=>"Texachussets", :postal_code=>"99999", :country=>"USA"}}}})
70+
end
71+
4772
def test_array_of_strings
4873
assert_equal "<foo>\n <bar>baz</bar>\n <bar>guh</bar>\n</foo>\n", Qbxml::Hash.to_xml({:foo => {:bar => ['baz', 'guh']}}, {skip_instruct: true})
4974
end

0 commit comments

Comments
 (0)