Class: Philiprehberger::Phone::PhoneNumber

Inherits:
Object
  • Object
show all
Includes:
AreaCodeLookup, CarrierLookup, PhoneTypeDetection
Defined in:
lib/philiprehberger/phone.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CarrierLookup

#carrier

Methods included from AreaCodeLookup

#area_code_info

Methods included from PhoneTypeDetection

#phone_type

Constructor Details

#initialize(country_code:, national:, country:) ⇒ PhoneNumber

Returns a new instance of PhoneNumber.



22
23
24
25
26
27
# File 'lib/philiprehberger/phone.rb', line 22

def initialize(country_code:, national:, country:)
  @country_code = country_code
  @national = national
  @country = country
  freeze
end

Instance Attribute Details

#countryObject (readonly)

Returns the value of attribute country.



20
21
22
# File 'lib/philiprehberger/phone.rb', line 20

def country
  @country
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



20
21
22
# File 'lib/philiprehberger/phone.rb', line 20

def country_code
  @country_code
end

#nationalObject (readonly)

Returns the value of attribute national.



20
21
22
# File 'lib/philiprehberger/phone.rb', line 20

def national
  @national
end

Instance Method Details

#==(other) ⇒ Boolean

Equality based on E.164 representation.

Parameters:

  • other (PhoneNumber)

    another phone number to compare

Returns:

  • (Boolean)


85
86
87
# File 'lib/philiprehberger/phone.rb', line 85

def ==(other)
  other.is_a?(PhoneNumber) && e164 == other.e164
end

#country_nameString?

Human-readable country name (e.g. “United States”).

Returns:

  • (String, nil)


116
117
118
# File 'lib/philiprehberger/phone.rb', line 116

def country_name
  COUNTRIES.dig(@country, :name)
end

#e164String

E.164-formatted phone number (e.g. “+15551234567”).

Returns:

  • (String)


44
45
46
# File 'lib/philiprehberger/phone.rb', line 44

def e164
  "+#{@country_code}#{@national}"
end

#formattedString

National (country-specific) formatted representation.

Returns:

  • (String)


51
52
53
# File 'lib/philiprehberger/phone.rb', line 51

def formatted
  format_national
end

#inspectObject



148
149
150
# File 'lib/philiprehberger/phone.rb', line 148

def inspect
  "#<Philiprehberger::Phone::PhoneNumber #{e164} (#{country || 'unknown'})>"
end

#internationalString

International formatted representation with country code prefix.

Returns:

  • (String)


58
59
60
# File 'lib/philiprehberger/phone.rb', line 58

def international
  "+#{@country_code} #{format_national}"
end

#landline?Boolean

Whether the number is a landline.

Returns:

  • (Boolean)


130
131
132
# File 'lib/philiprehberger/phone.rb', line 130

def landline?
  phone_type == :landline
end

#masked(visible: 4) ⇒ String

E.164 form with national digits masked as ‘*` except the last `visible` digits; country code remains visible.

Parameters:

  • visible (Integer) (defaults to: 4)

    number of trailing digits to keep visible

Returns:

  • (String)


74
75
76
77
78
79
# File 'lib/philiprehberger/phone.rb', line 74

def masked(visible: 4)
  digits = @national
  clamped = visible.clamp(0, digits.length)
  masked_count = digits.length - clamped
  "+#{@country_code}#{'*' * masked_count}#{digits[masked_count..]}"
end

#mobile?Boolean

Whether the number is a mobile line.

Returns:

  • (Boolean)


123
124
125
# File 'lib/philiprehberger/phone.rb', line 123

def mobile?
  phone_type == :mobile
end

#premium?Boolean

Whether the number is a premium-rate line.

Returns:

  • (Boolean)


144
145
146
# File 'lib/philiprehberger/phone.rb', line 144

def premium?
  phone_type == :premium
end

#similar_to?(other) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/philiprehberger/phone.rb', line 89

def similar_to?(other)
  return false unless other.is_a?(PhoneNumber)

  e164 == other.e164
end

#to_hHash

Hash representation including all derived attributes.

Returns:

  • (Hash)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/philiprehberger/phone.rb', line 98

def to_h
  {
    country_code: country_code,
    national: national,
    country: country,
    e164: e164,
    formatted: formatted,
    international: international,
    valid: valid?,
    phone_type: phone_type,
    area_code_info: area_code_info,
    carrier: carrier
  }
end

#to_sString

String representation (E.164).

Returns:

  • (String)


65
66
67
# File 'lib/philiprehberger/phone.rb', line 65

def to_s
  e164
end

#toll_free?Boolean

Whether the number is toll-free.

Returns:

  • (Boolean)


137
138
139
# File 'lib/philiprehberger/phone.rb', line 137

def toll_free?
  phone_type == :toll_free
end

#valid?Boolean

Whether the phone number matches the country length rules.

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
# File 'lib/philiprehberger/phone.rb', line 32

def valid?
  return false unless @country

  data = COUNTRIES[@country]
  return false unless data

  data[:lengths].include?(@national.length)
end