Module: Ovh::Http2sms::PhoneNumber
- Defined in:
- lib/ovh/http2sms/phone_number.rb
Overview
Phone number formatting and validation utilities
Converts local phone number formats to international format required by OVH API. OVH requires the 00 prefix (e.g., 0033601020304 for French numbers). Supports configurable country codes for different regions.
Constant Summary collapse
- LOCAL_FORMAT_PATTERN =
Pattern for numbers starting with 0 but not 00 (local format)
/\A0(?!0)/- PLUS_FORMAT_PATTERN =
Pattern for numbers starting with + (international format with plus)
/\A\+/- DOUBLE_ZERO_PATTERN =
Pattern for numbers starting with 00 (already OVH format)
/\A00/- VALID_PHONE_PATTERN =
Valid phone number pattern (digits only, 9-17 digits for 00 prefix format)
/\A00\d{7,15}\z/
Class Method Summary collapse
-
.format(phone, country_code: nil) ⇒ String
Format a phone number to OVH international format (00 prefix).
-
.format_multiple(phones, country_code: nil) ⇒ Array<String>
Format multiple phone numbers.
-
.valid?(phone) ⇒ Boolean
Validate a phone number format.
-
.validate!(phone) ⇒ void
Validate a phone number and raise error if invalid.
Class Method Details
.format(phone, country_code: nil) ⇒ String
Format a phone number to OVH international format (00 prefix)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ovh/http2sms/phone_number.rb', line 48 def format(phone, country_code: nil) return nil if phone.nil? country_code ||= Ovh::Http2sms.configuration.default_country_code # Remove all non-digit characters except leading + cleaned = clean_phone(phone) # Convert to OVH international format (00 prefix) formatted = to_ovh_format(cleaned, country_code) # Validate the result validate!(formatted) formatted end |
.format_multiple(phones, country_code: nil) ⇒ Array<String>
Format multiple phone numbers
79 80 81 82 |
# File 'lib/ovh/http2sms/phone_number.rb', line 79 def format_multiple(phones, country_code: nil) phone_array = phones.is_a?(Array) ? phones : phones.to_s.split(",") phone_array.map { |p| format(p.strip, country_code: country_code) } end |
.valid?(phone) ⇒ Boolean
Validate a phone number format
88 89 90 91 92 |
# File 'lib/ovh/http2sms/phone_number.rb', line 88 def valid?(phone) return false if phone.nil? || phone.empty? phone.match?(VALID_PHONE_PATTERN) end |
.validate!(phone) ⇒ void
This method returns an undefined value.
Validate a phone number and raise error if invalid
99 100 101 102 103 104 105 106 107 |
# File 'lib/ovh/http2sms/phone_number.rb', line 99 def validate!(phone) return if valid?(phone) raise PhoneNumberError.new( "Invalid phone number format: '#{phone}'. " \ "Expected OVH format with 00 prefix (e.g., 0033601020304)", phone_number: phone ) end |