Skip to content

Commit

Permalink
Update FedEx with new rate and added ability to auto-generate rate na…
Browse files Browse the repository at this point in the history
…me from code that gets returned by their server.
  • Loading branch information
Soleone committed Aug 6, 2010
1 parent c631f19 commit 9317203
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Update FedEx rates and added ability to auto-generate rate name from code that gets returned by FedEx [dennis]
* Assume test_helper is in load path when running tests [cody]
* Add support Kunaki rating service [cody]
* Require active_support instead of activesupport to avoid deprecation warning in Rails 2.3.5 [cody]
Expand Down
12 changes: 10 additions & 2 deletions lib/active_shipping/shipping/carriers/fedex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class FedEx < Carrier
"FEDEX_2_DAY_SATURDAY_DELIVERY" => "FedEx 2 Day Saturday Delivery",
"STANDARD_OVERNIGHT" => "FedEx Standard Overnight",
"FIRST_OVERNIGHT" => "FedEx First Overnight",
"FIRST_OVERNIGHT_SATURDAY_DELIVERY" => "FedEx First Overnight Saturday Delivery",
"FEDEX_EXPRESS_SAVER" => "FedEx Express Saver",
"FEDEX_1_DAY_FREIGHT" => "FedEx 1 Day Freight",
"FEDEX_1_DAY_FREIGHT_SATURDAY_DELIVERY" => "FedEx 1 Day Freight Saturday Delivery",
Expand Down Expand Up @@ -83,6 +84,13 @@ class FedEx < Carrier
'express_reference' => 'EXPRESS_REFERENCE',
'express_mps_master' => 'EXPRESS_MPS_MASTER'
}

def self.service_name_for_code(service_code)
ServiceTypes[service_code] || begin
name = service_code.downcase.split('_').collect{|word| word.capitalize }.join(' ')
"FedEx #{name.sub(/Fedex /, '')}"
end
end

def requirements
[:key, :password, :account, :login]
Expand Down Expand Up @@ -227,10 +235,10 @@ def parse_rate_response(origin, destination, packages, response, options)
root_node.elements.each('RateReplyDetails') do |rated_shipment|
service_code = rated_shipment.get_text('ServiceType').to_s
is_saturday_delivery = rated_shipment.get_text('AppliedOptions').to_s == 'SATURDAY_DELIVERY'
service_type = is_saturday_delivery ? "#{rated_shipment.get_text('ServiceType').to_s}_SATURDAY_DELIVERY" : rated_shipment.get_text('ServiceType').to_s
service_type = is_saturday_delivery ? "#{service_code}_SATURDAY_DELIVERY" : service_code

rate_estimates << RateEstimate.new(origin, destination, @@name,
ServiceTypes[service_type],
self.class.service_name_for_code(service_type),
:service_code => service_code,
:total_price => rated_shipment.get_text('RatedShipmentDetails/ShipmentRateDetail/TotalNetCharge/Amount').to_s.to_f,
:currency => rated_shipment.get_text('RatedShipmentDetails/ShipmentRateDetail/TotalNetCharge/Currency').to_s,
Expand Down
13 changes: 13 additions & 0 deletions test/unit/carriers/fedex_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,17 @@ def test_building_request_and_parsing_response
assert_instance_of Package, package_rate[:package]
assert_nil package_rate[:rate]
end

def test_service_name_for_code
FedEx::ServiceTypes.each do |capitalized_name, readable_name|
assert_equal readable_name, FedEx.service_name_for_code(capitalized_name)
end
end

def test_service_name_for_code_handles_yet_unknown_codes
assert_equal "FedEx Express Saver Saturday Delivery", FedEx.service_name_for_code('FEDEX_EXPRESS_SAVER_SATURDAY_DELIVERY')
assert_equal "FedEx Some Weird Rate", FedEx.service_name_for_code('SOME_WEIRD_RATE')
end


end

0 comments on commit 9317203

Please sign in to comment.