Skip to content
Browse files

Calculates UPS delivery date for rate estimates

Closes #38
  • Loading branch information...
1 parent 5fa2892 commit 43082cef1a57c4f439832be6f4206cbc52e196eb Dennis O'Connor committed
Showing with 11 additions and 1 deletion.
  1. +5 −1 lib/active_shipping/shipping/carriers/ups.rb
  2. +6 −0 test/unit/carriers/ups_test.rb
View
6 lib/active_shipping/shipping/carriers/ups.rb
@@ -275,12 +275,16 @@ def parse_rate_response(origin, destination, packages, response, options={})
xml.elements.each('/*/RatedShipment') do |rated_shipment|
service_code = rated_shipment.get_text('Service/Code').to_s
+ days_to_delivery = rated_shipment.get_text('GuaranteedDaysToDelivery').to_s.to_i
+ delivery_date = days_to_delivery >= 1 ? days_to_delivery.days.from_now.strftime("%Y-%m-%d") : nil
+
rate_estimates << RateEstimate.new(origin, destination, @@name,
service_name_for(origin, service_code),
:total_price => rated_shipment.get_text('TotalCharges/MonetaryValue').to_s.to_f,
:currency => rated_shipment.get_text('TotalCharges/CurrencyCode').to_s,
:service_code => service_code,
- :packages => packages)
+ :packages => packages,
+ :delivery_range => [delivery_date])
end
end
RateResponse.new(success, message, Hash.from_xml(response).values.first, :rates => rate_estimates, :xml => response, :request => last_request)
View
6 test/unit/carriers/ups_test.rb
@@ -71,6 +71,12 @@ def test_response_parsing
"UPS Next Day Air Early A.M.",
"UPS Next Day Air"], response.rates.map(&:service_name)
assert_equal [992, 2191, 3007, 5509, 9401, 6124], response.rates.map(&:price)
+
+ date_test = [nil, 3, 2, 1, 1, 1].map do |days|
+ DateTime.strptime(days.days.from_now.strftime("%Y-%m-%d"), "%Y-%m-%d") if days
+ end
+
+ assert_equal date_test, response.rates.map(&:delivery_date)
end
def test_maximum_weight

0 comments on commit 43082ce

Please sign in to comment.
Something went wrong with that request. Please try again.