lib/lite/address/parser.rb in lite-address-1.0.1 vs lib/lite/address/parser.rb in lite-address-1.1.0

- old
+ new

@@ -1,29 +1,29 @@ # frozen_string_literal: true -require 'countries' unless defined?(ISO3166::Country) +require "countries" unless defined?(ISO3166::Country) module Lite module Address class Parser LOOKUPS = %i[any formal informal intersectional].freeze CAPITALIZATION_PARTS = %w[street street_type street2 street_type2 city unit_prefix].freeze - STREET_POSITIONS = ['', '1', '2'].freeze + STREET_POSITIONS = ["", "1", "2"].freeze attr_reader :address, :country_code - def initialize(address, country_code: 'US') + def initialize(address, country_code: "US") @address = sanitize_address(address) @country_code = sanitize_country_code(country_code) end class << self LOOKUPS.each do |method_name| define_method(method_name) do |address, args = {}| - instance = new(address, country_code: args.delete(:country_code) || 'US') + instance = new(address, country_code: args.delete(:country_code) || "US") instance.public_send(method_name, args) end end end @@ -50,13 +50,13 @@ def intersectional(args = {}) return unless (match = regexp.intersectional_address.match(address)) map = match_map(match) - intersectional_submatch(match, map, 'street') - intersectional_submatch(match, map, 'street_type') - intersectional_rematch(match, map, 'street_type') + intersectional_submatch(match, map, "street") + intersectional_submatch(match, map, "street_type") + intersectional_rematch(match, map, "street_type") generate_address(map, args) end protected @@ -74,11 +74,11 @@ end private def sanitize_address(value) - value.delete_prefix('(').delete_suffix(')') + value.delete_prefix("(").delete_suffix(")") end def sanitize_country_code(value) value.to_s.upcase end @@ -87,23 +87,22 @@ match.names.each_with_object({}) do |name, hash| hash[name] = match[name] if match[name] end end - # rubocop:disable Metrics/AbcSize def normalization_map @normalization_map ||= { - 'prefix' => list.cardinal_types, - 'prefix1' => list.cardinal_types, - 'prefix2' => list.cardinal_types, - 'suffix' => list.cardinal_types, - 'suffix1' => list.cardinal_types, - 'suffix2' => list.cardinal_types, - 'street_type' => list.street_types, - 'street_type1' => list.street_types, - 'street_type2' => list.street_types, - 'state' => list.subdivision_names + "prefix" => list.cardinal_types, + "prefix1" => list.cardinal_types, + "prefix2" => list.cardinal_types, + "suffix" => list.cardinal_types, + "suffix1" => list.cardinal_types, + "suffix2" => list.cardinal_types, + "street_type" => list.street_types, + "street_type1" => list.street_types, + "street_type2" => list.street_types, + "state" => list.subdivision_names } end # rubocop:enable Metrics/AbcSize def intersectional_submatch(match, map, part) @@ -115,40 +114,40 @@ def intersectional_rematch(_match, map, part) return unless map[part] && (!map["#{part}2"] || (map[part] == map["#{part}2"])) type = map[part].dup - return unless type.gsub!(/s\W*$/i, '') && (/\A#{regexp.public_send(part)}\z/io =~ type) + return unless type.gsub!(/s\W*$/i, "") && (/\A#{regexp.public_send(part)}\z/io =~ type) map[part] = map["#{part}2"] = type end def address_strip_chars(map) map.each do |key, string| string.strip! - if key == 'number' - string.gsub!(%r{[^\w\s\-\#&/.]}, '') + if key == "number" + string.gsub!(%r{[^\w\s\-\#&/.]}, "") else - string.gsub!(%r{[^\w\s\-\#&/]}, '') + string.gsub!(%r{[^\w\s\-\#&/]}, "") end end end def address_redundantize_street_type(map) - map['redundant_street_type'] = false - return unless map['street'] && !map['street_type'] + map["redundant_street_type"] = false + return unless map["street"] && !map["street_type"] - match = regexp.street.match(map['street']) - map['street_type'] = match['street_type'] if match - map['redundant_street_type'] = true + match = regexp.street.match(map["street"]) + map["street_type"] = match["street_type"] if match + map["redundant_street_type"] = true end def address_abbreviate_unit_prefixes(map) list.unit_abbr_regexps.each do |abbr, regex| - regex.match(map['unit_prefix']) do |_match| - map['unit_prefix'] = abbr + regex.match(map["unit_prefix"]) do |_match| + map["unit_prefix"] = abbr end end end def address_normalize_values(map) @@ -172,29 +171,29 @@ map.delete("street_type#{suffix}") end end def address_expand_cardinals(map) - return unless map['city'] + return unless map["city"] - map['city'].gsub!(/^(#{regexp.cardinal_code})\s+(?=\S)/o) do |match| + map["city"].gsub!(/^(#{regexp.cardinal_code})\s+(?=\S)/o) do |match| "#{list.cardinal_codes[match[0].upcase]} " end end def address_fix_dirty_ordinals(map) # Sometimes parcel data will have addresses like "1 1ST ST" as "1 1 ST ST" - return unless map['street'] + return unless map["street"] - map['street'].gsub!(/\A(\d+\s+st|\d+\s+nd|\d+\s+rd|\d+\s+th)\z/i) do |match| - match.gsub!(/\s+/, '') + map["street"].gsub!(/\A(\d+\s+st|\d+\s+nd|\d+\s+rd|\d+\s+th)\z/i) do |match| + match.gsub!(/\s+/, "") end end def address_capitalize_parts(map) CAPITALIZATION_PARTS.each do |k| - map[k] = map[k].split.map(&:capitalize).join(' ') if map[k] + map[k] = map[k].split.map(&:capitalize).join(" ") if map[k] end end def generate_address(map, args = {}) address_strip_chars(map) @@ -204,11 +203,12 @@ address_avoid_redundant_street_type(map) if args[:avoid_redundant_street_type] address_expand_cardinals(map) address_fix_dirty_ordinals(map) address_capitalize_parts(map) - map.merge!(country: country, list: list, regexp: regexp) - Lite::Address::Format.new(map) + map[:country] = country + + Lite::Address::Format.new(list:, **map) end end end end