Module: Edi::AddressAbbreviator

Included in:
BaseEdiService
Defined in:
app/services/edi/address_abbreviator.rb

Overview

EDI-specific concern that wraps Address::LineAbbreviator with convenience
instance methods and adds note-recording for EDI order processing.

Included in Edi::BaseEdiService so all EDI order processors can use it.

Usage in an EDI order message processor's create_shipping_address:

raw_street1 = order_hash.dig(:AddressLine1)
raw_street2 = order_hash.dig(:AddressLine2)
originals = collect_street_originals(street1: raw_street1, street2: raw_street2)

address.street1 = abbreviate_street(raw_street1)
address.street2 = abbreviate_street(raw_street2)
... save address ...

After the order is saved and has an id, record the originals as a note:

record_address_abbreviation_notes(order, originals, address)

Constant Summary collapse

MAX_LENGTH =
Address::LineAbbreviator::MAX_LENGTH

Instance Method Summary collapse

Instance Method Details

#abbreviate_street(value, locale: nil) ⇒ String?

Abbreviate a single street line via Address::LineAbbreviator.

Parameters:

  • value (String, nil)

    street line

  • locale (Symbol, nil) (defaults to: nil)

    optional locale to scope abbreviations (:en, :fr, :es, etc.)

Returns:

  • (String, nil)

    abbreviated street line



29
30
31
# File 'app/services/edi/address_abbreviator.rb', line 29

def abbreviate_street(value, locale: nil)
  Address::LineAbbreviator.new(value, locale: locale).call.abbreviated
end

#collect_street_originals(street1: nil, street2: nil, street3: nil) ⇒ Object

Collect the original street values before abbreviation so we can log them.
Returns a hash of { field_name => original_value } only for fields that exceed MAX_LENGTH.



35
36
37
38
39
40
41
# File 'app/services/edi/address_abbreviator.rb', line 35

def collect_street_originals(street1: nil, street2: nil, street3: nil)
  originals = {}
  originals[:street1] = street1.to_s.squish if street1.present? && street1.to_s.squish.length > MAX_LENGTH
  originals[:street2] = street2.to_s.squish if street2.present? && street2.to_s.squish.length > MAX_LENGTH
  originals[:street3] = street3.to_s.squish if street3.present? && street3.to_s.squish.length > MAX_LENGTH
  originals
end

#record_address_abbreviation_notes(notable, originals, address) ⇒ Object

Add a note to the order (or any Notable) documenting which fields were
abbreviated and what the originals were, so CS can see the unmodified input.
Safe to call with an empty originals hash (no-op).



46
47
48
49
50
51
52
53
54
55
# File 'app/services/edi/address_abbreviator.rb', line 46

def record_address_abbreviation_notes(notable, originals, address)
  return if originals.blank?

  lines = ["Address was automatically abbreviated to fit #{MAX_LENGTH}-character carrier limit:"]
  originals.each do |field, original|
    abbreviated = address.public_send(field)
    lines << "  #{field}: \"#{original}\" → \"#{abbreviated}\""
  end
  notable.quick_note(lines.join("\n"))
end