Class: CommandTower::Identity::Phone::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
app/services/command_tower/identity/phone/normalizer.rb

Overview

Canonical E.164 normalize/reject entry point for Identity phone numbers.

Defined Under Namespace

Classes: Normalized

Constant Summary collapse

DEFAULT_COUNTRY =
"US"
BLANK_MESSAGE =
"is required"
INVALID_MESSAGE =
"is invalid"

Class Method Summary collapse

Class Method Details

.call(phone_number:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/command_tower/identity/phone/normalizer.rb', line 21

def self.call(phone_number:)
  raw = phone_number.to_s.strip
  return failure(BLANK_MESSAGE) if raw.blank?

  parsed = Phonelib.parse(raw, DEFAULT_COUNTRY)
  return failure(INVALID_MESSAGE) unless parsed.valid?

  e164 = parsed.e164.to_s
  return failure(INVALID_MESSAGE) if e164.blank?

  Normalized.new(normalized: e164, message: nil)
end