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) ⇒ Boolean
Equality based on E.164 representation.
-
#country_name ⇒ String?
Human-readable country name (e.g. “United States”).
-
#e164 ⇒ String
E.164-formatted phone number (e.g. “+15551234567”).
-
#formatted ⇒ String
National (country-specific) formatted representation.
-
#initialize(country_code:, national:, country:) ⇒ PhoneNumber
constructor
A new instance of PhoneNumber.
- #inspect ⇒ Object
-
#international ⇒ String
International formatted representation with country code prefix.
-
#landline? ⇒ Boolean
Whether the number is a landline.
-
#masked(visible: 4) ⇒ String
E.164 form with national digits masked as ‘*` except the last `visible` digits; country code remains visible.
-
#mobile? ⇒ Boolean
Whether the number is a mobile line.
-
#premium? ⇒ Boolean
Whether the number is a premium-rate line.
- #similar_to?(other) ⇒ Boolean
-
#to_h ⇒ Hash
Hash representation including all derived attributes.
-
#to_s ⇒ String
String representation (E.164).
-
#toll_free? ⇒ Boolean
Whether the number is toll-free.
-
#valid? ⇒ Boolean
Whether the phone number matches the country length rules.
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) ⇒ Boolean
Equality based on E.164 representation.
85 86 87 |
# File 'lib/philiprehberger/phone.rb', line 85 def ==(other) other.is_a?(PhoneNumber) && e164 == other.e164 end |
#country_name ⇒ String?
Human-readable country name (e.g. “United States”).
116 117 118 |
# File 'lib/philiprehberger/phone.rb', line 116 def country_name COUNTRIES.dig(@country, :name) end |
#e164 ⇒ String
E.164-formatted phone number (e.g. “+15551234567”).
44 45 46 |
# File 'lib/philiprehberger/phone.rb', line 44 def e164 "+#{@country_code}#{@national}" end |
#formatted ⇒ String
National (country-specific) formatted representation.
51 52 53 |
# File 'lib/philiprehberger/phone.rb', line 51 def formatted format_national end |
#inspect ⇒ Object
148 149 150 |
# File 'lib/philiprehberger/phone.rb', line 148 def inspect "#<Philiprehberger::Phone::PhoneNumber #{e164} (#{country || 'unknown'})>" end |
#international ⇒ String
International formatted representation with country code prefix.
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.
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.
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.
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.
144 145 146 |
# File 'lib/philiprehberger/phone.rb', line 144 def premium? phone_type == :premium end |
#similar_to?(other) ⇒ 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_h ⇒ Hash
Hash representation including all derived attributes.
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_s ⇒ String
String representation (E.164).
65 66 67 |
# File 'lib/philiprehberger/phone.rb', line 65 def to_s e164 end |
#toll_free? ⇒ Boolean
Whether the number is toll-free.
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.
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 |