piliponi 1.0.0
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.
- data/.gitignore +29 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +75 -0
- data/Rakefile +7 -0
- data/lib/piliponi.rb +58 -0
- data/lib/piliponi/piliponi_api.rb +21 -0
- data/lib/piliponi/version.rb +3 -0
- data/piliponi.gemspec +24 -0
- data/spec/piliponi_spec.rb +107 -0
- data/spec/spec_helper.rb +8 -0
- metadata +107 -0
data/.gitignore
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
.rspec
|
7
|
+
Gemfile.lock
|
8
|
+
InstalledFiles
|
9
|
+
_yardoc
|
10
|
+
coverage
|
11
|
+
doc/
|
12
|
+
lib/bundler/man
|
13
|
+
pkg
|
14
|
+
rdoc
|
15
|
+
spec/reports
|
16
|
+
test/tmp
|
17
|
+
test/version_tmp
|
18
|
+
tmp
|
19
|
+
|
20
|
+
#vim temp files
|
21
|
+
*.un~
|
22
|
+
*.swp
|
23
|
+
*.swo
|
24
|
+
|
25
|
+
#rvm files
|
26
|
+
.rvmrc
|
27
|
+
.ruby-version
|
28
|
+
|
29
|
+
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ace Dimasuhid
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Piliponi
|
2
|
+
|
3
|
+
Simple mobile phone formatter for the Philippines
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'piliponi'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install piliponi
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
Include piliponi to your class to use its methods
|
21
|
+
|
22
|
+
class Dummy
|
23
|
+
include Piliponi
|
24
|
+
end
|
25
|
+
|
26
|
+
dummy = Dummy.new
|
27
|
+
|
28
|
+
###Plausible
|
29
|
+
Check if the number is a probable mobile number
|
30
|
+
|
31
|
+
dummy.plausible? "091700000001" #true
|
32
|
+
dummy.plausible? "NotaNumber" #false
|
33
|
+
|
34
|
+
###Clean
|
35
|
+
Removes Non-numeric characters
|
36
|
+
|
37
|
+
dummy.clean "+639-17-000-0001" #639170000001
|
38
|
+
|
39
|
+
###Telco
|
40
|
+
Returns local Telco coverage of a given number
|
41
|
+
|
42
|
+
dummy.telco "09170000001" #globe
|
43
|
+
|
44
|
+
###Normalization
|
45
|
+
Returns formatted numbers as specified
|
46
|
+
|
47
|
+
####Local
|
48
|
+
dummy.normalize("0917-000-0000", format: :local) #09170000000
|
49
|
+
|
50
|
+
####International
|
51
|
+
dummy.normalize("0917-000-0000", format: :international) #+639170000000
|
52
|
+
|
53
|
+
####Pure
|
54
|
+
dummy.normalize("0917-000-0000", format: :pure) #9170000000
|
55
|
+
|
56
|
+
####Erroneous Numbers
|
57
|
+
dummy.normalize("wat", format: :pure) #Piliponi::InvalidPhoneNumberException
|
58
|
+
|
59
|
+
####Wrong format
|
60
|
+
dummy.normalize("0917-000-0000", format: :none) #Piliponi::FormatNotRecognizedException
|
61
|
+
|
62
|
+
##Needs
|
63
|
+
|
64
|
+
Accepting Integers
|
65
|
+
<br>
|
66
|
+
Thorough testing
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
1. Fork it
|
71
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
72
|
+
3. Add Rspec Tests
|
73
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
6. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/piliponi.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require "piliponi/version"
|
2
|
+
require "piliponi/piliponi_api"
|
3
|
+
|
4
|
+
module Piliponi
|
5
|
+
class ::FormatNotRecognizedException < Exception; end;
|
6
|
+
class ::InvalidPhoneNumberException < Exception; end;
|
7
|
+
|
8
|
+
def plausible? number
|
9
|
+
clean_num = clean(number)
|
10
|
+
size = clean_num.size
|
11
|
+
|
12
|
+
(clean_num[0] == "9" && size == 10) ||
|
13
|
+
(clean_num[0..1] == "63" && size == 12) ||
|
14
|
+
(clean_num[0..1] == "09" && size == 11)
|
15
|
+
end
|
16
|
+
|
17
|
+
def normalize(number, options={})
|
18
|
+
formats = [:pure, :local, :international]
|
19
|
+
format = options[:format].intern
|
20
|
+
|
21
|
+
raise FormatNotRecognizedException unless formats.include?(format)
|
22
|
+
|
23
|
+
return send "_nf_#{format}", clean(number) if plausible?(number)
|
24
|
+
raise InvalidPhoneNumberException
|
25
|
+
end
|
26
|
+
|
27
|
+
def clean(number=nil)
|
28
|
+
number.tr('^0-9','') if number
|
29
|
+
end
|
30
|
+
|
31
|
+
def telco? number=nil
|
32
|
+
PiliponiApi.new.lookup prefix(clean number)
|
33
|
+
end
|
34
|
+
|
35
|
+
def prefix number=nil
|
36
|
+
hash = { '0' => [0,3], '6' => [2,4], '9' => [0,2] }
|
37
|
+
start, finish = hash[number[0]].first, hash[number[0]].last
|
38
|
+
clean_prefix(number[start..finish])
|
39
|
+
end
|
40
|
+
|
41
|
+
def clean_prefix(number)
|
42
|
+
number = "0#{number}" if number[0] != '0'; number
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def _nf_pure(number)
|
48
|
+
number[-10..-1]
|
49
|
+
end
|
50
|
+
|
51
|
+
def _nf_local(number)
|
52
|
+
'0' << _nf_pure(number)
|
53
|
+
end
|
54
|
+
|
55
|
+
def _nf_international(number)
|
56
|
+
'+63' << _nf_pure(number)
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Piliponi
|
2
|
+
class PiliponiApi
|
3
|
+
def lookup(prefix)
|
4
|
+
smart = %w{0813 0918 0947 0998 0999 0907 0908 0909 0910 0912 0919 0921
|
5
|
+
0928 0929 0949 0989 0920 0930 0938 0939 0946}
|
6
|
+
globe = %w{0817 0917 0994 0905 0915 0916 0926 0927 0935 0936 0937 0996 0997}
|
7
|
+
sun = %w{0922 0923 0925 0932 0933 0934 0942 0943}
|
8
|
+
|
9
|
+
if smart.include? prefix
|
10
|
+
"smart"
|
11
|
+
elsif globe.include? prefix
|
12
|
+
"globe"
|
13
|
+
elsif sun.include? prefix
|
14
|
+
"sun"
|
15
|
+
else
|
16
|
+
"unknown"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/piliponi.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'piliponi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "piliponi"
|
8
|
+
spec.version = Piliponi::VERSION
|
9
|
+
spec.authors = ["Ace Dimasuhid"]
|
10
|
+
spec.email = ["ace.dimasuhid@gmail.com"]
|
11
|
+
spec.description = %q{Philippine Mobile Phone Formatter}
|
12
|
+
spec.summary = %q{Mobile Phone Number Formatter for the Philippines}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Dummy
|
4
|
+
include Piliponi
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Piliponi do
|
8
|
+
let!(:dummy) { Dummy.new }
|
9
|
+
|
10
|
+
context "#plausible?" do
|
11
|
+
it "works" do
|
12
|
+
lambda do
|
13
|
+
dummy.plausible?("whatevs")
|
14
|
+
end.should_not raise_error
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns true given 9170000001" do
|
18
|
+
dummy.plausible?("9170000001").should eq(true)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns true given 639170000001" do
|
22
|
+
dummy.plausible?("639170000001").should eq(true)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns true given +639170000001" do
|
26
|
+
dummy.plausible?("+639170000001").should eq(true)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns false given a non-numerical string" do
|
30
|
+
dummy.plausible?("wawa").should eq(false)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
context "#clean" do
|
36
|
+
it "removes characters" do
|
37
|
+
dummy.clean("+639170000001").should eq("639170000001")
|
38
|
+
end
|
39
|
+
|
40
|
+
it "returns nil if no number is placed" do
|
41
|
+
dummy.clean.should be_nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "#normalize" do
|
46
|
+
let(:number) { '(0919) 363-2598' }
|
47
|
+
|
48
|
+
it "formats to 09XXXXXXXX" do
|
49
|
+
dummy.normalize(number, {format: 'local'}).should eq('09193632598')
|
50
|
+
end
|
51
|
+
|
52
|
+
it "formats to +639XXXXXXX" do
|
53
|
+
dummy.normalize(number, {format: 'international'}).should eq('+639193632598')
|
54
|
+
end
|
55
|
+
|
56
|
+
it "formats to 9XXXXXXX" do
|
57
|
+
dummy.normalize(number, {format: 'pure'}).should eq('9193632598')
|
58
|
+
end
|
59
|
+
|
60
|
+
context "when the format passed is not recognized" do
|
61
|
+
it "raises FormatNotRecognizedException" do
|
62
|
+
expect{
|
63
|
+
dummy.normalize(number, format: :foo)
|
64
|
+
}.to raise_error(FormatNotRecognizedException)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "when the number is not valid" do
|
69
|
+
it "raises InvalidPhoneNumberException" do
|
70
|
+
expect{
|
71
|
+
dummy.normalize("notanumber", format: :local)
|
72
|
+
}.to raise_error(InvalidPhoneNumberException)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "#telco?" do
|
78
|
+
it "returns smart for 09XX" do
|
79
|
+
dummy.telco?('09201234567').should eq('smart')
|
80
|
+
end
|
81
|
+
|
82
|
+
it "returns globe for 09XX" do
|
83
|
+
dummy.telco?("09170000000").should eq("globe")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context '#prefix' do
|
88
|
+
describe '09171231234' do
|
89
|
+
it 'returns 0917' do
|
90
|
+
expect(dummy.prefix('09171231234')).to eq '0917'
|
91
|
+
expect(dummy.prefix('639171231234')).to eq '0917'
|
92
|
+
expect(dummy.prefix('9171231234')).to eq '0917'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context '#clean_prefix' do
|
98
|
+
describe '0917' do
|
99
|
+
it 'retunrs 0917' do
|
100
|
+
expect(dummy.prefix('0917')).to eq '0917'
|
101
|
+
expect(dummy.prefix('63917')).to eq '0917'
|
102
|
+
expect(dummy.prefix('917')).to eq '0917'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: piliponi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ace Dimasuhid
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Philippine Mobile Phone Formatter
|
63
|
+
email:
|
64
|
+
- ace.dimasuhid@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/piliponi.rb
|
75
|
+
- lib/piliponi/piliponi_api.rb
|
76
|
+
- lib/piliponi/version.rb
|
77
|
+
- piliponi.gemspec
|
78
|
+
- spec/piliponi_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
homepage: ''
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.23
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Mobile Phone Number Formatter for the Philippines
|
105
|
+
test_files:
|
106
|
+
- spec/piliponi_spec.rb
|
107
|
+
- spec/spec_helper.rb
|