Method: Spaceship::Client#match_phone_to_masked_phone

Defined in:
spaceship/lib/spaceship/two_step_or_factor_client.rb

#match_phone_to_masked_phone(phone_number, masked_number) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'spaceship/lib/spaceship/two_step_or_factor_client.rb', line 266

def match_phone_to_masked_phone(phone_number, masked_number)
  characters_to_remove_from_phone_numbers = ' \-()"'

  # start with e.g. +49 162 1234585 or +1-123-456-7866
  phone_number = phone_number.tr(characters_to_remove_from_phone_numbers, '')
  # cleaned: +491621234585 or +11234567866

  # rubocop:disable Style/AsciiComments
  # start with: +49 •••• •••••85 or +1 (•••) •••-••66
  number_with_dialcode_masked = masked_number.tr(characters_to_remove_from_phone_numbers, '')
  # cleaned: +49•••••••••85 or +1••••••••66
  # rubocop:enable Style/AsciiComments

  maskings_count = number_with_dialcode_masked.count('') # => 9 or 8
  pattern = /^([0-9+]{2,4})([•]{#{maskings_count}})([0-9]{2})$/
  # following regex: range from maskings_count-2 because sometimes the masked number has 1 or 2 dots more than the actual number
  # e.g. https://github.com/fastlane/fastlane/issues/14969
  replacement = "\\1([0-9]{#{maskings_count - 2},#{maskings_count}})\\3"
  number_with_dialcode_regex_part = number_with_dialcode_masked.gsub(pattern, replacement)
  # => +49([0-9]{8,9})85 or +1([0-9]{7,8})66

  backslash = '\\'
  number_with_dialcode_regex_part = backslash + number_with_dialcode_regex_part
  number_with_dialcode_regex = /^#{number_with_dialcode_regex_part}$/
  # => /^\+49([0-9]{8})85$/ or /^\+1([0-9]{7,8})66$/

  return phone_number =~ number_with_dialcode_regex
  # +491621234585 matches /^\+49([0-9]{8})85$/
end