Class: Phoner::Phone

Inherits:
Object show all
Defined in:
lib/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

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_codeObject

Returns the value of attribute area_code.



17
18
19
# File 'lib/phone.rb', line 17

def area_code
  @area_code
end

#countryObject

Returns the value of attribute country.



17
18
19
# File 'lib/phone.rb', line 17

def country
  @country
end

#country_codeObject

Returns the value of attribute country_code.



17
18
19
# File 'lib/phone.rb', line 17

def country_code
  @country_code
end

#extensionObject

Returns the value of attribute extension.



17
18
19
# File 'lib/phone.rb', line 17

def extension
  @extension
end

#numberObject

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

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/phone.rb', line 90

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)



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, 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

  parts = split_to_parts(normalized, options)

  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, options={})
    parse(string, options.merge(:raise_exception_on_error => true))
end

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

is this string a valid phone number?

Returns:

  • (Boolean)


82
83
84
85
86
87
88
# File 'lib/phone.rb', line 82

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



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_longObject



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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


146
147
148
# File 'lib/phone.rb', line 146

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

#number1Object

first n characters of :number



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

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

#number2Object

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_sObject

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



185
186
187
# File 'lib/phone.rb', line 185

def to_s
  format(:default)
end