Class: Philiprehberger::Phone::PhoneNumber
- Inherits:
-
Object
- Object
- Philiprehberger::Phone::PhoneNumber
- Includes:
- AreaCodeLookup, CarrierLookup, PhoneTypeDetection
- Defined in:
- lib/philiprehberger/phone.rb
Instance Attribute Summary collapse
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#national ⇒ Object
readonly
Returns the value of attribute national.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #country_name ⇒ Object
- #e164 ⇒ Object
- #formatted ⇒ Object
-
#initialize(country_code:, national:, country:) ⇒ PhoneNumber
constructor
A new instance of PhoneNumber.
- #inspect ⇒ Object
- #international ⇒ Object
- #landline? ⇒ Boolean
- #masked(visible: 4) ⇒ Object
- #mobile? ⇒ Boolean
- #premium? ⇒ Boolean
- #similar_to?(other) ⇒ Boolean
- #to_h ⇒ Object
- #to_s ⇒ Object
- #toll_free? ⇒ Boolean
- #valid? ⇒ Boolean
Methods included from CarrierLookup
Methods included from AreaCodeLookup
Methods included from PhoneTypeDetection
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
#country ⇒ Object (readonly)
Returns the value of attribute country.
20 21 22 |
# File 'lib/philiprehberger/phone.rb', line 20 def country @country end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
20 21 22 |
# File 'lib/philiprehberger/phone.rb', line 20 def country_code @country_code end |
#national ⇒ Object (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_name ⇒ Object
86 87 88 |
# File 'lib/philiprehberger/phone.rb', line 86 def country_name COUNTRIES.dig(@country, :name) end |
#e164 ⇒ Object
38 39 40 |
# File 'lib/philiprehberger/phone.rb', line 38 def e164 "+#{@country_code}#{@national}" end |
#formatted ⇒ Object
42 43 44 |
# File 'lib/philiprehberger/phone.rb', line 42 def formatted format_national end |
#inspect ⇒ Object
106 107 108 |
# File 'lib/philiprehberger/phone.rb', line 106 def inspect "#<Philiprehberger::Phone::PhoneNumber #{e164} (#{country || 'unknown'})>" end |
#international ⇒ Object
46 47 48 |
# File 'lib/philiprehberger/phone.rb', line 46 def international "+#{@country_code} #{format_national}" end |
#landline? ⇒ 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
90 91 92 |
# File 'lib/philiprehberger/phone.rb', line 90 def mobile? phone_type == :mobile end |
#premium? ⇒ Boolean
102 103 104 |
# File 'lib/philiprehberger/phone.rb', line 102 def premium? phone_type == :premium end |
#similar_to?(other) ⇒ 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_h ⇒ Object
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_s ⇒ Object
50 51 52 |
# File 'lib/philiprehberger/phone.rb', line 50 def to_s e164 end |
#toll_free? ⇒ Boolean
98 99 100 |
# File 'lib/philiprehberger/phone.rb', line 98 def toll_free? phone_type == :toll_free end |
#valid? ⇒ 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 |