Browse files

Added a method to DRY up address building in UPS API requests.

  • Loading branch information...
1 parent 6797133 commit f6056e7bcb805f4951888a34d8bd1b8b13c8a15b @laserlemon laserlemon committed with Jun 2, 2010
Showing with 16 additions and 0 deletions.
  1. +16 −0 lib/shippinglogic/ups/request.rb
View
16 lib/shippinglogic/ups/request.rb
@@ -28,6 +28,22 @@ def build_authentication(b)
b.Password base.password
end
end
+
+ # A convenience method for building the address block in your XML request
+ def build_address(b, type)
+ address_lines = send("#{type}_streets").to_s.split(/(?:\s*\n\s*)+/m, 3)
+
+ b.Address do
+ b.AddressLine1 address_lines[0] if address_lines[0]
+ b.AddressLine2 address_lines[1] if address_lines[1]
+ b.AddressLine3 address_lines[2] if address_lines[2]
+ b.City send("#{type}_city") if send("#{type}_city")
+ b.StateProvinceCode send("#{type}_state") if send("#{type}_state")
+ b.PostalCode send("#{type}_postal_code") if send("#{type}_postal_code")
+ b.CountryCode send("#{type}_country") if send("#{type}_country")
+ b.ResidentialAddressIndicator attribute_names.include?("#{type}_residential") && send("#{type}_residential")
+ end
+ end
end
end
end

0 comments on commit f6056e7

Please sign in to comment.