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.

Examples:

Convert French number

PhoneNumber.format("0601020304") # => "0033601020304"

Convert UK number

PhoneNumber.format("07911123456", country_code: "44") # => "00447911123456"

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

Class Method Details

.format(phone, country_code: nil) ⇒ String

Format a phone number to OVH international format (00 prefix)

Examples:

Local French number

PhoneNumber.format("0601020304") # => "0033601020304"

Already international with +

PhoneNumber.format("+33601020304") # => "0033601020304"

Already OVH format

PhoneNumber.format("0033601020304") # => "0033601020304"

UK number

PhoneNumber.format("07911123456", country_code: "44") # => "00447911123456"

Parameters:

  • phone (String)

    Phone number in local or international format

  • country_code (String) (defaults to: nil)

    Country code to use for local numbers (default: from config)

Returns:

  • (String)

    Phone number in OVH format (e.g., “0033601020304”)

Raises:



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

Examples:

Array input

PhoneNumber.format_multiple(["0601020304", "0602030405"])
# => ["0033601020304", "0033602030405"]

Comma-separated string

PhoneNumber.format_multiple("0601020304,0602030405")
# => ["0033601020304", "0033602030405"]

Parameters:

  • phones (Array<String>, String)

    Phone number(s) - can be array or comma-separated string

  • country_code (String) (defaults to: nil)

    Country code to use for local numbers

Returns:

  • (Array<String>)

    Array of formatted phone numbers

Raises:



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

Parameters:

  • phone (String)

    Phone number to validate

Returns:

  • (Boolean)

    true if valid



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

Parameters:

  • phone (String)

    Phone number to validate

Raises:



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