phonelib 0.6.8 → 0.6.9
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.
- checksums.yaml +4 -4
- data/README.md +10 -1
- data/data/extended_data.dat +0 -0
- data/data/phone_data.dat +0 -0
- data/lib/phonelib/core.rb +16 -1
- data/lib/phonelib/data_importer_helper.rb +2 -2
- data/lib/phonelib/phone_formatter.rb +1 -1
- data/lib/phonelib/version.rb +1 -1
- data/lib/validators/phone_validator.rb +2 -1
- metadata +20 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 057bf64ada014da3a08f3355708b52ff368ecbba
|
4
|
+
data.tar.gz: 58f4e1fd554667c03f827fc73b45fdcb91c38cfe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7dc5128f57dd6d743d32ea86ae5de72cf3ac539d19a3bafa7fc8d33153520d7382d786286e417be9b4fb31f0cf69b9e5baa4c353de51d68754397862db28bb5
|
7
|
+
data.tar.gz: 4abda9ec008548813bd838137a4f9139345f57f2e794f296f218db0d48263c14874446aac396f1d264e5f864a35294a22aa6b2b6d8b619d5652f0237eb167caa
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
## Phonelib
|
2
2
|
|
3
|
+
[](https://www.jetbrains.com/ruby/)
|
3
4
|
[](http://badge.fury.io/rb/phonelib)
|
4
5
|
[](http://travis-ci.org/daddyz/phonelib)
|
5
6
|
[](https://codeclimate.com/github/daddyz/phonelib/coverage)
|
@@ -67,6 +68,12 @@ To set symbols that are used for separating extension from phone number for pars
|
|
67
68
|
Phonelib.extension_separate_symbols = '#;'
|
68
69
|
```
|
69
70
|
|
71
|
+
In case you need to overwrite some Google's libphonenumber library data, you need to assign file path to this setter. File should be Marshal.dump'ed with existing structure like in ```Phonelib.phone_data```. Gem is simply doing ```merge``` between hashes.
|
72
|
+
|
73
|
+
``` ruby
|
74
|
+
Phonelib.override_phone_data = '/path/to/override_phone_data.dat'
|
75
|
+
```
|
76
|
+
|
70
77
|
In case phone number that was passed for parsing has "+" sign in the beginning, library will try to detect a country regarding the provided one.
|
71
78
|
|
72
79
|
### ActiveRecord Integration
|
@@ -84,7 +91,7 @@ Please note that passing blank value also fails.
|
|
84
91
|
Additional options:
|
85
92
|
|
86
93
|
``` ruby
|
87
|
-
validates :attribute, phone: { possible: true, allow_blank: true, types: [:voip, :mobile] }
|
94
|
+
validates :attribute, phone: { possible: true, allow_blank: true, types: [:voip, :mobile], country_specifier: -> phone { phone.country.try(:upcase) } }
|
88
95
|
```
|
89
96
|
|
90
97
|
<tt>possible: true</tt> - enables validation to check whether the passed number is a possible phone number (not strict check).
|
@@ -95,6 +102,8 @@ Refer to [Google libphonenumber](http://code.google.com/p/libphonenumber/) for m
|
|
95
102
|
<tt>types: :mobile</tt> or <tt>types: [:voip, :mobile]</tt> - allows to validate against specific phone types patterns,
|
96
103
|
if mixed with <tt>possible</tt> will check if number is possible for specified type
|
97
104
|
|
105
|
+
<tt>country_specifier: -> phone { phone.country.try(:upcase) }</tt> - allows to specify country for validation dynamically for each validation.
|
106
|
+
|
98
107
|
### Basic usage
|
99
108
|
|
100
109
|
To check if phone number is valid simply run:
|
data/data/extended_data.dat
CHANGED
Binary file
|
data/data/phone_data.dat
CHANGED
Binary file
|
data/lib/phonelib/core.rb
CHANGED
@@ -107,6 +107,16 @@ module Phonelib
|
|
107
107
|
@@strict_check = strict
|
108
108
|
end
|
109
109
|
|
110
|
+
@@override_phone_data = nil
|
111
|
+
# setter for data file to use
|
112
|
+
def override_phone_data=(file_path)
|
113
|
+
@@override_phone_data = file_path
|
114
|
+
end
|
115
|
+
|
116
|
+
def override_phone_data
|
117
|
+
@@override_phone_data
|
118
|
+
end
|
119
|
+
|
110
120
|
# gem constants definition
|
111
121
|
|
112
122
|
# @private Main data file
|
@@ -324,7 +334,12 @@ module Phonelib
|
|
324
334
|
# @private Load data file into memory
|
325
335
|
def load_data
|
326
336
|
data_file = "#{File.dirname(__FILE__)}/../../#{FILE_MAIN_DATA}"
|
327
|
-
Marshal.load(File.binread(data_file))
|
337
|
+
default_data = Marshal.load(File.binread(data_file))
|
338
|
+
if override_phone_data
|
339
|
+
override_data_file = Marshal.load(File.binread(override_phone_data))
|
340
|
+
default_data.merge!(override_data_file)
|
341
|
+
end
|
342
|
+
default_data
|
328
343
|
end
|
329
344
|
|
330
345
|
# @private Load extended data file into memory
|
@@ -55,10 +55,10 @@ module Phonelib
|
|
55
55
|
def parse_raw_file(file)
|
56
56
|
data = {}
|
57
57
|
File.readlines(file).each do |line|
|
58
|
-
line = str_clean line
|
58
|
+
line = str_clean line, false
|
59
59
|
next if line.empty? || line[0] == '#'
|
60
60
|
prefix, line_data = line.split('|')
|
61
|
-
data[prefix] = line_data && line_data.split('&')
|
61
|
+
data[prefix] = line_data && line_data.strip.split('&')
|
62
62
|
end
|
63
63
|
data
|
64
64
|
end
|
@@ -124,7 +124,7 @@ module Phonelib
|
|
124
124
|
|
125
125
|
# @private Get needable data for formatting phone as national number
|
126
126
|
def formatting_data
|
127
|
-
return @formatting_data if @formatting_data
|
127
|
+
return @formatting_data if defined?(@formatting_data)
|
128
128
|
|
129
129
|
format = @data[country][:format]
|
130
130
|
prefix = @data[country][Core::NATIONAL_PREFIX]
|
data/lib/phonelib/version.rb
CHANGED
@@ -43,8 +43,9 @@ class PhoneValidator < ActiveModel::EachValidator
|
|
43
43
|
# Validation method
|
44
44
|
def validate_each(record, attribute, value)
|
45
45
|
return if options[:allow_blank] && value.blank?
|
46
|
+
country = options[:country_specifier].call(record) if options[:country_specifier]
|
46
47
|
|
47
|
-
phone = parse(value)
|
48
|
+
phone = parse(value, country)
|
48
49
|
valid = if simple_validation?
|
49
50
|
method = options[:possible] ? :possible? : :valid?
|
50
51
|
phone.send(method)
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phonelib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vadim Senderovich
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "<"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
19
|
+
version: '11.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "<"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
26
|
+
version: '11.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: nokogiri
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - '='
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '1.2'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: json
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.8.6
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.8.6
|
111
125
|
description: |2
|
112
126
|
Google libphonenumber library was taken as a basis for
|
113
127
|
this gem. Gem uses its data file for validations and number formatting.
|