module Phony
Basically this is just github.com/floere/phony/blob/master/lib/phony.rb without the dependencies. Needs regular updates
Public Class Methods
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
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
# 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
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
# File lib/motion-phony/phony-base.rb, line 45 def normalize! phone_number, options = {} @codes.normalize phone_number, options rescue raise NormalizationError.new end
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
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
# File lib/motion-phony/phony-base.rb, line 57 def split! phone_number parts = @codes.split phone_number parts.delete_at 1 parts end
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
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