lib/active_shipping/carriers/usps.rb in active_shipping-1.2.1 vs lib/active_shipping/carriers/usps.rb in active_shipping-1.2.2

- old
+ new

@@ -244,11 +244,13 @@ state = node.at('EventState').try(:text) zip_code = node.at('EventZIPCode').try(:text) country_node = node.at('EventCountry') country = country_node ? country_node.text : '' - country = 'USA' if country.empty? + country = 'UNITED STATES' if country.empty? + # USPS returns upcased country names which ActiveUtils doesn't recognize without translation + country = find_country_code_case_insensitive(country) time = Time.parse(timestamp) zoneless_time = Time.utc(time.year, time.month, time.mday, time.hour, time.min, time.sec) location = Location.new(city: city, state: state, postal_code: zip_code, country: country) EventDetails.new(description, time, zoneless_time, location, event_code) @@ -341,12 +343,15 @@ xml.FirstClassMailType(FIRST_CLASS_MAIL_TYPES[options[:first_class_mail_type].try(:to_sym)]) xml.ZipOrigination(strip_zip(origin_zip)) xml.ZipDestination(strip_zip(destination_zip)) xml.Pounds(0) xml.Ounces("%0.1f" % [package.ounces, 1].max) - xml.Container(CONTAINERS[package.options[:container]] || (package.cylinder? ? 'NONRECTANGULAR' : 'RECTANGULAR')) - xml.Size(USPS.size_code_for(package)) + size_code = USPS.size_code_for(package) + container = CONTAINERS[package.options[:container]] + container ||= (package.cylinder? ? 'NONRECTANGULAR' : 'RECTANGULAR') if size_code == 'LARGE' + xml.Container(container) + xml.Size(size_code) xml.Width("%0.2f" % package.inches(:width)) xml.Length("%0.2f" % package.inches(:length)) xml.Height("%0.2f" % package.inches(:height)) xml.Girth("%0.2f" % package.inches(:girth)) is_machinable = if package.options.has_key?(:machinable) @@ -402,12 +407,14 @@ xml.Girth("%0.2f" % [package.inches(:girth), 0.01].max) if commercial_type = commercial_type(options) xml.public_send(COMMERCIAL_FLAG_NAME.fetch(commercial_type), 'Y') end xml.OriginZip(origin.zip) - xml.AcceptanceDateTime((options[:acceptance_time] || Time.now.utc).iso8601) - xml.DestinationPostalCode(destination.zip) + if destination.zip.present? + xml.AcceptanceDateTime((options[:acceptance_time] || Time.now.utc).iso8601) + xml.DestinationPostalCode(destination.zip) + end end end end end save_request(xml_builder.to_xml) @@ -418,11 +425,11 @@ message = '' rate_hash = {} xml = Nokogiri.XML(response) - if error = xml.at('/Error') + if error = xml.at_xpath('/Error | //ServiceErrors/ServiceError') success = false message = error.at('Description').text else xml.root.xpath('Package').each do |package| if package.at('Error') @@ -638,9 +645,16 @@ node.xpath('Error').length > 0 end def response_message(document) response_status_node(document).text + end + + def find_country_code_case_insensitive(name) + upcase_name = name.upcase + country = ActiveUtils::Country::COUNTRIES.detect { |c| c[:name].upcase == upcase_name } + raise ActiveShipping::Error, "No country found for #{name}" unless country + country[:alpha2] end def commit(action, request, test = false) ssl_get(request_url(action, request, test)) end