Class: AtlasEngine::ValidationTranscriber::AddressParserBase

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Includes:
Formatter
Defined in:
app/lib/atlas_engine/validation_transcriber/address_parser_base.rb

Constant Summary collapse

AddressComponents =
T.type_alias { T::Hash[Symbol, String] }
BUILDING_NAME =

Parts that we slot into address format regular expressions

"(?<building_name>[\\w ]+)"
BUILDING_NUM =
"(?<building_num>(" \
'([[:digit:]]+\s)?([[:digit:]]+/[[:digit:]]+)|' \
'[[:digit:]][[:alpha:][:digit:]/\-]*|' \
'[[:alpha:]][[:digit:]][[:alpha:][:digit:]/\-]*' \
"))"
NUMERIC_ONLY_BUILDING_NUM =
"(?<building_num>(" \
'([[:digit:]]+\s+)?[[:digit:]][[:digit:]/]*[[:digit:]]|' \
"[[:digit:]]+" \
"))"
NON_NUMERIC_STREET =
"(?<street>[^[:digit:]/ -].*)"
STREET =
"(?<street>.+)"
STREET_NO_COMMAS =
"(?<street>[^,]+)"
UNIT_TYPE =
'(?<unit_type>[[:alpha:]]+\.?)'
UNIT_NUM =
'(?<unit_num>[[:alpha:][:digit:]/\-]+)'
UNIT_NUM_NO_HYPHEN =
"(?<unit_num>[[:alpha:][:digit:]/]+)"
PO_BOX =
%r{(?:^|\s|/)(?:p(?:ost)?\.?\s*o(?:ffice)?\.?\s*box|box|postal\s*box)\s+(\d+)(?:$|\s)}i

Instance Method Summary collapse

Methods included from Formatter

#build_address, #strip_trailing_punctuation, #strip_word

Constructor Details

#initialize(address:, preprocessor: nil) ⇒ AddressParserBase

Returns a new instance of AddressParserBase.

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/lib/atlas_engine/validation_transcriber/address_parser_base.rb', line 47

def initialize(address:, preprocessor: nil)
  raise ArgumentError, "country_code cannot be blank in address" if address.country_code.blank?

  @constants = T.let(
    AtlasEngine::ValidationTranscriber::Constants.instance,
    AtlasEngine::ValidationTranscriber::Constants,
  )
  @country_regex_formats = T.let(nil, T.nilable(T::Array[Regexp]))
  @address = T.let(address, AddressValidation::TAddress)

  @preprocessor = T.let(
    preprocessor || AddressParserPreprocessor.new(address: @address),
    T.nilable(ValidationTranscriber::AddressParserPreprocessor),
  )
end

Instance Method Details

#parseObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/lib/atlas_engine/validation_transcriber/address_parser_base.rb', line 64

def parse
  candidates = []

  address_lines = @preprocessor&.generate_combinations

  return candidates if address_lines&.empty?

  address_lines&.each do |address_line|
    address_line, po_box = extract_po_box(address_line)

    country_regex_formats.each do |format|
      m = format.match(address_line)
      next if m.nil?

      captures = m.named_captures.symbolize_keys

      next if ridiculous?(captures, @address)

      captures = captures.compact_blank.transform_values! do |value|
        strip_trailing_punctuation(value)
      end
      captures[:po_box] = po_box if po_box

      candidates << captures
    end

    if po_box && candidates.empty?
      candidates << { po_box: po_box }
    end
  end

  candidates.uniq
end