Class: NT54::PhoneNumber
- Inherits:
-
Object
- Object
- NT54::PhoneNumber
- Defined in:
- lib/nt54/phone_number.rb
Instance Attribute Summary collapse
-
#area_code ⇒ Object
Returns the value of attribute area_code.
-
#city ⇒ Object
Returns the value of attribute city.
-
#country_code ⇒ Object
Returns the value of attribute country_code.
-
#local_number ⇒ Object
Returns the value of attribute local_number.
-
#local_prefix ⇒ Object
Returns the value of attribute local_prefix.
-
#mobile ⇒ Object
Returns the value of attribute mobile.
-
#province_code ⇒ Object
Returns the value of attribute province_code.
-
#special ⇒ Object
Returns the value of attribute special.
-
#special_sequence ⇒ Object
Returns the value of attribute special_sequence.
Instance Method Summary collapse
- #area ⇒ Object
- #fix! ⇒ Object
- #format_international ⇒ Object
- #format_international_sms ⇒ Object
- #format_local ⇒ Object
- #format_national ⇒ Object
-
#initialize ⇒ PhoneNumber
constructor
A new instance of PhoneNumber.
- #mobile? ⇒ Boolean
- #real_length ⇒ Object
- #special? ⇒ Boolean
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ PhoneNumber
Returns a new instance of PhoneNumber.
7 8 9 10 11 |
# File 'lib/nt54/phone_number.rb', line 7 def initialize [:country_code, :area_code, :local_prefix, :local_number, :special_sequence].each do |var| instance_variable_set :"@#{var}", "" end end |
Instance Attribute Details
#area_code ⇒ Object
Returns the value of attribute area_code.
4 5 6 |
# File 'lib/nt54/phone_number.rb', line 4 def area_code @area_code end |
#city ⇒ Object
Returns the value of attribute city.
4 5 6 |
# File 'lib/nt54/phone_number.rb', line 4 def city @city end |
#country_code ⇒ Object
Returns the value of attribute country_code.
4 5 6 |
# File 'lib/nt54/phone_number.rb', line 4 def country_code @country_code end |
#local_number ⇒ Object
Returns the value of attribute local_number.
4 5 6 |
# File 'lib/nt54/phone_number.rb', line 4 def local_number @local_number end |
#local_prefix ⇒ Object
Returns the value of attribute local_prefix.
4 5 6 |
# File 'lib/nt54/phone_number.rb', line 4 def local_prefix @local_prefix end |
#mobile ⇒ Object
Returns the value of attribute mobile.
4 5 6 |
# File 'lib/nt54/phone_number.rb', line 4 def mobile @mobile end |
#province_code ⇒ Object
Returns the value of attribute province_code.
4 5 6 |
# File 'lib/nt54/phone_number.rb', line 4 def province_code @province_code end |
#special ⇒ Object
Returns the value of attribute special.
4 5 6 |
# File 'lib/nt54/phone_number.rb', line 4 def special @special end |
#special_sequence ⇒ Object
Returns the value of attribute special_sequence.
4 5 6 |
# File 'lib/nt54/phone_number.rb', line 4 def special_sequence @special_sequence end |
Instance Method Details
#area ⇒ Object
13 14 15 16 |
# File 'lib/nt54/phone_number.rb', line 13 def area @area ||= Area.get(area_code) rescue Ambry::NotFoundError end |
#fix! ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/nt54/phone_number.rb', line 63 def fix! if real_length == 3 && special_sequence == "" @special_sequence = "#{area_code}#{local_prefix}" @area_code = "" @local_prefix = "" return self end @country_code = "54" if @country_code == "" # If we didn't dial an area code, then we don't really know how long the # prefix/local numbers are, so fake it. unless area_code until @local_number.length >= @local_prefix.length @local_number.insert(0, @local_prefix.slice!(-1)) end end self end |
#format_international ⇒ Object
31 32 33 |
# File 'lib/nt54/phone_number.rb', line 31 def format_international "+#{country_code} #{mobile? ? 9 : ''} (#{area_code}) #{local_prefix}-#{local_number}".squeeze(' ') end |
#format_international_sms ⇒ Object
35 36 37 |
# File 'lib/nt54/phone_number.rb', line 35 def format_international_sms "+#{country_code} (#{area_code}) #{local_prefix}-#{local_number}" end |
#format_local ⇒ Object
43 44 45 |
# File 'lib/nt54/phone_number.rb', line 43 def format_local "#{mobile? ? 15 : ''} #{local_prefix}-#{local_number}".squeeze(' ') end |
#format_national ⇒ Object
39 40 41 |
# File 'lib/nt54/phone_number.rb', line 39 def format_national "(0#{area_code}) #{mobile? ? 15 : ''} #{local_prefix}-#{local_number}".squeeze(' ') end |
#mobile? ⇒ Boolean
18 19 20 |
# File 'lib/nt54/phone_number.rb', line 18 def mobile? !! mobile end |
#real_length ⇒ Object
47 48 49 |
# File 'lib/nt54/phone_number.rb', line 47 def real_length area_code.length + local_prefix.length + local_number.length + special_sequence.length end |
#special? ⇒ Boolean
27 28 29 |
# File 'lib/nt54/phone_number.rb', line 27 def special? !! special end |
#valid? ⇒ Boolean
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/nt54/phone_number.rb', line 51 def valid? if special? && real_length == 3 return true end return false unless area_code return false unless area return false unless local_prefix.length >= 2 return false unless local_number.length >= 3 return false unless real_length == 10 true end |