Class: AtlasEngine::Gb::ValidationTranscriber::FullAddressParser

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
ValidationTranscriber::Formatter
Defined in:
app/countries/atlas_engine/gb/validation_transcriber/full_address_parser.rb

Instance Method Summary collapse

Methods included from ValidationTranscriber::Formatter

#build_address, #strip_trailing_punctuation, #strip_word

Constructor Details

#initialize(address:) ⇒ FullAddressParser

Returns a new instance of FullAddressParser.



12
13
14
# File 'app/countries/atlas_engine/gb/validation_transcriber/full_address_parser.rb', line 12

def initialize(address:)
  @address = address
end

Instance Method Details

#parseObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/countries/atlas_engine/gb/validation_transcriber/full_address_parser.rb', line 37

def parse
  city = @address.city
  if city.present?
    city_parts = city.split(",").map(&:strip)
    post_town = city_parts.first
    if city_parts.count > 1
      county = city_parts[1..-1]&.join(", ")
    end
  end

  parsed_address = ValidationTranscriber::ParsedAddress.new(
    zip: @address.zip,
    province_code: @address.province_code,
    country_code: @address.country_code || "GB",
    post_town: post_town,
    county: county,
  )

  # Split address1 and address2 on both commas and newlines
  components = split_into_components(@address)

  pivot = components.count > 3 ? 3 : components.length
  pivot.downto(1).map do |pivot_value|
    street_components, locality_components = components.partition.with_index do |_, index|
      index <= components.count - pivot_value
    end
    hypothesize(
      street_components: street_components,
      locality_components: locality_components,
      parsed_fields: parsed_address,
    )
  end.flatten
end