Class: Address::LineAbbreviator

Inherits:
Object
  • Object
show all
Defined in:
app/services/address/line_abbreviator.rb

Overview

Abbreviates a single street address line that exceeds the carrier character limit
by applying postal abbreviations for the given locale, then smart-truncating
at a word boundary if still too long.

Supports: :en (US/CA), :fr (CA-QC / France), :es, :pt, :it, :nl, :de
When no locale is given, all abbreviation rules are applied.

Examples:

Basic usage

result = Address::LineAbbreviator.new("14927 Southwest Farmington Boulevard").call
result.abbreviated  # => "14927 SW Farmington Blvd"
result.changed?     # => true
result.original     # => "14927 Southwest Farmington Boulevard"

With locale scoping

result = Address::LineAbbreviator.new("Boulevard René-Lévesque Ouest Bureau 400", locale: :fr).call
result.abbreviated  # => "Boul René-Lévesque O Bur 400"

Defined Under Namespace

Classes: Result

Constant Summary collapse

MAX_LENGTH =

35

Address::MAX_STREET_FIELD_LENGTH

Instance Method Summary collapse

Constructor Details

#initialize(value, locale: nil) ⇒ LineAbbreviator

Returns a new instance of LineAbbreviator.

Parameters:

  • value (String, nil)

    The street line to abbreviate

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

    Limit abbreviations to a specific locale (:en, :fr, :es, :pt, :it, :nl, :de).
    nil applies all locales (safe default).



31
32
33
34
# File 'app/services/address/line_abbreviator.rb', line 31

def initialize(value, locale: nil)
  @value = value
  @locale = locale
end

Instance Method Details

#callObject



36
37
38
39
40
41
42
43
44
45
# File 'app/services/address/line_abbreviator.rb', line 36

def call
  return Result.new(original: nil, abbreviated: nil, changed: false) if @value.blank?

  original = @value.to_s.squish
  return Result.new(original: original, abbreviated: original, changed: false) if original.length <= MAX_LENGTH

  abbreviated = apply_abbreviations(original)

  Result.new(original: original, abbreviated: abbreviated, changed: abbreviated != original)
end