Class: Israeli::Validators::Phone
- Inherits:
-
Object
- Object
- Israeli::Validators::Phone
- Defined in:
- lib/israeli/validators/phone.rb
Overview
Validates Israeli phone numbers.
Supports three types of Israeli phone numbers:
- Mobile: 10 digits starting with 05X (050, 052, 053, 054, 055, 058)
- Landline: 9 digits starting with 0X (02, 03, 04, 08, 09)
- VoIP: 10 digits starting with 07X (072-079)
Automatically handles international format (+972) conversion.
Constant Summary collapse
- MOBILE_PATTERN =
Mobile phone pattern: 05X followed by 7 more digits (10 total)
/\A05\d{8}\z/- LANDLINE_PATTERN =
Landline pattern: 0X followed by 7 digits (9 total) Valid area codes: 02 (Jerusalem), 03 (Tel Aviv), 04 (Haifa), 08 (South), 09 (Sharon)
/\A0[2-489]\d{7}\z/- VOIP_PATTERN =
VoIP pattern: 07X (X=2-9) followed by 7 digits (10 total)
/\A07[2-9]\d{7}\z/
Class Method Summary collapse
-
.detect_type(value) ⇒ Symbol?
Detects the type of phone number.
-
.format(value, style: :dashed) ⇒ String?
Formats a phone number.
-
.invalid_reason(value, type: :any) ⇒ Symbol?
Returns the reason why a phone number is invalid.
-
.landline?(value) ⇒ Boolean
Checks if the number is a valid landline number.
-
.mobile?(value) ⇒ Boolean
Checks if the number is a valid mobile number.
-
.valid?(value, type: :any) ⇒ Boolean
Validates an Israeli phone number.
-
.voip?(value) ⇒ Boolean
Checks if the number is a valid VoIP number.
Class Method Details
.detect_type(value) ⇒ Symbol?
Detects the type of phone number.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/israeli/validators/phone.rb', line 87 def self.detect_type(value) normalized = Sanitizer.normalize_phone(value) return nil if normalized.nil? || normalized.empty? return :mobile if mobile?(normalized) return :landline if landline?(normalized) return :voip if voip?(normalized) nil end |
.format(value, style: :dashed) ⇒ String?
Formats a phone number.
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/israeli/validators/phone.rb', line 132 def self.format(value, style: :dashed) normalized = Sanitizer.normalize_phone(value) return nil unless valid?(normalized) case style when :dashed format_dashed(normalized) when :international "+972-#{normalized[1..]}" else normalized end end |
.invalid_reason(value, type: :any) ⇒ Symbol?
Returns the reason why a phone number is invalid.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/israeli/validators/phone.rb', line 110 def self.invalid_reason(value, type: :any) normalized = Sanitizer.normalize_phone(value) return :blank if normalized.nil? || normalized.empty? detected = detect_type(normalized) return :invalid_format if detected.nil? return nil if type == :any || type == detected :wrong_type end |
.landline?(value) ⇒ Boolean
Checks if the number is a valid landline number.
65 66 67 |
# File 'lib/israeli/validators/phone.rb', line 65 def self.landline?(value) value.match?(LANDLINE_PATTERN) end |
.mobile?(value) ⇒ Boolean
Checks if the number is a valid mobile number.
57 58 59 |
# File 'lib/israeli/validators/phone.rb', line 57 def self.mobile?(value) value.match?(MOBILE_PATTERN) end |
.valid?(value, type: :any) ⇒ Boolean
Validates an Israeli phone number.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/israeli/validators/phone.rb', line 40 def self.valid?(value, type: :any) normalized = Sanitizer.normalize_phone(value) return false if normalized.nil? || normalized.empty? case type when :mobile then mobile?(normalized) when :landline then landline?(normalized) when :voip then voip?(normalized) when :any then mobile?(normalized) || landline?(normalized) || voip?(normalized) else false end end |
.voip?(value) ⇒ Boolean
Checks if the number is a valid VoIP number.
73 74 75 |
# File 'lib/israeli/validators/phone.rb', line 73 def self.voip?(value) value.match?(VOIP_PATTERN) end |