module FPhone
Constants
- VERSION
Public Class Methods
country_code_from_number(phone)
click to toggle source
# File lib/f_phone.rb, line 80 def self.country_code_from_number phone country_codes return nil unless Phony.plausible? phone country_code = Phony.split(Phony.normalize(phone)).first puts "Country code is: #{country_code}" array_country = @country_codes['countries'].values array_country.each do |country| return country.values[1] if country_code == country.values[0] end end
country_codes()
click to toggle source
# File lib/f_phone.rb, line 11 def self.country_codes @country_codes = YAML::load_file(File.join(__dir__, "data/country_codes.yaml")) end
format(phone, options = {})
click to toggle source
# File lib/f_phone.rb, line 72 def self.format phone, options = {} return nil if phone.nil? phone = phone.gsub(/\D/, "") # Delete any thing other than digits (xoa bat cu thu gi ngoai so) format = options[:format] || "global" # Set default format is global formatted_phone = format == "global" ? self.global_format(phone) : self.national_format(phone) formatted_phone end
global_format(phone)
click to toggle source
# File lib/f_phone.rb, line 64 def self.global_format phone Phony.format(phone, format: :international) rescue phone end
gmobile?(phone)
click to toggle source
# File lib/f_phone.rb, line 39 def self.gmobile? phone pattern = /\A(\+84|084|84|0)(#{mobile_networks["vi"]["gmobile"]})\d{7}$/ #\A match the begining of string \d{7} pattern.match phone end
hi()
click to toggle source
# File lib/f_phone.rb, line 7 def self.hi puts "Hello from F::Phone" end
mobifone?(phone)
click to toggle source
# File lib/f_phone.rb, line 19 def self.mobifone? phone pattern = /\A(\+84|084|84|0)(#{mobile_networks["vi"]["mobifone"]})\d{7}$/ #\A match the begining of string pattern.match phone end
mobile_networks()
click to toggle source
# File lib/f_phone.rb, line 15 def self.mobile_networks @mobile_networks = YAML::load_file(File.join(__dir__, "data/mobile_networks.yaml")) end
national_format(phone)
click to toggle source
# File lib/f_phone.rb, line 68 def self.national_format phone Phony.format(phone, format: :national) rescue phone end
normalize(phone, options = {})
click to toggle source
# File lib/f_phone.rb, line 91 def self.normalize phone, options = {} return nil if phone.nil? original_phone = phone.dup phone = phone.gsub(/\D/, '') # remove string character unless Phony.plausible?(phone) # Do not include country code if options[:format] == 'vietnam' phone = phone.gsub(/\A0(.*?)/, '84\1') # replace 0 with 84 phone = "84#{phone}" if /\A(8|9)\d{8}$|\A1\d{9}$/.match phone # insert 84 before phone number else # if options[:format] == "global" default_country_code = options[:default_country_code] if default_country_code && Phony.plausible?("#{default_country_code}#{phone.gsub(/^0/, '')}") phone = "#{default_country_code}#{phone.gsub(/^0/, "")}" # add country code before phone number else return "Error. Can not normalize number phone" end end end phone = "+#{phone}" # add plus sign before phone number phone end
phone_to_provider(phone)
click to toggle source
# File lib/f_phone.rb, line 44 def self.phone_to_provider phone return "Mobifone" if self.mobifone? phone return "Viettel" if self.viettel? phone return "Vinaphone" if self.vinaphone? phone return "Vietnamobile" if self.vietnamobile? phone return "G Mobile" if self.gmobile? phone "Other" end
validate?(phone)
click to toggle source
# File lib/f_phone.rb, line 58 def self.validate? phone return false if phone.nil? pattern = /^\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$/ #reference code at https://regexr.com/38pvb pattern.match phone end
vietnamobile?(phone)
click to toggle source
# File lib/f_phone.rb, line 34 def self.vietnamobile? phone pattern = /\A(\+84|084|84|0)(#{mobile_networks["vi"]["vietnamobile"]})\d{7}$/ #\A match the begining of string pattern.match phone end
viettel?(phone)
click to toggle source
# File lib/f_phone.rb, line 24 def self.viettel? phone pattern = /\A(\+84|084|84|0)(#{mobile_networks["vi"]["viettel"]})\d{7}$/ #\A match the begining of string pattern.match phone end
vinaphone?(phone)
click to toggle source
# File lib/f_phone.rb, line 29 def self.vinaphone? phone pattern = /\A(\+84|084|84|0)(#{mobile_networks["vi"]["vinaphone"]})\d{7}$/ #\A match the begining of string pattern.match phone end
vn_phone?(phone)
click to toggle source
# File lib/f_phone.rb, line 53 def self.vn_phone?(phone) return false unless /\A(\+84|84|084|0)(\d{8}|\d{9})$/.match phone true end