Class: Edi::Amazon::ShipCodeMapper

Inherits:
Object
  • Object
show all
Defined in:
app/services/edi/amazon/ship_code_mapper.rb

Constant Summary collapse

CARRIER_CODE_MAP =

Maps internal carrier names to Amazon carrierCode values for ship confirmations
https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/xsd/release_1_9/amzn-base._TTH_.xsd

{
  'UPS' => 'UPS',
  'Ups' => 'UPS',
  'USPS' => 'USPS',
  'Usps' => 'USPS',
  'FedEx' => 'FedEx',
  'Fedex' => 'FedEx',
  'FEDEX' => 'FedEx',
  'Canadapost' => 'Canada Post',
  'Canpar' => 'Canpar',
  'Purolator' => 'PUROLATOR',
  'RlCarriers' => 'R+L',
  'AmazonSeller' => 'Amazon'
}.freeze
PRIMARY_CARRIER_PREFIXES =
{
  'fedex' => 'FedEx',
  'ups' => 'UPS',
  'usps' => 'USPS'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(orchestrator) ⇒ ShipCodeMapper

Returns a new instance of ShipCodeMapper.



33
34
35
36
# File 'app/services/edi/amazon/ship_code_mapper.rb', line 33

def initialize(orchestrator)
  @orchestrator = orchestrator
  build_shipping_map
end

Instance Attribute Details

#orchestratorObject (readonly)

Returns the value of attribute orchestrator.



31
32
33
# File 'app/services/edi/amazon/ship_code_mapper.rb', line 31

def orchestrator
  @orchestrator
end

#shipping_mapObject (readonly)

Returns the value of attribute shipping_map.



31
32
33
# File 'app/services/edi/amazon/ship_code_mapper.rb', line 31

def shipping_map
  @shipping_map
end

Instance Method Details

#az_to_hw(shipping_code) ⇒ Object

Pass an Amazon shipping code (either ShipmentServiceLevelCategory or AutomatedShipMethod)
and retrieve a shipping option result object



115
116
117
118
119
120
# File 'app/services/edi/amazon/ship_code_mapper.rb', line 115

def az_to_hw(shipping_code)
  match = shipping_map.detect { |sor| sor.code == shipping_code }
  match ||= shipping_map.detect { |sor| sor.code.downcase == shipping_code.to_s.downcase }
  match ||= shipping_map.detect { |sor| sor.code == 'UNSP' }
  match
end

#carrier_code(carrier) ⇒ Object

Map internal carrier name to Amazon carrier code for ship confirmations



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/edi/amazon/ship_code_mapper.rb', line 44

def carrier_code(carrier)
  return nil if carrier.blank?

  carrier_str = carrier.to_s.strip
  return CARRIER_CODE_MAP[carrier_str] if CARRIER_CODE_MAP.key?(carrier_str)

  _key, value = CARRIER_CODE_MAP.find { |k, _v| k.casecmp?(carrier_str) }
  return value if value

  carrier_lower = carrier_str.downcase
  PRIMARY_CARRIER_PREFIXES.each do |prefix, amazon_code|
    return amazon_code if carrier_lower.start_with?(prefix)
  end

  nil
end

#carrier_info(shipment) ⇒ Object

Build carrier info hash for ship confirmation, matching Walmart SWW pattern.
For Amazon Buy Shipping shipments, uses the actual carrier from amz_metadata.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/services/edi/amazon/ship_code_mapper.rb', line 63

def carrier_info(shipment)
  is_amzbs = shipment.carrier == 'AmazonSeller'
  actual_carrier = if is_amzbs && shipment.amz_carrier.present?
                     shipment.amz_carrier
                   else
                     shipment.carrier
                   end

  carrier = carrier_code(actual_carrier)
  {
    carrierCode: carrier || 'Other',
    carrierName: actual_carrier.to_s,
    shippingMethod: shipment.delivery&.shipping_method_friendly,
    trackingNumber: shipment.tracking_number
  }.compact
end

#ch_to_hw(shipping_code) ⇒ Object

Map Amazon shipping code to Heatwave shipping option



39
40
41
# File 'app/services/edi/amazon/ship_code_mapper.rb', line 39

def ch_to_hw(shipping_code)
  @shipping_map.find { |result| result.code.casecmp?(shipping_code.to_s) }
end

#hw_to_az(shipping_method_name) ⇒ Object

Translates a heatwave shipping option name to an Amazon shipping code



123
124
125
126
# File 'app/services/edi/amazon/ship_code_mapper.rb', line 123

def hw_to_az(shipping_method_name)
  match = shipping_map.detect { |sor| sor.name == shipping_method_name }&.code
  match || 'Standard'
end