module CogiPhony

Constants

VERSION

Public Class Methods

country_code_from_number(phone) click to toggle source

Extract country code from phone number.

@param [String] phone Phone number

@return [String] Country code @return [nil] if can not extract country code

@example

CogiPhony.country_code_from_number('84933081090') # =>  '84'
CogiPhony.country_code_from_number('0933081090')  # =>  nil
    # File lib/cogi_phony.rb
206 def self.country_code_from_number(phone)
207   return nil unless Phony.plausible?(phone)
208   Phony.split(Phony.normalize(phone)).first
209 end
country_codes_hash() click to toggle source
   # File lib/cogi_phony.rb
11 def self.country_codes_hash
12   @country_codes_hash ||= YAML.load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'data/country_codes.yaml'))
13 end
format(phone, options = {}) click to toggle source

Format formats phone numbers according to the predominant format of a country.

@param [String] phone Phone number @param [Hash] options An option hash

format: 'global' or 'vietnam'. Default is 'global'

@return [String] Phone number formatted based on each country.

@example

CogiPhony.format('84933081090', format: 'global')   # => '+84 93 3081090'
CogiPhony.format('84837621350', format: 'global')   # => '+84 8 3762 1350'
CogiPhony.format('84933081090')                     # =>'+84 93 3081090'
CogiPhony.format('14037089189')                     # => '+1 (403) 708-9189'
CogiPhony.format('84933081090', format: 'vietnam')  # => '093 3081090'
CogiPhony.format('84837621350', format: 'vietnam')  # => '08 3762 1350'
    # File lib/cogi_phony.rb
151 def self.format(phone, options = {})
152   return nil if phone.nil?
153 
154   phone = phone.gsub(/\D/, '')
155   format = options[:format] || 'global'
156   formatted_phone = format == 'global' ? self.global_format(phone) : self.national_format(phone)
157   formatted_phone
158 end
global_format(phone) click to toggle source

Format phone number into international format. If missing country code, it will return the raw number.

@param [String] phone Phone number

@return [String] Phone number in national format

@example

CogiPhony.global_format('84933081090') # => '+84 93 3081090'
CogiPhony.global_format('84837621350') # => '+84 8 3762 1350'
CogiPhony.global_format('0933081090')  # => '0933081090'
CogiPhony.global_format('14037089189') # => '+1 (403) 708-9189'

@example It return raw number if can not format

CogiPhony.national_format('+84837621350') # => '+84837621350'
    # File lib/cogi_phony.rb
175 def self.global_format(phone)
176   Phony.format(phone, format: :international) rescue phone
177 end
gmobile?(phone) click to toggle source

Check if a phone number is provided by Gmobile

@param [String] phone Phone number

@return [Boolean]

   # File lib/cogi_phony.rb
28 def self.gmobile?(phone)
29   pattern = /\A(\+84|084|84|0)(#{mobile_networks_hash['vi']['gmobile']})\d{7}$/
30   pattern.match phone
31 end
hi() click to toggle source
  # File lib/cogi_phony.rb
7 def self.hi
8   puts "Hello from Cogi::Phony"
9 end
mobifone?(phone) click to toggle source

Check if a phone number is provided by Mobifone

@param [String] phone Phone number

@return [Boolean]

   # File lib/cogi_phony.rb
48 def self.mobifone?(phone)
49   pattern = /\A(\+84|084|84|0)(#{mobile_networks_hash['vi']['mobifone']})\d{7}$/
50   pattern.match phone
51 end
mobile_networks_hash() click to toggle source

Return a Hash of mobile networks

@return [Hash]

   # File lib/cogi_phony.rb
19 def self.mobile_networks_hash
20   @mobile_networks_hash ||= YAML.load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'data/mobile_networks.yaml'))
21 end
national_format(phone) click to toggle source

Format phone number into national format. If missing country code, it will return the raw number.

@param [String] phone Phone number

@return [String] Phone number in national format

@example Format phone number into national format

