Class: Phoner::Phone
- Inherits:
-
Object
- Object
- Phoner::Phone
- Defined in:
- lib/phoner/phone.rb
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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/phoner/phone.rb', line 32 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.
14 15 16 |
# File 'lib/phoner/phone.rb', line 14 def area_code @area_code end |
#country ⇒ Object
Returns the value of attribute country.
14 15 16 |
# File 'lib/phoner/phone.rb', line 14 def country @country end |
#country_code ⇒ Object
Returns the value of attribute country_code.
14 15 16 |
# File 'lib/phoner/phone.rb', line 14 def country_code @country_code end |
#extension ⇒ Object
Returns the value of attribute extension.
14 15 16 |
# File 'lib/phoner/phone.rb', line 14 def extension @extension end |
#number ⇒ Object
Returns the value of attribute number.
14 15 16 |
# File 'lib/phoner/phone.rb', line 14 def number @number end |
Class Method Details
.is_mobile?(string, options = {}) ⇒ Boolean
95 96 97 98 99 |
# File 'lib/phoner/phone.rb', line 95 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)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/phoner/phone.rb', line 58 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 = nil if [:country_code].is_a?(Array) [:country_code].find do |country_code| parts = split_to_parts(normalized, .merge(:country_code => country_code)) end else parts = split_to_parts(normalized, ) end pn = Phone.new(parts) if parts if pn.present? and extension.present? pn.extension = extension end pn end |
.parse!(string, options = {}) ⇒ Object
52 53 54 |
# File 'lib/phoner/phone.rb', line 52 def self.parse!(string, ={}) parse(string, .merge(:raise_exception_on_error => true)) end |
.valid?(string, options = {}) ⇒ Boolean
is this string a valid phone number?
87 88 89 90 91 92 93 |
# File 'lib/phoner/phone.rb', line 87 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
205 206 207 208 |
# File 'lib/phoner/phone.rb', line 205 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
145 146 147 |
# File 'lib/phoner/phone.rb', line 145 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)
180 181 182 183 184 185 186 187 |
# File 'lib/phoner/phone.rb', line 180 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?
200 201 202 |
# File 'lib/phoner/phone.rb', line 200 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?
195 196 197 |
# File 'lib/phoner/phone.rb', line 195 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.
151 152 153 |
# File 'lib/phoner/phone.rb', line 151 def is_mobile? country.is_mobile? "#{area_code}#{number}" end |
#number1 ⇒ Object
first n characters of :number
156 157 158 |
# File 'lib/phoner/phone.rb', line 156 def number1 number[0...self.class.n1_length] end |
#number2 ⇒ Object
everything left from number after the first n characters (see number1)
161 162 163 164 |
# File 'lib/phoner/phone.rb', line 161 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”
190 191 192 |
# File 'lib/phoner/phone.rb', line 190 def to_s format(:default) end |