Class: Phoner::Phone
Constant Summary collapse
- @@n1_length =
default length of first number part
3
- @@named_formats =
{ :default => "+%c%a%n", :default_with_extension => "+%c%a%nx%x", :europe => '+%c (0) %a %f %l', :us => "(%a) %f-%l" }
Instance Attribute Summary collapse
-
#area_code ⇒ Object
Returns the value of attribute area_code.
-
#country ⇒ Object
Returns the value of attribute country.
-
#country_code ⇒ Object
Returns the value of attribute country_code.
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#number ⇒ Object
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
-
#format(fmt) ⇒ Object
Formats the phone number.
-
#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(*hash_or_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”.
Constructor Details
#initialize(*hash_or_args) ⇒ Phone
Returns a new instance of Phone.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/phone.rb', line 35 def initialize(*hash_or_args) if hash_or_args.first.is_a?(Hash) hash_or_args = hash_or_args.first keys = {:country => :country, :number => :number, :area_code => :area_code, :country_code => :country_code, :extension => :extension} else keys = {:number => 0, :area_code => 1, :country_code => 2, :extension => 3, :country => 4} end self.number = hash_or_args[ keys[:number] ] self.area_code = hash_or_args[ keys[:area_code] ] || self.default_area_code self.country_code = hash_or_args[ keys[:country_code] ] || self.default_country_code self.extension = hash_or_args[ keys[:extension] ] self.country = hash_or_args[ keys[:country] ] # Santity checks raise "Must enter number" if self.number.blank? raise "Must enter area code or set default area code" if self.area_code.blank? raise "Must enter country code or set default country code" if self.country_code.blank? end |
Instance Attribute Details
#area_code ⇒ Object
Returns the value of attribute area_code.
17 18 19 |
# File 'lib/phone.rb', line 17 def area_code @area_code end |
#country ⇒ Object
Returns the value of attribute country.
17 18 19 |
# File 'lib/phone.rb', line 17 def country @country end |
#country_code ⇒ Object
Returns the value of attribute country_code.
17 18 19 |
# File 'lib/phone.rb', line 17 def country_code @country_code end |
#extension ⇒ Object
Returns the value of attribute extension.
17 18 19 |
# File 'lib/phone.rb', line 17 def extension @extension end |
#number ⇒ Object
Returns the value of attribute number.
17 18 19 |
# File 'lib/phone.rb', line 17 def number @number end |
Class Method Details
.is_mobile?(string, options = {}) ⇒ Boolean
90 91 92 93 94 |
# File 'lib/phone.rb', line 90 def self.is_mobile?(string, = {}) pn = parse(string, ) return false if pn.nil? 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)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/phone.rb', line 61 def self.parse(string, ={}) return nil unless string.present? Country.load extension = extract_extension(string) normalized = normalize(string) [:country_code] ||= self.default_country_code [:area_code] ||= self.default_area_code parts = split_to_parts(normalized, ) pn = Phone.new(parts) if parts if pn.present? and extension.present? pn.extension = extension end pn end |
.parse!(string, options = {}) ⇒ Object
55 56 57 |
# File 'lib/phone.rb', line 55 def self.parse!(string, ={}) parse(string, .merge(:raise_exception_on_error => true)) end |
.valid?(string, options = {}) ⇒ Boolean
is this string a valid phone number?
82 83 84 85 86 87 88 |
# File 'lib/phone.rb', line 82 def self.valid?(string, = {}) begin parse(string, ).present? rescue false # don't raise exceptions on parse errors end end |
Instance Method Details
#==(other) ⇒ Object
comparison of 2 phone objects
200 201 202 203 |
# File 'lib/phone.rb', line 200 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
140 141 142 |
# File 'lib/phone.rb', line 140 def area_code_long "0" + area_code if area_code end |
#format(fmt) ⇒ Object
Formats the phone number.
if the method argument is a String, it is used as a format string, with the following fields being interpolated:
-
%c - country_code (385)
-
%a - area_code (91)
-
%A - area_code with leading zero (091)
-
%n - number (5125486)
-
%f - first @@n1_length characters of number (configured through Phone.n1_length), default is 3 (512)
-
%l - last characters of number (5486)
-
%x - entire extension
if the method argument is a Symbol, it is used as a lookup key for a format String in Phone.named_formats
pn.format(:europe)
175 176 177 178 179 180 181 182 |
# File 'lib/phone.rb', line 175 def format(fmt) if fmt.is_a?(Symbol) raise "The format #{fmt} doesn't exist'" unless named_formats.has_key?(fmt) format_number named_formats[fmt] else format_number(fmt) end end |
#has_default_area_code? ⇒ Boolean
does this number belong to the default area code?
195 196 197 |
# File 'lib/phone.rb', line 195 def has_default_area_code? area_code == self.class.default_area_code end |
#has_default_country_code? ⇒ Boolean
does this number belong to the default country code?
190 191 192 |
# File 'lib/phone.rb', line 190 def has_default_country_code? country_code == self.class.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.
146 147 148 |
# File 'lib/phone.rb', line 146 def is_mobile? country.is_mobile? "#{area_code}#{number}" end |
#number1 ⇒ Object
first n characters of :number
151 152 153 |
# File 'lib/phone.rb', line 151 def number1 number[0...self.class.n1_length] end |
#number2 ⇒ Object
everything left from number after the first n characters (see number1)
156 157 158 159 |
# File 'lib/phone.rb', line 156 def number2 n2_length = number.size - self.class.n1_length number[-n2_length, n2_length] end |
#to_s ⇒ Object
the default format is “+%c%a%n”
185 186 187 |
# File 'lib/phone.rb', line 185 def to_s format(:default) end |