Module: FPhone

Defined in:
lib/f_phone.rb,
lib/f_phone/version.rb

Constant Summary collapse

VERSION =
"0.1.12"

Class Method Summary collapse

Class Method Details

.country_code_from_number(phone) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# 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_codesObject



11
12
13
# 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 = {}) ⇒ Object



72
73
74
75
76
77
78
# 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) ⇒ Object



64
65
66
# File 'lib/f_phone.rb', line 64

def self.global_format phone
  Phony.format(phone, format: :international) rescue phone
end

.gmobile?(phone) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# 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

.hiObject



7
8
9
# File 'lib/f_phone.rb', line 7

def self.hi
  puts "Hello from F::Phone"
end

.mobifone?(phone) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# 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_networksObject



15
16
17
# 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) ⇒ Object



68
69
70
# File 'lib/f_phone.rb', line 68

def self.national_format phone
  Phony.format(phone, format: :national) rescue phone
end

.normalize(phone, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object



44
45
46
47
48
49
50
51
# 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) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# 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) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# 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) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# 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) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
# 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) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
# 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