Class: Twilio::Rails::PhoneNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/twilio/rails/phone_number.rb

Overview

A phone number object that includes the country and some optional metadata.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number:, country:, label: nil, project: nil) ⇒ PhoneNumber

Returns a new instance of PhoneNumber.

Parameters:

  • number (String)

    the phone number string.

  • country (String)

    the country code.

  • label (String, nil) (defaults to: nil)

    an optional label for the phone number, such as its source or purpose.

  • project (String, nil) (defaults to: nil)

    an optional project identifier for grouping phone numbers.

Raises:



12
13
14
15
16
17
18
# File 'lib/twilio/rails/phone_number.rb', line 12

def initialize(number:, country:, label: nil, project: nil)
  @number = Twilio::Rails::Formatter.coerce_to_valid_phone_number(number)
  raise Twilio::Rails::Phone::Error, "Invalid phone number '#{ number }'" unless @number
  @country = country&.upcase
  @label = label
  @project = project.presence&.to_s
end

Instance Attribute Details

#countryObject (readonly)

Returns the value of attribute country.



6
7
8
# File 'lib/twilio/rails/phone_number.rb', line 6

def country
  @country
end

#labelObject (readonly)

Returns the value of attribute label.



6
7
8
# File 'lib/twilio/rails/phone_number.rb', line 6

def label
  @label
end

#numberObject (readonly)

Returns the value of attribute number.



6
7
8
# File 'lib/twilio/rails/phone_number.rb', line 6

def number
  @number
end

#projectObject (readonly)

Returns the value of attribute project.



6
7
8
# File 'lib/twilio/rails/phone_number.rb', line 6

def project
  @project
end

Instance Method Details

#to_sString

Returns a human readable string representation of the phone number and its metadata.

Returns:

  • (String)

    a human readable string representation of the phone number and its metadata.



21
22
23
24
25
26
# File 'lib/twilio/rails/phone_number.rb', line 21

def to_s
  s = "Phone number #{ number } (#{ country })"
  s = "#{ s } #{ label }" if label.present?
  s = "#{ s } for #{ project }" if project.present?
  s
end