CogiPhony.national_format('84933081090') # => '093 3081090'
CogiPhony.national_format('84837621350') # => '08 3762 1350'

@example It return raw number if can not format

CogiPhony.national_format('+84837621350') # => '+84837621350'
    # File lib/cogi_phony.rb
192 def self.national_format(phone)
193   Phony.format(phone, format: :national) rescue phone
194 end
normalize(phone, options = {}) click to toggle source

Normalize phone numer to international format.

@param [String] phone Phone number @param [Hash] options An option hash

format: 'global' or 'vietnam'. Default is 'global'
default_country_code: Default country code

@return [String] A phone number formatted in international format.

@raise [CogiPhony::NormalizationError] If can not normalize the given number.

@example Normalize phone number with country code.

CogiPhony.normalize('+84 933081090 ')     # => '+84933081090'
CogiPhony.normalize('(+84)933081090 ')    # => '+84933081090'
CogiPhony.normalize('+8493-308-1090')     # => '+84933081090'
CogiPhony.normalize('84 933081090 ')      # => '+84933081090'
CogiPhony.normalize('+1 (403) 708-9189')  # => '+14037089189'

@example Normalize phone number without country code, and in vietnam format

CogiPhony.normalize('0933081090', format: 'vietnam') # => '+84933081090'
CogiPhony.normalize('933081090', format: 'vietnam')  # => '+84933081090'
CogiPhony.normalize('1214468866', format: 'vietnam') # => '+841214468866'
CogiPhony.normalize('0837621351', format: 'vietnam') # => '+84837621351'

@example Normalize phone number with default country code, and in global format

