module Phony

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

Public Class Methods

[](cc) click to toggle source

Get the Country for the given CC.

Example:

us = Phony['1']
normalized_number = us.normalize number
# File lib/motion-phony/phony-base.rb, line 33
def [] cc
  @codes[cc]
end
format(phone_number, options = {}) click to toggle source

Formats a E164 number according to local customs.

# 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
Also aliased as: formatted
format!(phone_number, options = {}) click to toggle source
# 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
Also aliased as: formatted!
formatted(phone_number, options = {})
Alias for: format
formatted!(phone_number, options = {})
Alias for: format!
normalize(phone_number, options = {}) click to toggle source

Normalizes the given number.

Useful before inserting the number into a database.

# 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 = {}) click to toggle source
# 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 = {}) click to toggle source

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.

# File lib/motion-phony/phony-base.rb, line 83
def plausible? number, hints = {}
  @codes.plausible? number, hints
end
split(phone_number) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source

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

# File lib/motion-phony/phony-base.rb, line 90
def vanity? phone_number
  @codes.vanity? phone_number.dup
end
vanity_to_number(vanity_number) click to toggle source

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.

# File lib/motion-phony/phony-base.rb, line 97
def vanity_to_number vanity_number
  @codes.vanity_to_number vanity_number.dup
end