Class: AtlasEngine::ValidationTranscriber::AddressParsings

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
LogHelper
Defined in:
app/lib/atlas_engine/validation_transcriber/address_parsings.rb

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from LogHelper

#log_error, #log_info, #log_warn

Constructor Details

#initialize(address_input:, locale: nil) ⇒ AddressParsings

Returns a new instance of AddressParsings.



13
14
15
16
# File 'app/lib/atlas_engine/validation_transcriber/address_parsings.rb', line 13

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

Instance Method Details

#describes_po_box?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/lib/atlas_engine/validation_transcriber/address_parsings.rb', line 37

def describes_po_box?
  parsings.any? { |parsing| parsing[:po_box] }
end

#log_unparsable_address(address_input) ⇒ Object



54
55
56
# File 'app/lib/atlas_engine/validation_transcriber/address_parsings.rb', line 54

def log_unparsable_address(address_input)
  log_info("[AddressValidation] Unable to parse address lines", address_input.to_h.except(:phone))
end

#parsingsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/lib/atlas_engine/validation_transcriber/address_parsings.rb', line 19

def parsings
  @parsings ||= T.let(
    begin
      if @address_input.country_code.blank?
        []
      elsif @address_input.street_name.nil? && @address_input.street_number.nil?
        parsing_result = AddressParserFactory.create(address: @address_input, locale: @locale).parse
        log_unparsable_address(@address_input) if parsing_result.empty?
        parsing_result
      else
        [{ street: @address_input.street_name, building_num: @address_input.street_number }]
      end
    end,
    T.nilable(T::Array[ParsedComponents]),
  )
end

#potential_building_numbersObject



49
50
51
# File 'app/lib/atlas_engine/validation_transcriber/address_parsings.rb', line 49

def potential_building_numbers
  parsings.pluck(:building_num).compact.uniq.reject { |n| n.match?(/\d{7,}/) }
end

#potential_streetsObject



42
43
44
45
46
# File 'app/lib/atlas_engine/validation_transcriber/address_parsings.rb', line 42

def potential_streets
  potential_parsings = parsings.pluck(:street).compact
  potential_parsings << "po box" if describes_po_box?
  potential_parsings.uniq
end