Class: Phonie::Phone
- Inherits:
-
Object
- Object
- Phonie::Phone
- Defined in:
- lib/phonie/phone.rb
Constant Summary collapse
- EXTENSION =
/[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(*([-0-9]{1,})\)*#?$/i
Instance Attribute Summary collapse
-
#area_code ⇒ Object
readonly
Returns the value of attribute area_code.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Class Method Summary collapse
- .is_mobile?(string, options = {}) ⇒ Boolean
-
.parse(string, options = {}) ⇒ Object
create a new phone number by parsing a string the format of the string is detect automatically (from FORMATS).
- .parse!(string, options = {}) ⇒ Object
-
.valid?(string, options = {}) ⇒ Boolean
is this string a valid phone number?.
Instance Method Summary collapse
-
#==(other) ⇒ Object
comparison of 2 phone objects.
- #area_code_long ⇒ Object
- #extension_with_prefix ⇒ Object
- #format(fmt) ⇒ Object
-
#has_default_area_code? ⇒ Boolean
does this number belong to the default area code?.
-
#has_default_country_code? ⇒ Boolean
does this number belong to the default country code?.
-
#initialize(*args) ⇒ Phone
constructor
A new instance of Phone.
-
#is_mobile? ⇒ Boolean
For many countries it’s not apparent from the number Will return false positives rather than false negatives.
-
#number1 ⇒ Object
first n characters of :number.
-
#number2 ⇒ Object
everything left from number after the first n characters (see number1).
-
#to_s ⇒ Object
the default format is “+%c%a%n”.
- #valid? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Phone
Returns a new instance of Phone.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/phonie/phone.rb', line 19 def initialize(*args) params = normalize_args(args) @number = params[:number] @area_code = params[:area_code] @country_code = params[:country_code] @extension = params[:extension] @country = params[:country] @errors = {} end |
Instance Attribute Details
#area_code ⇒ Object (readonly)
Returns the value of attribute area_code.
17 18 19 |
# File 'lib/phonie/phone.rb', line 17 def area_code @area_code end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
17 18 19 |
# File 'lib/phonie/phone.rb', line 17 def country @country end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
17 18 19 |
# File 'lib/phonie/phone.rb', line 17 def country_code @country_code end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
17 18 19 |
# File 'lib/phonie/phone.rb', line 17 def errors @errors end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
17 18 19 |
# File 'lib/phonie/phone.rb', line 17 def extension @extension end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
17 18 19 |
# File 'lib/phonie/phone.rb', line 17 def number @number end |
Class Method Details
.is_mobile?(string, options = {}) ⇒ Boolean
63 64 65 66 |
# File 'lib/phonie/phone.rb', line 63 def self.is_mobile?(string, = {}) pn = parse(string, ) pn && pn.is_mobile? end |
.parse(string, options = {}) ⇒ Object
create a new phone number by parsing a string the format of the string is detect automatically (from FORMATS)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/phonie/phone.rb', line 38 def self.parse(string, = {}) return if string.nil? string = string.to_s [:country_code] ||= Phonie.configuration.default_country_code [:area_code] ||= Phonie.configuration.default_area_code extension = extract_extension(string) normalized = normalize(string) return unless country = Country.detect(normalized, [:country_code], [:area_code]) parts = country.parse(normalized, [:area_code]) parts[:country] = country parts[:country_code] = country.country_code parts[:extension] = extension new(parts) end |
.parse!(string, options = {}) ⇒ Object
30 31 32 33 34 |
# File 'lib/phonie/phone.rb', line 30 def self.parse!(string, = {}) phone_number = parse(string, ) raise ArgumentError.new("#{string} is not a valid phone number") unless phone_number && phone_number.valid? phone_number end |
.valid?(string, options = {}) ⇒ Boolean
is this string a valid phone number?
58 59 60 61 |
# File 'lib/phonie/phone.rb', line 58 def self.valid?(string, = {}) pn = parse(string, ) pn && pn.valid? end |
Instance Method Details
#==(other) ⇒ Object
comparison of 2 phone objects
119 120 121 122 |
# File 'lib/phonie/phone.rb', line 119 def ==(other) methods = [:country_code, :area_code, :number, :extension] methods.all? { |method| other.respond_to?(method) && send(method) == other.send(method) } end |
#area_code_long ⇒ Object
68 69 70 |
# File 'lib/phonie/phone.rb', line 68 def area_code_long "0" + area_code if area_code end |
#extension_with_prefix ⇒ Object
89 90 91 92 |
# File 'lib/phonie/phone.rb', line 89 def extension_with_prefix return unless extension [' x', extension ].join end |
#format(fmt) ⇒ Object
94 95 96 |
# File 'lib/phonie/phone.rb', line 94 def format(fmt) Formatter.new(format: fmt, phone_number: self).to_s end |
#has_default_area_code? ⇒ Boolean
does this number belong to the default area code?
109 110 111 |
# File 'lib/phonie/phone.rb', line 109 def has_default_area_code? area_code == Phonie.configuration.default_area_code end |
#has_default_country_code? ⇒ Boolean
does this number belong to the default country code?
104 105 106 |
# File 'lib/phonie/phone.rb', line 104 def has_default_country_code? country_code == Phonie.configuration.default_country_code end |
#is_mobile? ⇒ Boolean
For many countries it’s not apparent from the number Will return false positives rather than false negatives.
74 75 76 |
# File 'lib/phonie/phone.rb', line 74 def is_mobile? country.is_mobile? "#{area_code}#{number}" end |
#number1 ⇒ Object
first n characters of :number
79 80 81 |
# File 'lib/phonie/phone.rb', line 79 def number1 number[0...Phonie.configuration.n1_length] end |
#number2 ⇒ Object
everything left from number after the first n characters (see number1)
84 85 86 87 |
# File 'lib/phonie/phone.rb', line 84 def number2 n2_length = number.size - Phonie.configuration.n1_length number[-n2_length, n2_length] end |
#to_s ⇒ Object
the default format is “+%c%a%n”
99 100 101 |
# File 'lib/phonie/phone.rb', line 99 def to_s format(:default) end |
#valid? ⇒ Boolean
113 114 115 116 |
# File 'lib/phonie/phone.rb', line 113 def valid? validate errors.empty? end |