Class: Phoner::Phone

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*hash_or_args) ⇒ 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_codeObject

Returns the value of attribute area_code.



14
15
16
# File 'lib/phoner/phone.rb', line 14

def area_code
  @area_code
end

#countryObject

Returns the value of attribute country.



14
15
16
# File 'lib/phoner/phone.rb', line 14

def country
  @country
end

#country_codeObject

Returns the value of attribute country_code.



14
15
16
# File 'lib/phoner/phone.rb', line 14

def country_code
  @country_code
end

#extensionObject

Returns the value of attribute extension.



14
15
16
# File 'lib/phoner/phone.rb', line 14

def extension
  @extension
end

#numberObject

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



94
95
96
97
98
# File 'lib/phoner/phone.rb', line 94

def self.is_mobile?(string, options = {})
  pn = parse(string, options)
  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
# File 'lib/phoner/phone.rb', line 58

def self.parse(string, options={})
  return nil unless string.present?

  Country.load

  extension = extract_extension(string)
  normalized = normalize(string)

  options[:country_code] ||= self.default_country_code
  options[:area_code] ||= self.default_area_code

  if options[:country_code].is_a?(Array)
    options[:country_code].find do |country_code|
      parts = split_to_parts(normalized, options.merge(:country_code => country_code))
      return parts if parts
    end
  else
    parts = split_to_parts(normalized, options)
  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, options={})
    parse(string, options.merge(:raise_exception_on_error => true))
end

.valid?(string, options = {}) ⇒ Boolean

is this string a valid phone number?



86
87
88
89
90
91
92
# File 'lib/phoner/phone.rb', line 86

def self.valid?(string, options = {})
  begin
    parse(string, options).present?
  rescue
    false  # don't raise exceptions on parse errors
  end
end

Instance Method Details

#==(other) ⇒ Object

comparison of 2 phone objects



204
205
206
207
# File 'lib/phoner/phone.rb', line 204

def ==(other)
  methods = [:country_code, :area_code, :number, :extension]
  methods.all? { |method| other.respond_to?(method) && send(method) == other.send(method) }
end

#area_code_longObject



144
145
146
# File 'lib/phoner/phone.rb', line 144

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)


179
180
181
182
183
184
185
186
# File 'lib/phoner/phone.rb', line 179

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?



199
200
201
# File 'lib/phoner/phone.rb', line 199

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?



194
195
196
# File 'lib/phoner/phone.rb', line 194

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.



150
151
152
# File 'lib/phoner/phone.rb', line 150

def is_mobile?
  country.is_mobile? "#{area_code}#{number}"
end

#number1Object

first n characters of :number



155
156
157
# File 'lib/phoner/phone.rb', line 155

def number1
  number[0...self.class.n1_length]
end

#number2Object

everything left from number after the first n characters (see number1)



160
161
162
163
# File 'lib/phoner/phone.rb', line 160

def number2
  n2_length = number.size - self.class.n1_length
  number[-n2_length, n2_length]
end

#to_sObject

the default format is “+%c%a%n”



189
190
191
# File 'lib/phoner/phone.rb', line 189

def to_s
  format(:default)
end