CogiPhony.normalize('0933081090', default_country_code: '84')   # => '+84933081090'
CogiPhony.normalize('933081090', default_country_code: '84')    # => '+84933081090'
CogiPhony.normalize('(403) 708-9189', default_country_code: '1' # => '+14037089189'

@example Normalize phone number with invalid default country code, and in global format

CogiPhony.normalize('0933081090', default_country_code: '111111') # => raise error CogiPhony::NormalizationError
CogiPhony.normalize('0933081090', default_country_code: 'abcd')   # => raise error CogiPhony::NormalizationError

@example Normalize phone number without default country code, and in global format

CogiPhony.normalize('0933081090', format: 'global') # => raise error CogiPhony::NormalizationError
CogiPhony.normalize('37621351', format: 'global')   # => raise error CogiPhony::NormalizationError
    # File lib/cogi_phony.rb
247 def self.normalize(phone, options = {})
248   return nil if phone.nil? || phone.empty?
249 
250   raise CogiPhony::NormalizationError unless self.validate? phone
251 
252   phone = phone.gsub(/\D/, '') # remove string character
253 
254   unless Phony.plausible?(phone) # Do not include country code
255     if options[:format] == 'vietnam'
256       phone = phone.gsub(/\A0(.*?)/, '84\1') # replace 0 with 84
257       phone = "84#{phone}" if /\A(8|9)\d{8}$|\A1\d{9}$/.match phone # insert 84 before phone number
258     else # if options[:format] == 'global'
259       default_country_code = options[:default_country_code]
260       if default_country_code && Phony.plausible?("#{default_country_code}#{phone.gsub(/^0/, '')}")
261         phone = "#{default_country_code}#{phone.gsub(/^0/, '')}" # add country code before phone number
262       else
263         # TODO: handle if can not normalize
264         raise CogiPhony::NormalizationError
265       end
266     end
267   end
268 
269   phone = phone.gsub(/\A0(.*?)/, '\1') if phone.start_with?('084')
270   phone = "+#{phone}" # add plus sign before phone number
271   phone
272 end
phone_to_provider(phone) click to toggle source

Return phone provider from phone number.

@param [String] phone Phone number

@return [String] Phone provider

@example

CogiPhony.phone_to_provider('0988091097') # => Viettel
CogiPhony.phone_to_provider('0938091097') # => Mobifone
CogiPhony.phone_to_provider('0918091097') # => Vinaphone
CogiPhony.phone_to_provider('0928091097') # => Vietnamobile
CogiPhony.phone_to_provider('0998091097') # => Gmobile (Beeline)
CogiPhony.phone_to_provider('0837621351') # => Others
   # File lib/cogi_phony.rb
86 def self.phone_to_provider(phone)
87   return 'Viettel' if self.viettel? phone
88   return 'Mobifone' if self.mobifone? phone
89   return 'Vinaphone' if self.vinaphone? phone
90   return 'Vietnamobile' if self.vietnamobile? phone
91   return 'Gmobile (Beeline)' if self.gmobile? phone
92   'Others'
93 end
validate?(phone) click to toggle source

Check if a string is a valid phone number. Reference: www.regexr.com/38pvb

@param [String] phone Phone number

@return [Boolean] True if it is in a valid phone number, otherwise False

    # File lib/cogi_phony.rb
280 def self.validate?(phone)
281   return false if phone.nil?
282 
283   # pattern = /\A((\+)?\d{1,6}|0)\d{7,10}$/
284   pattern = /^\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$/
285   pattern.match phone
286 end
vietnamobile?(phone) click to toggle source

Check if a phone number is provided by Vietnamobile

@param [String] phone Phone number

@return [Boolean]

   # File lib/cogi_phony.rb
38 def self.vietnamobile?(phone)
39   pattern = /\A(\+84|084|84|0)(#{mobile_networks_hash['vi']['vietnamobile']})\d{7}$/
40   pattern.match phone
41 end
viettel?(phone) click to toggle source

Check if a phone number is provided by Viettel

@param [String] phone Phone number

@return [Boolean]

   # File lib/cogi_phony.rb
68 def self.viettel?(phone)
69   pattern = /\A(\+84|084|84|0)(#{mobile_networks_hash['vi']['viettel']})\d{7}$/
70   pattern.match phone
71 end
vinaphone?(phone) click to toggle source

Check if a phone number is provided by Vinaphone

@param [String] phone Phone number

@return [Boolean]

   # File lib/cogi_phony.rb
58 def self.vinaphone?(phone)
59   pattern = /\A(\+84|084|84|0)(#{mobile_networks_hash['vi']['vinaphone']})\d{7}$/
60   pattern.match phone
61 end
vn_mobile_phone?(phone) click to toggle source

Check if phone number is Vietnam mobile phone format

A phone is valid if:

- Is not empty
- Begin with +84|84|0 and
  - next number is 8|9 the length must be 8 (short number. Ex: 0933081090)
  - next number is 1, the length must be 9 (long number: Ex: 01214468866)

@param [String] phone Phone number

@return [Boolean] True if it is in Vietnam mobile phone format, otherwise False

@example

CogiPhony.vn_mobile_phone?('0933081090')     # => true
CogiPhony.vn_mobile_phone?('84933081090')    # => true
CogiPhony.vn_mobile_phone?('+84933081090')   # => true
CogiPhony.vn_mobile_phone?('+14037089189')   # => false
CogiPhony.vn_mobile_phone?('84837621350')    # => false
    # File lib/cogi_phony.rb
113 def self.vn_mobile_phone?(phone)
114   return false if phone.nil? || phone.empty?
115   return false if /\A(\+84|084|84|0)((9\d{8})|(88\d{7})|(1\d{9}))$/.match(phone).nil?
116   true
117 end
vn_phone?(phone) click to toggle source

Check if a phone number is in Vietnam

@param [String] phone Phone number

@return [Boolean] True if phone in vietnam, otherwise False

@example

CogiPhony.vn_phone?('+84933081090') # => True
CogiPhony.vn_phone?('084933081090') # => True
CogiPhony.vn_phone?('84933081090')  # => True
CogiPhony.vn_phone?('0933081090')   # => True
CogiPhony.vn_phone?('+14037089189') # => False
    # File lib/cogi_phony.rb
131 def self.vn_phone?(phone)
132   return false unless /\A(\+84|84|084|0)(\d{8,10})$/.match phone
133   true
134 end