8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/japanese_address_parser/address_parser.rb', line 8
def call(normalized:, full_address:)
prefecture = ::JapaneseAddressParser::Models::Prefecture.all.find { |candidate| normalized.start_with?(candidate.name) }
return _build_address(full_address: full_address) if prefecture.nil?
city_and_after = normalized.delete_prefix(prefecture.name)
city = prefecture.cities.find { |candidate| city_and_after.start_with?(candidate.name) }
return _build_address(full_address: full_address, prefecture: prefecture) if city.nil?
town_and_after = city_and_after.delete_prefix(city.name)
return _build_address(full_address: full_address, prefecture: prefecture, city: city) if town_and_after.empty?
town = city.towns.find { |candidate| town_and_after == candidate.name }
_build_address(full_address: full_address, prefecture: prefecture, city: city, town: town)
end
|