# Basically this is just # https://github.com/floere/phony/blob/master/lib/phony.rb # without the dependencies. # Needs regular updates module Phony class NormalizationError < StandardError def initialize super %Q{Phony could not normalize the given number. Is it a phone number?} end end # Phony uses a single country codes instance. # @codes = CountryCodes.instance class << self # Get the Country for the given CC. # # Example: # us = Phony['1'] # normalized_number = us.normalize number # def [] cc @codes[cc] end # Normalizes the given number. # # Useful before inserting the number into a database. # 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 def normalize! phone_number, options = {} @codes.normalize phone_number, options rescue raise NormalizationError.new end # Splits the phone number into pieces according to the country codes. # 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 def split! phone_number parts = @codes.split phone_number parts.delete_at 1 parts end # Formats a E164 number according to local customs. # 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 def format! phone_number, options = {} @codes.format phone_number, options end alias formatted format alias formatted! format! # 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. # def plausible? number, hints = {} @codes.plausible? number, hints end # Returns true if there is a character in the number # after the first four numbers. # 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. # def vanity_to_number vanity_number @codes.vanity_to_number vanity_number.dup end end end