hackedteam/rcs-db

View on GitHub
lib/rcs-db/country_calling_codes.rb

Summary

Maintainability
A
25 mins
Test Coverage
module RCS
  module DB
    module CountryCallingCodes

      # List of all the calling codes of all the countries of the world.
      # @see https://en.wikipedia.org/wiki/List_of_country_calling_codes#Alphabetical_listing_by_country_or_region
      CALLING_CODES = [
        "+7 840", "+7 940", "+995 44", "+93", "+358 18", "+355", "+213", "+1 684", "+376", "+244", "+1 264",
        "+1 268", "+54", "+374", "+297", "+247", "+61", "+672", "+43", "+994", "+1 242", "+973", "+880", "+1 246",
        "+375", "+32", "+501", "+229", "+1 441", "+975", "+591", "+599 7", "+387", "+267", "+55", "+246", "+1 284",
        "+673", "+359", "+226", "+95", "+257", "+855", "+237", "+1", "+238", "+1 345", "+236", "+235", "+64", "+56",
        "+86", "+57", "+269", "+242", "+243", "+682", "+506", "+225", "+385", "+53", "+53 99", "+599 9", "+357",
        "+420", "+45", "+253", "+1 767", "+1 809", "+1 829", "+1 849", "+670", "+593", "+20", "+503", "+882 13",
        "+240", "+291", "+372", "+251", "+500", "+298", "+679", "+358", "+33", "+596", "+594", "+689", "+241", "+220",
        "+995", "+49", "+233", "+350", "+881", "+30", "+299", "+1 473", "+590", "+1 671", "+502", "+44", "+224", "+245",
        "+592", "+509", "+504", "+852", "+36", "+354", "+91", "+62", "+870", "+800", "+808", "+98", "+964", "+353", "+972",
        "+39", "+1 876", "+47 79", "+81", "+962", "+7 6", "+7 7", "+254", "+686", "+850", "+82", "+965", "+996", "+856",
        "+371", "+961", "+266", "+231", "+218", "+423", "+370", "+352", "+853", "+389", "+261", "+265", "+60", "+960",
        "+223", "+356", "+692", "+222", "+230", "+262", "+52", "+691", "+1 808", "+373", "+377", "+976", "+382", "+1 664",
        "+212", "+258", "+264", "+674", "+977", "+31", "+1 869", "+687", "+505", "+227", "+234", "+683", "+1 670", "+47",
        "+968", "+92", "+680", "+970", "+507", "+675", "+595", "+51", "+63", "+48", "+351", "+1 787", "+1 939", "+974",
        "+40", "+7", "+250", "+599 4", "+290", "+1 758", "+508", "+1 784", "+685", "+378", "+239", "+966", "+221", "+381",
        "+248", "+232", "+65", "+599 3", "+1 721", "+421", "+386", "+677", "+252", "+27", "+995 34", "+211", "+34", "+94",
        "+249", "+597", "+268", "+46", "+41", "+963", "+886", "+992", "+255", "+66", "+882 16", "+228", "+690", "+676",
        "+1 868", "+290 8", "+216", "+90", "+993", "+1 649", "+688", "+256", "+380", "+971", "+878", "+598", "+1 340",
        "+998", "+678", "+58", "+39 066", "+379", "+84", "+681", "+967", "+260", "+263", "+881 2", "+881 3", "+881 8",
        "+881 9", "+881 0", "+881 1", "+881 6", "+881 7"
      ]

      # Caches an array of all the calling codes sorted from the biggest one to
      # the smaller one.
      def self.calling_codes_search_ary
        @@calling_codes_search_hash ||= begin
          CALLING_CODES.sort { |v1, v2| v2.gsub(/[^0-9]/, '').to_i <=> v1.gsub(/[^0-9]/, '').to_i }
        end
      end

      # If the given number starts with a calling code, return the given number without the
      # calling code, otherwise returns the given number as is.
      def self.number_without_calling_code(number)
        parsed = number.to_s.strip
        twochrs = parsed[0..1]

        if twochrs[0] != '+' and twochrs != '00'
          return parsed
        else
          calling_codes_search_ary.each do |code|
            regexp = /^(\+|00)(#{code.gsub('+', '')}|#{code.gsub(/[^0-9]/, '')})(.+)/
            parts = parsed.scan(regexp).flatten
            return parts.last.strip if parts.any?
          end
        end

        parsed
      end
    end
  end
end