Class: ActiveShipping::FedEx
- Defined in:
- lib/active_shipping/carriers/fedex.rb
Overview
FedEx carrier implementation.
FedEx module by Jimmy Baker (http://github.com/jimmyebaker) Documentation can be found here: http://images.fedex.com/us/developer/product/WebServices/MyWebHelp/PropDevGuide.pdf
Constant Summary collapse
- TEST_URL =
'https://gatewaybeta.fedex.com:443/xml'
- LIVE_URL =
'https://gateway.fedex.com:443/xml'
- CARRIER_CODES =
{ "fedex_ground" => "FDXG", "fedex_express" => "FDXE" }
- DELIVERY_ADDRESS_NODE_NAMES =
%w(DestinationAddress ActualDeliveryAddress)
- SHIPPER_ADDRESS_NODE_NAMES =
%w(ShipperAddress)
- SERVICE_TYPES =
{ "PRIORITY_OVERNIGHT" => "FedEx Priority Overnight", "PRIORITY_OVERNIGHT_SATURDAY_DELIVERY" => "FedEx Priority Overnight Saturday Delivery", "FEDEX_2_DAY" => "FedEx 2 Day", "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", "FEDEX_2_DAY_FREIGHT" => "FedEx 2 Day Freight", "FEDEX_2_DAY_FREIGHT_SATURDAY_DELIVERY" => "FedEx 2 Day Freight Saturday Delivery", "FEDEX_3_DAY_FREIGHT" => "FedEx 3 Day Freight", "FEDEX_3_DAY_FREIGHT_SATURDAY_DELIVERY" => "FedEx 3 Day Freight Saturday Delivery", "INTERNATIONAL_PRIORITY" => "FedEx International Priority", "INTERNATIONAL_PRIORITY_SATURDAY_DELIVERY" => "FedEx International Priority Saturday Delivery", "INTERNATIONAL_ECONOMY" => "FedEx International Economy", "INTERNATIONAL_FIRST" => "FedEx International First", "INTERNATIONAL_PRIORITY_FREIGHT" => "FedEx International Priority Freight", "INTERNATIONAL_ECONOMY_FREIGHT" => "FedEx International Economy Freight", "GROUND_HOME_DELIVERY" => "FedEx Ground Home Delivery", "FEDEX_GROUND" => "FedEx Ground", "INTERNATIONAL_GROUND" => "FedEx International Ground", "SMART_POST" => "FedEx SmartPost", "FEDEX_FREIGHT_PRIORITY" => "FedEx Freight Priority", "FEDEX_FREIGHT_ECONOMY" => "FedEx Freight Economy" }
- PACKAGE_TYPES =
{ "fedex_envelope" => "FEDEX_ENVELOPE", "fedex_pak" => "FEDEX_PAK", "fedex_box" => "FEDEX_BOX", "fedex_tube" => "FEDEX_TUBE", "fedex_10_kg_box" => "FEDEX_10KG_BOX", "fedex_25_kg_box" => "FEDEX_25KG_BOX", "your_packaging" => "YOUR_PACKAGING" }
- DROPOFF_TYPES =
{ 'regular_pickup' => 'REGULAR_PICKUP', 'request_courier' => 'REQUEST_COURIER', 'dropbox' => 'DROP_BOX', 'business_service_center' => 'BUSINESS_SERVICE_CENTER', 'station' => 'STATION' }
- SIGNATURE_OPTION_CODES =
{ adult: 'ADULT', # 21 years plus direct: 'DIRECT', # A person at the delivery address indirect: 'INDIRECT', # A person at the delivery address, or a neighbor, or a signed note for fedex on the door none_required: 'NO_SIGNATURE_REQUIRED', default_for_service: 'SERVICE_DEFAULT' }
- PAYMENT_TYPES =
{ 'sender' => 'SENDER', 'recipient' => 'RECIPIENT', 'third_party' => 'THIRDPARTY', 'collect' => 'COLLECT' }
- PACKAGE_IDENTIFIER_TYPES =
{ 'tracking_number' => 'TRACKING_NUMBER_OR_DOORTAG', 'door_tag' => 'TRACKING_NUMBER_OR_DOORTAG', 'rma' => 'RMA', 'ground_shipment_id' => 'GROUND_SHIPMENT_ID', 'ground_invoice_number' => 'GROUND_INVOICE_NUMBER', 'ground_customer_reference' => 'GROUND_CUSTOMER_REFERENCE', 'ground_po' => 'GROUND_PO', 'express_reference' => 'EXPRESS_REFERENCE', 'express_mps_master' => 'EXPRESS_MPS_MASTER', 'shipper_reference' => 'SHIPPER_REFERENCE', }
- TRANSIT_TIMES =
%w(UNKNOWN ONE_DAY TWO_DAYS THREE_DAYS FOUR_DAYS FIVE_DAYS SIX_DAYS SEVEN_DAYS EIGHT_DAYS NINE_DAYS TEN_DAYS ELEVEN_DAYS TWELVE_DAYS THIRTEEN_DAYS FOURTEEN_DAYS FIFTEEN_DAYS SIXTEEN_DAYS SEVENTEEN_DAYS EIGHTEEN_DAYS)
- TRACKING_STATUS_CODES =
FedEx tracking codes as described in the FedEx Tracking Service WSDL Guide All delays also have been marked as exceptions
HashWithIndifferentAccess.new( 'AA' => :at_airport, 'AD' => :at_delivery, 'AF' => :at_fedex_facility, 'AR' => :at_fedex_facility, 'AP' => :at_pickup, 'CA' => :canceled, 'CH' => :location_changed, 'DE' => :exception, 'DL' => :delivered, 'DP' => :departed_fedex_location, 'DR' => :vehicle_furnished_not_used, 'DS' => :vehicle_dispatched, 'DY' => :exception, 'EA' => :exception, 'ED' => :enroute_to_delivery, 'EO' => :enroute_to_origin_airport, 'EP' => :enroute_to_pickup, 'FD' => :at_fedex_destination, 'HL' => :held_at_location, 'IT' => :in_transit, 'LO' => :left_origin, 'OC' => :order_created, 'OD' => :out_for_delivery, 'PF' => :plane_in_flight, 'PL' => :plane_landed, 'PU' => :picked_up, 'RS' => :return_to_shipper, 'SE' => :exception, 'SF' => :at_sort_facility, 'SP' => :split_status, 'TR' => :transfer )
- DEFAULT_LABEL_STOCK_TYPE =
'PAPER_7X4.75'
- LABEL_FORMATS =
Available return formats for image data when creating labels
['DPL', 'EPL2', 'PDF', 'ZPLII', 'PNG']
- @@name =
"FedEx"
Instance Attribute Summary
Attributes inherited from Carrier
Class Method Summary collapse
Instance Method Summary collapse
- #build_contact_address_nodes(xml, location) ⇒ Object protected
- #build_document(xml, expected_root_tag) ⇒ Object protected
- #build_freight_shipment_detail_node(xml, freight_options, packages, imperial) ⇒ Object protected
- #build_location_node(xml, name, location) ⇒ Object protected
- #build_package_dimensions_node(xml, pkg, imperial) ⇒ Object protected
- #build_package_weight_node(xml, pkg, imperial) ⇒ Object protected
- #build_packages_nodes(xml, packages, imperial) ⇒ Object protected
- #build_rate_request(origin, destination, packages, options = {}) ⇒ Object protected
- #build_rate_request_types_node(xml, type = 'ACCOUNT') ⇒ Object protected
- #build_request_header(xml) ⇒ Object protected
- #build_shipment_request(origin, destination, packages, options = {}) ⇒ Object protected
- #build_shipment_responsible_party_node(xml, origin) ⇒ Object protected
- #build_shipping_charges_payment_node(xml, freight_options) ⇒ Object protected
- #build_tracking_request(tracking_number, options = {}) ⇒ Object protected
- #build_version_node(xml, service_id, major, intermediate, minor) ⇒ Object protected
-
#business_day?(date) ⇒ Boolean
protected
Transit times for FedEx® Ground do not include Saturdays, Sundays, or holidays.
- #business_days_from(date, days, is_home_delivery = false) ⇒ Object protected
- #commit(request, test = false) ⇒ Object protected
-
#create_shipment(origin, destination, packages, options = {}) ⇒ Object
Get Shipping labels.
- #delivery_range_from(transit_time, max_transit_time, delivery_timestamp, is_home_delivery, options) ⇒ Object protected
- #extract_address(document, possible_node_names) ⇒ Object protected
- #extract_timestamp(document, node_name) ⇒ Object protected
- #find_rates(origin, destination, packages, options = {}) ⇒ Object
- #find_tracking_info(tracking_number, options = {}) ⇒ Object
- #has_freight?(options) ⇒ Boolean protected
-
#home_delivery_business_day?(date) ⇒ Boolean
protected
Transit times for FedEx® Home Delivery, do not include Sundays, Mondays, or holidays.
- #location_uses_imperial(location) ⇒ Object protected
- #maximum_address_field_length ⇒ Object
- #parse_rate_response(origin, destination, packages, response, options) ⇒ Object protected
- #parse_ship_response(response) ⇒ Object protected
- #parse_tracking_response(response, options) ⇒ Object protected
- #parse_transit_times(times) ⇒ Object protected
- #requirements ⇒ Object
- #response_message(document) ⇒ Object protected
- #response_success?(document) ⇒ Boolean protected
- #ship_date(delay_in_hours) ⇒ Object protected
- #ship_timestamp(delay_in_hours) ⇒ Object protected
Methods inherited from Carrier
#cancel_shipment, default_location, #initialize, #maximum_weight, #save_request, #timestamp_from_business_day, #valid_credentials?
Constructor Details
This class inherits a constructor from ActiveShipping::Carrier
Class Method Details
.service_name_for_code(service_code) ⇒ Object
142 143 144 |
# File 'lib/active_shipping/carriers/fedex.rb', line 142 def self.service_name_for_code(service_code) SERVICE_TYPES[service_code] || "FedEx #{service_code.titleize.sub(/Fedex /, '')}" end |
Instance Method Details
#build_contact_address_nodes(xml, location) ⇒ Object (protected)
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/active_shipping/carriers/fedex.rb', line 263 def build_contact_address_nodes(xml, location) xml.Contact do xml.PersonName(location.name) xml.CompanyName(location.company) xml.PhoneNumber(location.phone) end xml.Address do xml.StreetLines(location.address1) if location.address1 xml.StreetLines(location.address2) if location.address2 xml.City(location.city) if location.city xml.StateOrProvinceCode(location.state) xml.PostalCode(location.postal_code) xml.CountryCode(location.country_code(:alpha2)) xml.Residential('true') if location.residential? end end |
#build_document(xml, expected_root_tag) ⇒ Object (protected)
760 761 762 763 764 765 766 767 768 769 |
# File 'lib/active_shipping/carriers/fedex.rb', line 760 def build_document(xml, expected_root_tag) document = Nokogiri.XML(xml) { |config| config.strict } document.remove_namespaces! if document.root.nil? || document.root.name != expected_root_tag raise ActiveShipping::ResponseContentError.new(StandardError.new('Invalid document'), xml) end document rescue Nokogiri::XML::SyntaxError => e raise ActiveShipping::ResponseContentError.new(e, xml) end |
#build_freight_shipment_detail_node(xml, freight_options, packages, imperial) ⇒ Object (protected)
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/active_shipping/carriers/fedex.rb', line 369 def build_freight_shipment_detail_node(xml, , packages, imperial) xml.FreightShipmentDetail do # TODO: case of different freight account numbers? xml.FedExFreightAccountNumber([:account]) build_location_node(xml, 'FedExFreightBillingContactAndAddress', [:billing_location]) xml.Role([:role]) packages.each do |pkg| xml.LineItems do xml.FreightClass([:freight_class]) xml.Packaging([:packaging]) build_package_weight_node(xml, pkg, imperial) build_package_dimensions_node(xml, pkg, imperial) end end end end |
#build_location_node(xml, name, location) ⇒ Object (protected)
462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/active_shipping/carriers/fedex.rb', line 462 def build_location_node(xml, name, location) xml.public_send(name) do xml.Address do xml.StreetLines(location.address1) if location.address1 xml.StreetLines(location.address2) if location.address2 xml.City(location.city) if location.city xml.PostalCode(location.postal_code) xml.CountryCode(location.country_code(:alpha2)) xml.Residential(true) unless location.commercial? end end end |
#build_package_dimensions_node(xml, pkg, imperial) ⇒ Object (protected)
398 399 400 401 402 403 404 405 406 |
# File 'lib/active_shipping/carriers/fedex.rb', line 398 def build_package_dimensions_node(xml, pkg, imperial) xml.Dimensions do [:length, :width, :height].each do |axis| value = ((imperial ? pkg.inches(axis) : pkg.cm(axis)).to_f * 1000).round / 1000.0 # 3 decimals xml.public_send(axis.to_s.capitalize, value.ceil) end xml.Units(imperial ? 'IN' : 'CM') end end |
#build_package_weight_node(xml, pkg, imperial) ⇒ Object (protected)
391 392 393 394 395 396 |
# File 'lib/active_shipping/carriers/fedex.rb', line 391 def build_package_weight_node(xml, pkg, imperial) xml.Weight do xml.Units(imperial ? 'LB' : 'KG') xml.Value([((imperial ? pkg.lbs : pkg.kgs).to_f * 1000).round / 1000.0, 0.1].max) end end |
#build_packages_nodes(xml, packages, imperial) ⇒ Object (protected)
347 348 349 350 351 352 353 354 355 |
# File 'lib/active_shipping/carriers/fedex.rb', line 347 def build_packages_nodes(xml, packages, imperial) packages.map do |pkg| xml.RequestedPackageLineItems do xml.GroupPackageCount(1) build_package_weight_node(xml, pkg, imperial) build_package_dimensions_node(xml, pkg, imperial) end end end |
#build_rate_request(origin, destination, packages, options = {}) ⇒ Object (protected)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/active_shipping/carriers/fedex.rb', line 291 def build_rate_request(origin, destination, packages, = {}) imperial = location_uses_imperial(origin) xml_builder = Nokogiri::XML::Builder.new do |xml| xml.RateRequest(xmlns: 'http://fedex.com/ws/rate/v13') do build_request_header(xml) build_version_node(xml, 'crs', 13, 0 ,0) # Returns delivery dates xml.ReturnTransitAndCommit(true) # Returns saturday delivery shipping options when available xml.VariableOptions('SATURDAY_DELIVERY') xml.RequestedShipment do if [:pickup_date] xml.ShipTimestamp([:pickup_date].to_time.iso8601(0)) else xml.ShipTimestamp(([:turn_around_time]).iso8601(0)) end freight = has_freight?() unless freight # fedex api wants this up here otherwise request returns an error xml.DropoffType([:dropoff_type] || 'REGULAR_PICKUP') xml.PackagingType([:packaging_type] || 'YOUR_PACKAGING') end build_location_node(xml, 'Shipper', [:shipper] || origin) build_location_node(xml, 'Recipient', destination) if [:shipper] && [:shipper] != origin build_location_node(xml, 'Origin', origin) end if freight = [:freight] build_shipping_charges_payment_node(xml, ) build_freight_shipment_detail_node(xml, , packages, imperial) build_rate_request_types_node(xml) else xml.SmartPostDetail do xml.Indicia([:smart_post_indicia] || 'PARCEL_SELECT') xml.HubId([:smart_post_hub_id] || 5902) # default to LA end build_rate_request_types_node(xml) xml.PackageCount(packages.size) build_packages_nodes(xml, packages, imperial) end end end end xml_builder.to_xml end |
#build_rate_request_types_node(xml, type = 'ACCOUNT') ⇒ Object (protected)
408 409 410 |
# File 'lib/active_shipping/carriers/fedex.rb', line 408 def build_rate_request_types_node(xml, type = 'ACCOUNT') xml.RateRequestTypes(type) end |
#build_request_header(xml) ⇒ Object (protected)
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/active_shipping/carriers/fedex.rb', line 435 def build_request_header(xml) xml.WebAuthenticationDetail do xml.UserCredential do xml.Key(@options[:key]) xml.Password(@options[:password]) end end xml.ClientDetail do xml.AccountNumber(@options[:account]) xml.MeterNumber(@options[:login]) end xml.TransactionDetail do xml.CustomerTransactionId(@options[:transaction_id] || 'ActiveShipping') # TODO: Need to do something better with this... end end |
#build_shipment_request(origin, destination, packages, options = {}) ⇒ Object (protected)
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/active_shipping/carriers/fedex.rb', line 190 def build_shipment_request(origin, destination, packages, = {}) imperial = location_uses_imperial(origin) xml_builder = Nokogiri::XML::Builder.new do |xml| xml.ProcessShipmentRequest(xmlns: 'http://fedex.com/ws/ship/v13') do build_request_header(xml) build_version_node(xml, 'ship', 13, 0 ,0) xml.RequestedShipment do xml.ShipTimestamp(([:turn_around_time]).iso8601(0)) xml.DropoffType('REGULAR_PICKUP') xml.ServiceType([:service_type] || 'FEDEX_GROUND') xml.PackagingType('YOUR_PACKAGING') xml.Shipper do build_contact_address_nodes(xml, [:shipper] || origin) end xml.Recipient do build_contact_address_nodes(xml, destination) end xml.Origin do build_contact_address_nodes(xml, origin) end xml.ShippingChargesPayment do xml.PaymentType('SENDER') xml.Payor do build_shipment_responsible_party_node(xml, [:shipper] || origin) end end xml.LabelSpecification do xml.LabelFormatType('COMMON2D') xml.ImageType([:label_format] || 'PNG') xml.LabelStockType([:label_stock_type] || DEFAULT_LABEL_STOCK_TYPE) end xml.RateRequestTypes('ACCOUNT') xml.PackageCount(packages.size) packages.each do |package| xml.RequestedPackageLineItems do xml.GroupPackageCount(1) build_package_weight_node(xml, package, imperial) build_package_dimensions_node(xml, package, imperial) # Reference Numbers reference_numbers = Array(package.[:reference_numbers]) if reference_numbers.size > 0 xml.CustomerReferences do reference_numbers.each do |reference_number_info| xml.CustomerReferenceType(reference_number_info[:type] || "CUSTOMER_REFERENCE") xml.Value(reference_number_info[:value]) end end end xml.SpecialServicesRequested do xml.SpecialServiceTypes("SIGNATURE_OPTION") xml.SignatureOptionDetail do xml.OptionType(SIGNATURE_OPTION_CODES[package.[:signature_option] || :default_for_service]) end end end end end end end xml_builder.to_xml end |
#build_shipment_responsible_party_node(xml, origin) ⇒ Object (protected)
280 281 282 283 284 285 286 287 288 289 |
# File 'lib/active_shipping/carriers/fedex.rb', line 280 def build_shipment_responsible_party_node(xml, origin) xml.ResponsibleParty do xml.AccountNumber(@options[:account]) xml.Contact do xml.PersonName(origin.name) xml.CompanyName(origin.company) xml.PhoneNumber(origin.phone) end end end |
#build_shipping_charges_payment_node(xml, freight_options) ⇒ Object (protected)
357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/active_shipping/carriers/fedex.rb', line 357 def build_shipping_charges_payment_node(xml, ) xml.ShippingChargesPayment do xml.PaymentType([:payment_type]) xml.Payor do xml.ResponsibleParty do # TODO: case of different freight account numbers? xml.AccountNumber([:account]) end end end end |
#build_tracking_request(tracking_number, options = {}) ⇒ Object (protected)
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/active_shipping/carriers/fedex.rb', line 412 def build_tracking_request(tracking_number, = {}) xml_builder = Nokogiri::XML::Builder.new do |xml| xml.TrackRequest(xmlns: 'http://fedex.com/ws/track/v7') do build_request_header(xml) build_version_node(xml, 'trck', 7, 0, 0) xml.SelectionDetails do xml.PackageIdentifier do xml.Type(PACKAGE_IDENTIFIER_TYPES[[:package_identifier_type] || 'tracking_number']) xml.Value(tracking_number) end xml.ShipDateRangeBegin([:ship_date_range_begin]) if [:ship_date_range_begin] xml.ShipDateRangeEnd([:ship_date_range_end]) if [:ship_date_range_end] xml.TrackingNumberUniqueIdentifier([:unique_identifier]) if [:unique_identifier] end xml.ProcessingOptions('INCLUDE_DETAILED_SCANS') end end xml_builder.to_xml end |
#build_version_node(xml, service_id, major, intermediate, minor) ⇒ Object (protected)
453 454 455 456 457 458 459 460 |
# File 'lib/active_shipping/carriers/fedex.rb', line 453 def build_version_node(xml, service_id, major, intermediate, minor) xml.Version do xml.ServiceId(service_id) xml.Major(major) xml.Intermediate(intermediate) xml.Minor(minor) end end |
#business_day?(date) ⇒ Boolean (protected)
Transit times for FedEx® Ground do not include Saturdays, Sundays, or holidays.
573 574 575 |
# File 'lib/active_shipping/carriers/fedex.rb', line 573 def business_day?(date) (1..5).include?(date.wday) end |
#business_days_from(date, days, is_home_delivery = false) ⇒ Object (protected)
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
# File 'lib/active_shipping/carriers/fedex.rb', line 556 def business_days_from(date, days, is_home_delivery=false) future_date = date count = 0 while count < days future_date += 1.day if is_home_delivery count += 1 if home_delivery_business_day?(future_date) else count += 1 if business_day?(future_date) end end future_date end |
#commit(request, test = false) ⇒ Object (protected)
717 718 719 |
# File 'lib/active_shipping/carriers/fedex.rb', line 717 def commit(request, test = false) ssl_post(test ? TEST_URL : LIVE_URL, request.gsub("\n", '')) end |
#create_shipment(origin, destination, packages, options = {}) ⇒ Object
Get Shipping labels
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/active_shipping/carriers/fedex.rb', line 171 def create_shipment(origin, destination, packages, = {}) = @options.merge() packages = Array(packages) raise Error, "Multiple packages are not supported yet." if packages.length > 1 request = build_shipment_request(origin, destination, packages, ) logger.debug(request) if logger response = commit(save_request(request), ([:test] || false)) parse_ship_response(response) end |
#delivery_range_from(transit_time, max_transit_time, delivery_timestamp, is_home_delivery, options) ⇒ Object (protected)
529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/active_shipping/carriers/fedex.rb', line 529 def delivery_range_from(transit_time, max_transit_time, , is_home_delivery, ) delivery_range = [, ] # if there's no delivery timestamp but we do have a transit time, use it if .blank? && transit_time.present? transit_range = parse_transit_times([transit_time, max_transit_time.presence || transit_time]) pickup_date = [:pickup_date] || ship_date([:turn_around_time]) delivery_range = transit_range.map { |days| business_days_from(pickup_date, days, is_home_delivery) } end delivery_range end |
#extract_address(document, possible_node_names) ⇒ Object (protected)
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 |
# File 'lib/active_shipping/carriers/fedex.rb', line 730 def extract_address(document, possible_node_names) node = nil args = {} possible_node_names.each do |name| node = document.at(name) break if node end if node args[:country] = node.at('CountryCode').try(:text) || ActiveUtils::Country.new(:alpha2 => 'ZZ', :name => 'Unknown or Invalid Territory', :alpha3 => 'ZZZ', :numeric => '999') args[:province] = node.at('StateOrProvinceCode').try(:text) || 'unknown' args[:city] = node.at('City').try(:text) || 'unknown' end Location.new(args) end |
#extract_timestamp(document, node_name) ⇒ Object (protected)
750 751 752 753 754 755 756 757 758 |
# File 'lib/active_shipping/carriers/fedex.rb', line 750 def (document, node_name) if = document.at(node_name) if .text =~ /\A(\d{4}-\d{2}-\d{2})T00:00:00\Z/ Date.parse($1) else Time.parse(.text) end end end |
#find_rates(origin, destination, packages, options = {}) ⇒ Object
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/active_shipping/carriers/fedex.rb', line 150 def find_rates(origin, destination, packages, = {}) = @options.merge() packages = Array(packages) rate_request = build_rate_request(origin, destination, packages, ) xml = commit(save_request(rate_request), ([:test] || false)) parse_rate_response(origin, destination, packages, xml, ) end |
#find_tracking_info(tracking_number, options = {}) ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/active_shipping/carriers/fedex.rb', line 161 def find_tracking_info(tracking_number, = {}) = @options.merge() tracking_request = build_tracking_request(tracking_number, ) xml = commit(save_request(tracking_request), ([:test] || false)) parse_tracking_response(xml, ) end |
#has_freight?(options) ⇒ Boolean (protected)
387 388 389 |
# File 'lib/active_shipping/carriers/fedex.rb', line 387 def has_freight?() [:freight] && [:freight].present? end |
#home_delivery_business_day?(date) ⇒ Boolean (protected)
Transit times for FedEx® Home Delivery, do not include Sundays, Mondays, or holidays.
578 579 580 |
# File 'lib/active_shipping/carriers/fedex.rb', line 578 def home_delivery_business_day?(date) (2..6).include?(date.wday) end |
#location_uses_imperial(location) ⇒ Object (protected)
771 772 773 |
# File 'lib/active_shipping/carriers/fedex.rb', line 771 def location_uses_imperial(location) %w(US LR MM).include?(location.country_code(:alpha2)) end |
#maximum_address_field_length ⇒ Object
183 184 185 186 |
# File 'lib/active_shipping/carriers/fedex.rb', line 183 def maximum_address_field_length # See Fedex Developper Guide 35 end |
#parse_rate_response(origin, destination, packages, response, options) ⇒ Object (protected)
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/active_shipping/carriers/fedex.rb', line 475 def parse_rate_response(origin, destination, packages, response, ) xml = build_document(response, 'RateReply') success = response_success?(xml) = (xml) if success missing_xml_field = false rate_estimates = xml.root.css('> RateReplyDetails').map do |rated_shipment| begin service_code = rated_shipment.at('ServiceType').text is_saturday_delivery = rated_shipment.at('AppliedOptions').try(:text) == 'SATURDAY_DELIVERY' service_type = is_saturday_delivery ? "#{service_code}_SATURDAY_DELIVERY" : service_code transit_time = rated_shipment.at('TransitTime').text if ["FEDEX_GROUND", "GROUND_HOME_DELIVERY"].include?(service_code) max_transit_time = rated_shipment.at('MaximumTransitTime').try(:text) if service_code == "FEDEX_GROUND" = rated_shipment.at('DeliveryTimestamp').try(:text) delivery_range = delivery_range_from(transit_time, max_transit_time, , (service_code == "GROUND_HOME_DELIVERY"), ) currency = rated_shipment.at('RatedShipmentDetails/ShipmentRateDetail/TotalNetCharge/Currency').text RateEstimate.new(origin, destination, @@name, self.class.service_name_for_code(service_type), :service_code => service_code, :total_price => rated_shipment.at('RatedShipmentDetails/ShipmentRateDetail/TotalNetCharge/Amount').text.to_f, :currency => currency, :packages => packages, :delivery_range => delivery_range) rescue NoMethodError missing_xml_field = true nil end end rate_estimates = rate_estimates.compact logger.warn("[FedexParseRateError] Some fields where missing in the response: #{response}") if logger && missing_xml_field if rate_estimates.empty? success = false if missing_xml_field = "The response from the carrier contained errors and could not be treated" else = "No shipping rates could be found for the destination address" if .blank? end end else rate_estimates = [] end RateResponse.new(success, , Hash.from_xml(response), :rates => rate_estimates, :xml => response, :request => last_request, :log_xml => [:log_xml]) end |
#parse_ship_response(response) ⇒ Object (protected)
543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/active_shipping/carriers/fedex.rb', line 543 def parse_ship_response(response) xml = build_document(response, 'ProcessShipmentReply') success = response_success?(xml) = (xml) response_info = Hash.from_xml(response) tracking_number = xml.css("CompletedPackageDetails TrackingIds TrackingNumber").text base_64_image = xml.css("Label Image").text labels = [Label.new(tracking_number, Base64.decode64(base_64_image))] LabelResponse.new(success, , response_info, {labels: labels}) end |
#parse_tracking_response(response, options) ⇒ Object (protected)
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/active_shipping/carriers/fedex.rb', line 582 def parse_tracking_response(response, ) xml = build_document(response, 'TrackReply') success = response_success?(xml) = (xml) if success origin = nil delivery_signature = nil shipment_events = [] all_tracking_details = xml.root.xpath('CompletedTrackDetails/TrackDetails') tracking_details = case all_tracking_details.length when 1 all_tracking_details.first when 0 raise ActiveShipping::Error, "The response did not contain tracking details" else all_unique_identifiers = xml.root.xpath('CompletedTrackDetails/TrackDetails/TrackingNumberUniqueIdentifier').map(&:text) raise ActiveShipping::Error, "Multiple matches were found. Specify a unqiue identifier: #{all_unique_identifiers.join(', ')}" end first_notification = tracking_details.at('Notification') if first_notification.at('Severity').text == 'ERROR' case first_notification.at('Code').text when '9040' raise ActiveShipping::ShipmentNotFound, first_notification.at('Message').text else raise ActiveShipping::ResponseContentError, StandardError.new(first_notification.at('Message').text) end elsif first_notification.at('Severity').text == 'FAILURE' case first_notification.at('Code').text when '9045' raise ActiveShipping::ResponseContentError, StandardError.new(first_notification.at('Message').text) end end tracking_number = tracking_details.at('TrackingNumber').text status_detail = tracking_details.at('StatusDetail') if status_detail.nil? raise ActiveShipping::Error, "Tracking response does not contain status information" end status_code = status_detail.at('Code').try(:text) if status_code.nil? raise ActiveShipping::Error, "Tracking response does not contain status code" end status_description = (status_detail.at('AncillaryDetails/ReasonDescription') || status_detail.at('Description')).text status = TRACKING_STATUS_CODES[status_code] if status_code == 'DL' && tracking_details.at('AvailableImages').try(:text) == 'SIGNATURE_PROOF_OF_DELIVERY' delivery_signature = tracking_details.at('DeliverySignatureName').text end if origin_node = tracking_details.at('OriginLocationAddress') origin = Location.new( :country => origin_node.at('CountryCode').text, :province => origin_node.at('StateOrProvinceCode').text, :city => origin_node.at('City').text ) end destination = extract_address(tracking_details, DELIVERY_ADDRESS_NODE_NAMES) shipper_address = extract_address(tracking_details, SHIPPER_ADDRESS_NODE_NAMES) ship_time = (tracking_details, 'ShipTimestamp') actual_delivery_time = (tracking_details, 'ActualDeliveryTimestamp') scheduled_delivery_time = (tracking_details, 'EstimatedDeliveryTimestamp') tracking_details.xpath('Events').each do |event| address = event.at('Address') next if address.nil? || address.at('CountryCode').nil? city = address.at('City').try(:text) state = address.at('StateOrProvinceCode').try(:text) zip_code = address.at('PostalCode').try(:text) country = address.at('CountryCode').try(:text) location = Location.new(:city => city, :state => state, :postal_code => zip_code, :country => country) description = event.at('EventDescription').text type_code = event.at('EventType').text time = Time.parse(event.at('Timestamp').text) zoneless_time = time.utc shipment_events << ShipmentEvent.new(description, zoneless_time, location, description, type_code) end shipment_events = shipment_events.sort_by(&:time) end TrackingResponse.new(success, , Hash.from_xml(response), :carrier => @@name, :xml => response, :request => last_request, :status => status, :status_code => status_code, :status_description => status_description, :ship_time => ship_time, :scheduled_delivery_date => scheduled_delivery_time, :actual_delivery_date => actual_delivery_time, :delivery_signature => delivery_signature, :shipment_events => shipment_events, :shipper_address => (shipper_address.nil? || shipper_address.unknown?) ? nil : shipper_address, :origin => origin, :destination => destination, :tracking_number => tracking_number ) end |
#parse_transit_times(times) ⇒ Object (protected)
721 722 723 724 725 726 727 728 |
# File 'lib/active_shipping/carriers/fedex.rb', line 721 def parse_transit_times(times) results = [] times.each do |day_count| days = TRANSIT_TIMES.index(day_count.to_s.chomp) results << days.to_i end results end |
#requirements ⇒ Object
146 147 148 |
# File 'lib/active_shipping/carriers/fedex.rb', line 146 def requirements [:key, :password, :account, :login] end |
#response_message(document) ⇒ Object (protected)
710 711 712 713 714 715 |
# File 'lib/active_shipping/carriers/fedex.rb', line 710 def (document) notifications = document.root.at('Notifications') return "" if notifications.nil? "#{notifications.at('Severity').text} - #{notifications.at('Code').text}: #{notifications.at('Message').text}" end |
#response_success?(document) ⇒ Boolean (protected)
704 705 706 707 708 |
# File 'lib/active_shipping/carriers/fedex.rb', line 704 def response_success?(document) highest_severity = document.root.at('HighestSeverity') return false if highest_severity.nil? %w(SUCCESS WARNING NOTE).include?(highest_severity.text) end |
#ship_date(delay_in_hours) ⇒ Object (protected)
699 700 701 702 |
# File 'lib/active_shipping/carriers/fedex.rb', line 699 def ship_date(delay_in_hours) delay_in_hours ||= 0 (Time.now + delay_in_hours.hours).to_date end |
#ship_timestamp(delay_in_hours) ⇒ Object (protected)
694 695 696 697 |
# File 'lib/active_shipping/carriers/fedex.rb', line 694 def (delay_in_hours) delay_in_hours ||= 0 Time.now + delay_in_hours.hours end |