Module: UPS::Data

Defined in:
lib/ups.rb,
lib/ups/data.rb,
lib/ups/data/us_states.rb,
lib/ups/data/ie_counties.rb,
lib/ups/data/canadian_states.rb,
lib/ups/data/ie_county_prefixes.rb

Constant Summary collapse

EMPTY_STATE_MESSAGE =
'Invalid Address State [:state]'
US_STATES =
{
  'Alabama' => 'AL',
  'Alaska' => 'AK',
  'Arizona' => 'AZ',
  'Arkansas' => 'AR',
  'California' => 'CA',
  'Colorado' => 'CO',
  'Connecticut' => 'CT',
  'Delaware' => 'DE',
  'District of Columbia' => 'DC',
  'Florida' => 'FL',
  'Georgia' => 'GA',
  'Hawaii' => 'HI',
  'Idaho' => 'ID',
  'Illinois' => 'IL',
  'Indiana' => 'IN',
  'Iowa' => 'IA',
  'Kansas' => 'KS',
  'Kentucky' => 'KY',
  'Louisiana' => 'LA',
  'Maine' => 'ME',
  'Maryland' => 'MD',
  'Massachusetts' => 'MA',
  'Michigan' => 'MI',
  'Minnesota' => 'MN',
  'Mississippi' => 'MS',
  'Missouri' => 'MO',
  'Montana' => 'MT',
  'Nebraska' => 'NE',
  'Nevada' => 'NV',
  'New Hampshire' => 'NH',
  'New Jersey' => 'NJ',
  'New Mexico' => 'NM',
  'New York' => 'NY',
  'North Carolina' => 'NC',
  'North Dakota' => 'ND',
  'Ohio' => 'OH',
  'Oklahoma' => 'OK',
  'Oregon' => 'OR',
  'Pennsylvania' => 'PA',
  'Rhode Island' => 'RI',
  'South Carolina' => 'SC',
  'South Dakota' => 'SD',
  'Tennessee' => 'TN',
  'Texas' => 'TX',
  'Utah' => 'UT',
  'Vermont' => 'VT',
  'Virginia' => 'VA',
  'Washington' => 'WA',
  'West Virginia' => 'WV',
  'Wisconsin' => 'WI',
  'Wyoming' => 'WY'
}.insensitive
IE_COUNTIES =
%w(
  Antrim Armagh Carlow Cavan Clare Cork Derry Donegal Down Dublin Fermanagh
  Galway Kerry Kildare Kilkenny Laois Leitrim Limerick Longford Louth Mayo
  Meath Monaghan Offaly Roscommon Sligo Tipperary Tyrone Waterford Westmeath
  Wexford Wicklow
)
CANADIAN_STATES =
{
  'British Columbia' => 'BC',
  'Ontario' => 'ON',
  'Newfoundland and Labrador' => 'NL',
  'Nova Scotia' => 'NS',
  'Prince Edward Island' => 'PE',
  'New Brunswick' => 'NB',
  'Quebec' => 'QC',
  'Manitoba' => 'MB',
  'Saskatchewan' => 'SK',
  'Alberta' => 'AB',
  'Northwest Territories' => 'NT',
  'Nunavut' => 'NU',
  'Yukon Territory' => 'YT'
}.insensitive
IE_COUNTY_PREFIXES =
[
  'County',
  'Cnty',
  'CC.',
  'CC',
  'Ct.',
  'Ct',
  'Co.',
  'Co',
  'C.'
]

Class Method Summary collapse

Class Method Details

.ie_state_matcher(match_string) ⇒ String

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

Parameters:

  • match_string (String)

    The Irish State to match

Returns:

  • (String)

    The closest matching irish state with the specified name

Raises:

  • (InvalidAttributeError)

    If the passed match_String is nil or empty



28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ String

Normalizes Irish states as per UPS requirements

Parameters:

  • string (String)

    The Irish State to normalize

Returns:

  • (String)

    The normalized Irish state name



12
13
14
15
16
17
18
# 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) ⇒ String

Removes extra characters from a string

Parameters:

  • string (String)

    The string to normalize and remove special characters

Returns:

  • (String)

    The normalized string



45
46
47
# File 'lib/ups/data.rb', line 45

def string_normalizer(string)
  string.downcase.gsub(/[^0-9a-z ]/i, '')
end