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) ⇒ Object



61
62
63
# File 'lib/philiprehberger/phone.rb', line 61

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

#country_nameObject



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

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

#e164Object



38
39
40
# File 'lib/philiprehberger/phone.rb', line 38

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

#formattedObject



42
43
44
# File 'lib/philiprehberger/phone.rb', line 42

def formatted
  format_national
end

#inspectObject



106
107
108
# File 'lib/philiprehberger/phone.rb', line 106

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

#internationalObject



46
47
48
# File 'lib/philiprehberger/phone.rb', line 46

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

#landline?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/philiprehberger/phone.rb', line 94

def landline?
  phone_type == :landline
end

#masked(visible: 4) ⇒ Object



54
55
56
57
58
59
# File 'lib/philiprehberger/phone.rb', line 54

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

Returns:

  • (Boolean)


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

def mobile?
  phone_type == :mobile
end

#premium?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/philiprehberger/phone.rb', line 102

def premium?
  phone_type == :premium
end

#similar_to?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  e164 == other.e164
end

#to_hObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/philiprehberger/phone.rb', line 71

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_sObject



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

def to_s
  e164
end

#toll_free?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/philiprehberger/phone.rb', line 98

def toll_free?
  phone_type == :toll_free
end

#valid?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
# File 'lib/philiprehberger/phone.rb', line 29

def valid?
  return false unless @country

  data = COUNTRIES[@country]
  return false unless data

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