Module: Phony

Defined in:
lib/motion-phony/phony-base.rb

Overview

Basically this is just github.com/floere/phony/blob/master/lib/phony.rb without the dependencies. Needs regular updates

Defined Under Namespace

Classes: CountryCodes, NormalizationError

Class Method Summary collapse

Class Method Details

.[](cc) ⇒ Object

Get the Country for the given CC.

Example:

us = Phony['1']
normalized_number = us.normalize number


33
34
35
# File 'lib/motion-phony/phony-base.rb', line 33

def [] cc
  @codes[cc]
end

.format(phone_number, options = {}) ⇒ Object Also known as: formatted

Formats a E164 number according to local customs.

Raises:

  • (ArgumentError)


65
66
67
68
# File 'lib/motion-phony/phony-base.rb', line 65

def format phone_number, options = {}
  raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.format(number)." unless phone_number
  format! phone_number.dup, options
end

.format!(phone_number, options = {}) ⇒ Object Also known as: formatted!



69
70
71
72
73
# File 'lib/motion-phony/phony-base.rb', line 69

def format! phone_number, options = {}
  #@codes.format phone_number, options
  # Strangely the Phony::CountryCodes.format method becomes private
  @codes.format_substitute phone_number, options
end

.normalize(phone_number, options = {}) ⇒ Object

Normalizes the given number.

Useful before inserting the number into a database.

Raises:

  • (ArgumentError)


41
42
43
44
# File 'lib/motion-phony/phony-base.rb', line 41

def normalize phone_number, options = {}
  raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.normalize(number)." unless phone_number
  normalize! phone_number.dup, options
end

.normalize!(phone_number, options = {}) ⇒ Object



45
46
47
48
49
# File 'lib/motion-phony/phony-base.rb', line 45

def normalize! phone_number, options = {}
  @codes.normalize phone_number, options
rescue
  raise NormalizationError.new
end

.plausible?(number, hints = {}) ⇒ Boolean

Makes a plausibility check.

If it returns false, it is not plausible. If it returns true, it is unclear whether it is plausible, leaning towards being plausible.

Returns:

  • (Boolean)


83
84
85
# File 'lib/motion-phony/phony-base.rb', line 83

def plausible? number, hints = {}
  @codes.plausible? number, hints
end

.split(phone_number) ⇒ Object

Splits the phone number into pieces according to the country codes.

Raises:

  • (ArgumentError)


53
54
55
56
# File 'lib/motion-phony/phony-base.rb', line 53

def split phone_number
  raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.split(number)." unless phone_number
  split! phone_number.dup
end

.split!(phone_number) ⇒ Object



57
58
59
60
61
# File 'lib/motion-phony/phony-base.rb', line 57

def split! phone_number
  parts = @codes.split phone_number
  parts.delete_at 1
  parts
end

.vanity?(phone_number) ⇒ Boolean

Returns true if there is a character in the number after the first four numbers.

Returns:

  • (Boolean)


90
91
92
# File 'lib/motion-phony/phony-base.rb', line 90

def vanity? phone_number
  @codes.vanity? phone_number.dup
end

.vanity_to_number(vanity_number) ⇒ Object

Converts any character in the vanity_number to its numeric representation. Does not check if the passed number is a valid vanity_number, simply does replacement.



97
98
99
# File 'lib/motion-phony/phony-base.rb', line 97

def vanity_to_number vanity_number
  @codes.vanity_to_number vanity_number.dup
end