lib/active_shipping/carriers/usps.rb in active_shipping-1.4.0 vs lib/active_shipping/carriers/usps.rb in active_shipping-1.4.1
- old
+ new
@@ -152,10 +152,17 @@
"VN" => "Vietnam",
"WF" => "Wallis and Futuna Islands",
"WS" => "Western Samoa"
}
+ TRACKING_ODD_COUNTRY_NAMES = {
+ 'TAIWAN' => 'TW',
+ 'MACEDONIA THE FORMER YUGOSLAV REPUBLIC OF'=> 'MK',
+ 'MICRONESIA FEDERATED STATES OF' => 'FM',
+ 'MOLDOVA REPUBLIC OF' => 'MD',
+ }
+
RESPONSE_ERROR_MESSAGES = [
/There is no record of that mail item/,
/This Information has not been included in this Test Server\./,
/Delivery status information is not available/
]
@@ -236,11 +243,18 @@
if prefix = ONLY_PREFIX_EVENTS.find { |p| description.start_with?(p) }
description = prefix
end
- timestamp = "#{node.at('EventDate').text}, #{node.at('EventTime').text}"
+ time = if node.at('EventDate').text.present?
+ timestamp = "#{node.at('EventDate').text}, #{node.at('EventTime').text}"
+ Time.parse(timestamp)
+ else
+ # Arbitrary time in past, because we need to sort properly by time
+ Time.parse("Jan 01, 2000")
+ end
+
event_code = node.at('EventCode').text
city = node.at('EventCity').try(:text)
state = node.at('EventState').try(:text)
zip_code = node.at('EventZIPCode').try(:text)
@@ -248,11 +262,10 @@
country = country_node ? country_node.text : ''
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)
end
@@ -637,11 +650,11 @@
:scheduled_delivery_date => scheduled_delivery
)
end
def error_description_node(node)
- node.xpath('//Error/Description')
+ node.xpath('Error/Description')
end
def response_status_node(node)
node.at('StatusSummary') || error_description_node(node)
end
@@ -653,10 +666,13 @@
def response_message(document)
response_status_node(document).text
end
def find_country_code_case_insensitive(name)
- upcase_name = name.upcase
+ upcase_name = name.upcase.gsub(' ', ', ')
+ if special = TRACKING_ODD_COUNTRY_NAMES[upcase_name]
+ return special
+ end
country = ActiveUtils::Country::COUNTRIES.detect { |c| c[:name].upcase == upcase_name }
raise ActiveShipping::Error, "No country found for #{name}" unless country
country[:alpha2]
end