module UPS::Data

Constants

CANADIAN_STATES
EMPTY_STATE_MESSAGE
IE_COUNTIES
IE_COUNTY_PREFIXES
US_STATES

Public Class Methods

ie_state_matcher(match_string) click to toggle source

Returns the closest matching Irish state name. Uses Levenshtein distance to correct any possible spelling errors.

@param [String] match_string The Irish State to match @raise [InvalidAttributeError] If the passed match_String is nil or

empty

@return [String] The closest matching irish state with the specified

name
# File lib/ups/data.rb, line 28
def ie_state_matcher(match_string)
  fail Exceptions::InvalidAttributeError, EMPTY_STATE_MESSAGE if
    match_string.nil? || match_string.empty?

  normalized_string = ie_state_normalizer string_normalizer match_string
  counties_with_distances = IE_COUNTIES.map do |county|
    [county, Levenshtein.distance(county.downcase, normalized_string)]
  end
  counties_with_distances_hash = Hash[*counties_with_distances.flatten]
  counties_with_distances_hash.min_by { |_k, v| v }[0]
end
ie_state_normalizer(string) click to toggle source

Normalizes Irish states as per UPS requirements

@param [String] string The Irish State to normalize @return [String] The normalized Irish state name

# File lib/ups/data.rb, line 12
def ie_state_normalizer(string)
  string.tap do |target|
    IE_COUNTY_PREFIXES.each do |prefix|
      target.gsub!(/^#{Regexp.escape(prefix.downcase)} /i, '')
    end
  end
end
string_normalizer(string) click to toggle source

Removes extra characters from a string

@param [String] string The string to normalize and remove special

characters

@return [String] The normalized string

# File lib/ups/data.rb, line 45
def string_normalizer(string)
  string.downcase.gsub(/[^0-9a-z ]/i, '')
end