Class: Phonie::Phone

Inherits:
Object
  • Object
show all
Defined in:
lib/phonie/phone.rb

Constant Summary collapse

EXTENSION =
/[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(*([-0-9]{1,})\)*#?$/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_codeObject (readonly)

Returns the value of attribute area_code.



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

def area_code
  @area_code
end

#countryObject (readonly)

Returns the value of attribute country.



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

def country
  @country
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



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

def country_code
  @country_code
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#extensionObject (readonly)

Returns the value of attribute extension.



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

def extension
  @extension
end

#numberObject (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

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/phonie/phone.rb', line 63

def self.is_mobile?(string, options = {})
  pn = parse(string, options)
  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, options = {})
  return if string.nil?

  string = string.to_s

  options[:country_code] ||= Phonie.configuration.default_country_code
  options[:area_code]    ||= Phonie.configuration.default_area_code

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

  return unless country = Country.detect(normalized, options[:country_code], options[:area_code])
  parts = country.parse(normalized, options[:area_code])
  parts[:country] = country
  parts[:country_code] = country.country_code
  parts[:extension] = extension
  new(parts)
end

.parse!(string, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
# File 'lib/phonie/phone.rb', line 30

def self.parse!(string, options = {})
  phone_number = parse(string, options)
  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?

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/phonie/phone.rb', line 58

def self.valid?(string, options = {})
  pn = parse(string, options)
  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_longObject



68
69
70
# File 'lib/phonie/phone.rb', line 68

def area_code_long
  "0" + area_code if area_code
end

#extension_with_prefixObject



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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


74
75
76
# File 'lib/phonie/phone.rb', line 74

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

#number1Object

first n characters of :number



79
80
81
# File 'lib/phonie/phone.rb', line 79

def number1
  number[0...Phonie.configuration.n1_length]
end

#number2Object

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_sObject

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

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/phonie/phone.rb', line 113

def valid?
  validate
  errors.empty?
end