Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 121 lines (107 sloc) 4.165 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
# Framework.
#
require File.expand_path '../phony/vanity', __FILE__
require File.expand_path '../phony/local_splitters/fixed', __FILE__
require File.expand_path '../phony/local_splitters/regex', __FILE__
require File.expand_path '../phony/national_splitters/dsl', __FILE__
require File.expand_path '../phony/national_splitters/fixed', __FILE__
require File.expand_path '../phony/national_splitters/variable', __FILE__
require File.expand_path '../phony/national_splitters/regex', __FILE__
require File.expand_path '../phony/national_splitters/default', __FILE__
require File.expand_path '../phony/national_splitters/none', __FILE__
require File.expand_path '../phony/national_code', __FILE__
require File.expand_path '../phony/country', __FILE__
require File.expand_path '../phony/country_codes', __FILE__
require File.expand_path '../phony/validator', __FILE__
require File.expand_path '../phony/validators', __FILE__
require File.expand_path '../phony/dsl', __FILE__

# Countries.
#
# The ones that need more space to define.
#
require File.expand_path '../phony/countries/austria', __FILE__
require File.expand_path '../phony/countries/china', __FILE__
require File.expand_path '../phony/countries/germany', __FILE__
require File.expand_path '../phony/countries/ireland', __FILE__
require File.expand_path '../phony/countries/italy', __FILE__
require File.expand_path '../phony/countries/malaysia', __FILE__
require File.expand_path '../phony/countries/netherlands', __FILE__
require File.expand_path '../phony/countries/russia_kazakhstan_abhasia_south_osetia', __FILE__
require File.expand_path '../phony/countries/south_korea', __FILE__
require File.expand_path '../phony/countries/sweden', __FILE__
require File.expand_path '../phony/countries/united_kingdom', __FILE__
#
# All other countries.
#
require File.expand_path '../phony/countries', __FILE__

module Phony

  # Phony uses a single country codes instance.
  #
  @codes = CountryCodes.instance
  @validators = Validators.instance

  class << self

    # Normalizes the given number.
    #
    # Useful before inserting the number into a database.
    #
    def normalize phone_number
      raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.normalize(number)." unless phone_number
      normalize! phone_number.dup
    end
    def normalize! phone_number
      @codes.normalize phone_number
    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
      formatted! phone_number.dup, options
    end
    def format! phone_number, options = {}
      @codes.formatted 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 = {}
      @validators.plausible? number, hints
    end

    # def service? number
    # @codes.service? number.dup
    # end
    # def mobile? number
    # @codes.mobile? number.dup
    # end
    # def landline? number
    # @codes.landline? number.dup
    # 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
Something went wrong with that request. Please try again.