f_phone 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ # List of mobile network in Viet Nam include mobifone, vinaphone, vietnam mobile, Gmobile, Viettel.
2
+ # References: https://gocit.vn/bai-viet/tong-hop-danh-sach-dau-so-mang-di-dong-o-viet-nam/
3
+
4
+ vi:
5
+ viettel: "86|96|97|98|32|33|34|35|36|37|38|39"
6
+ mobifone: "89|90|93|70|79|77|76|78"
7
+ vinaphone: "88|91|94|83|84|85|81|82"
8
+ vietnamobile: "92|56|58"
9
+ gmobile: "99|59"
data/lib/f_phone.rb ADDED
@@ -0,0 +1,112 @@
1
+ # require_relative "f_phone/error"
2
+ require_relative "../lib/f_phone/version"
3
+ require "phony"
4
+ require "yaml"
5
+ require "pry"
6
+
7
+ module FPhone
8
+ def self.hi
9
+ puts "Hello from F::Phone"
10
+ end
11
+
12
+ def self.country_codes
13
+ @country_codes = YAML::load_file(File.join(__dir__, "data/country_codes.yaml"))
14
+ end
15
+
16
+ def self.mobile_networks
17
+ @mobile_networks = YAML::load_file(File.join(__dir__, "data/mobile_networks.yaml"))
18
+ end
19
+
20
+ def self.mobifone? phone
21
+ pattern = /\A(\+84|084|84|0)(#{mobile_networks["vi"]["mobifone"]})\d{7}$/ #\A match the begining of string
22
+ pattern.match phone
23
+ end
24
+
25
+ def self.viettel? phone
26
+ pattern = /\A(\+84|084|84|0)(#{mobile_networks["vi"]["viettel"]})\d{7}$/ #\A match the begining of string
27
+ pattern.match phone
28
+ end
29
+
30
+ def self.vinaphone? phone
31
+ pattern = /\A(\+84|084|84|0)(#{mobile_networks["vi"]["vinaphone"]})\d{7}$/ #\A match the begining of string
32
+ pattern.match phone
33
+ end
34
+
35
+ def self.vietnamobile? phone
36
+ pattern = /\A(\+84|084|84|0)(#{mobile_networks["vi"]["vietnamobile"]})\d{7}$/ #\A match the begining of string
37
+ pattern.match phone
38
+ end
39
+
40
+ def self.gmobile? phone
41
+ pattern = /\A(\+84|084|84|0)(#{mobile_networks["vi"]["gmobile"]})\d{7}$/ #\A match the begining of string \d{7}
42
+ pattern.match phone
43
+ end
44
+
45
+ def self.phone_to_provider phone
46
+ return "Mobifone" if self.mobifone? phone
47
+ return "Viettel" if self.viettel? phone
48
+ return "Vinaphone" if self.vinaphone? phone
49
+ return "Vietnamobile" if self.vietnamobile? phone
50
+ return "G Mobile" if self.gmobile? phone
51
+ "Other"
52
+ end
53
+
54
+ def self.validate? phone
55
+ return false if phone.nil?
56
+ pattern = /^\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$/ #reference code at https://regexr.com/38pvb
57
+ pattern.match phone
58
+ end
59
+
60
+ def self.global_format phone
61
+ Phony.format(phone, format: :international) rescue phone
62
+ end
63
+
64
+ def self.national_format phone
65
+ Phony.format(phone, format: :national) rescue phone
66
+ end
67
+
68
+ def self.format phone, options = {}
69
+ return nil if phone.nil?
70
+
71
+ phone = phone.gsub(/\D/, "") # Delete any thing other than digits (xoa bat cu thu gi ngoai so)
72
+ format = options[:format] || "global" # Set default format is global
73
+ formatted_phone = format == "global" ? self.global_format(phone) : self.national_format(phone)
74
+ formatted_phone
75
+ end
76
+
77
+ def self.country_code_from_number phone
78
+ country_codes
79
+ return nil unless Phony.plausible? phone
80
+ country_code = Phony.split(Phony.normalize(phone)).first
81
+ puts "Country code is: #{country_code}"
82
+ array_country = @country_codes['countries'].values
83
+ array_country.each do |country|
84
+ return country.values[1] if country_code == country.values[0]
85
+ end
86
+ end
87
+
88
+ def self.normalize phone, options = {}
89
+ return nil if phone.nil?
90
+ original_phone = phone.dup
91
+ phone = phone.gsub(/\D/, '') # remove string character
92
+
93
+ unless Phony.plausible?(phone) # Do not include country code
94
+ if options[:format] == 'vietnam'
95
+ phone = phone.gsub(/\A0(.*?)/, '84\1') # replace 0 with 84
96
+ phone = "84#{phone}" if /\A(8|9)\d{8}$|\A1\d{9}$/.match phone # insert 84 before phone number
97
+ else # if options[:format] == "global"
98
+ default_country_code = options[:default_country_code]
99
+ if default_country_code && Phony.plausible?("#{default_country_code}#{phone.gsub(/^0/, '')}")
100
+ phone = "#{default_country_code}#{phone.gsub(/^0/, "")}" # add country code before phone number
101
+ else
102
+ return "Error. Can not normalize number phone"
103
+ end
104
+ end
105
+ end
106
+
107
+ phone = "+#{phone}" # add plus sign before phone number
108
+ phone
109
+ end
110
+ end
111
+
112
+ p FPhone.normalize '0913418980', default_country_code: '1111'
@@ -0,0 +1,3 @@
1
+ module FPhone
2
+ VERSION = "0.1.10"
3
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: f_phone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.10
5
+ platform: ruby
6
+ authors:
7
+ - Nguyen Van Thuan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: "Used to validate phone numbers, format phone numbers according to international
14
+ standards or Vietnamese standards, \n extract the country
15
+ code (country name) via phone number, \n check the phone
16
+ number of the network (within the range Viet Nam) then return to the network provider
17
+ providing the phone number"
18
+ email:
19
+ - thuan.nguyenvan0501@gmail.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - ".gitignore"
25
+ - ".rspec"
26
+ - ".travis.yml"
27
+ - CODE_OF_CONDUCT.md
28
+ - Gemfile
29
+ - Gemfile.lock
30
+ - LICENSE.txt
31
+ - README.md
32
+ - Rakefile
33
+ - bin/console
34
+ - bin/setup
35
+ - f_phone.gemspec
36
+ - lib/data/country_codes.yaml
37
+ - lib/data/mobile_networks.yaml
38
+ - lib/f_phone.rb
39
+ - lib/f_phone/version.rb
40
+ homepage: https://rubygems.org/gems/f_phone
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.3.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.1.2
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: This gem provide you library to validate, format phone number,... in Viet
63
+ Nam
64
+ test_files: []