Skip to content
Browse files

Have UPS calculate date range based off the business days they returned

  • Loading branch information...
1 parent fe6b240 commit 3248f109b3898fffedcd0c3ccfd92c50e4a0e35f Denis Odorcic committed Jun 18, 2012
View
11 lib/active_shipping/shipping/carrier.rb
@@ -65,6 +65,17 @@ def self.default_location
def save_request(r)
@last_request = r
end
+
+ def timestamp_from_business_day(days)
+ return unless days
+ date = DateTime.now
+ days.times do
+ begin
+ date = date + 1
+ end until ![0,6].include?(date.wday)
+ end
+ date
+ end
end
end
end
View
11 lib/active_shipping/shipping/carriers/shipwire.rb
@@ -173,17 +173,6 @@ def parse_child_attribute(parent, name, attribute)
element.attributes[attribute]
end
end
-
- def timestamp_from_business_day(days)
- return unless days
- date = DateTime.now
- days.times do
- begin
- date = date + 1
- end until ![0,6].include?(date.wday)
- end
- date
- end
end
end
end
View
4 lib/active_shipping/shipping/carriers/ups.rb
@@ -284,15 +284,15 @@ 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
+ days_to_delivery = nil if days_to_delivery == 0
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,
- :delivery_range => [delivery_date])
+ :delivery_range => [timestamp_from_business_day(days_to_delivery)])
end
end
RateResponse.new(success, message, Hash.from_xml(response).values.first, :rates => rate_estimates, :xml => response, :request => last_request)
View
21 test/unit/carriers/ups_test.rb
@@ -131,17 +131,26 @@ 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
+
+ def test_delivery_range_takes_weekend_into_consideration
+ mock_response = xml_fixture('ups/test_real_home_as_residential_destination_response')
+ @carrier.expects(:commit).returns(mock_response)
+ response = @carrier.find_rates( @locations[:beverly_hills],
+ @locations[:real_home_as_residential],
+ @packages.values_at(:chocolate_stuff))
+
+ DateTime.stubs(:now => DateTime.new(2012, 6, 15))
+ date_test = [nil, 3, 2, 1, 1, 1].map do |days|
+ DateTime.now + days + 3 if days
end
-
+
assert_equal date_test, response.rates.map(&:delivery_date)
end
-
+
def test_maximum_weight
assert Package.new(150 * 16, [5,5,5], :units => :imperial).mass == @carrier.maximum_weight
assert Package.new((150 * 16) + 0.01, [5,5,5], :units => :imperial).mass > @carrier.maximum_weight
assert Package.new((150 * 16) - 0.01, [5,5,5], :units => :imperial).mass < @carrier.maximum_weight
end
-end
+end

0 comments on commit 3248f10

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