mini_phone 1.1.1 → 1.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Dockerfile.dev +1 -1
- data/Gemfile +2 -0
- data/README.md +10 -8
- data/ext/mini_phone/extconf.rb +1 -1
- data/ext/mini_phone/mini_phone.cc +107 -76
- data/lib/mini_phone/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48fbcbb306ad72fdcbd33b68c46e1c3bc586d24a7ba931f8c4a059e22f65b3a7
|
4
|
+
data.tar.gz: cbdc2cadfa4c1438312de09177b2c54d476f68e5fc585248eeb046212d71025a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0cb7432992d4c61aaa8ccb0774e48aad98a61a575555dc6e4d509560938fd4fb4267bd62d90cdd2f589d93ed1eb5ded12503fe213023f10972ed399ded5f863c
|
7
|
+
data.tar.gz: 4dd9e4ae33cde41d27e308fc1d5249cc7676274162ecc20d79e1088f029b6aab330bfe1dc424fb74515fbb42ab744c375fd60ef6078ad6c9ba6e315ca07a71f8
|
data/Dockerfile.dev
CHANGED
data/Gemfile
CHANGED
@@ -8,6 +8,7 @@ gemspec
|
|
8
8
|
gem 'rake', require: false
|
9
9
|
# https://github.com/rake-compiler/rake-compiler/pull/166
|
10
10
|
gem 'get_process_mem', require: false
|
11
|
+
gem 'pry', require: false
|
11
12
|
gem 'rake-compiler', github: 'larskanis/rake-compiler', branch: 'fix-native-version'
|
12
13
|
gem 'rspec', '~> 3.0', require: false
|
13
14
|
gem 'rspec-github', require: false
|
@@ -16,4 +17,5 @@ gem 'rubocop', require: false
|
|
16
17
|
group :bench do
|
17
18
|
gem 'benchmark-ips'
|
18
19
|
gem 'phonelib'
|
20
|
+
gem 'telephone_number'
|
19
21
|
end
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
A Ruby gem which plugs directly into Google's native C++
|
4
4
|
[libphonenumber](https://github.com/google/libphonenumber) for extremely
|
5
5
|
_fast_ and _robust_ phone number parsing, validation, and formatting. On
|
6
|
-
average, most methods are
|
6
|
+
average, most methods are 70x to 80x faster than other Ruby phone number
|
7
7
|
libraries.
|
8
8
|
|
9
9
|
## Usage
|
@@ -77,16 +77,18 @@ an issue and we will try to support it.
|
|
77
77
|
On average, most methods are 40x to 50x faster than other libraries. To run
|
78
78
|
the benchmarks locally, execute: `bundle exec rake bench`
|
79
79
|
|
80
|
+
### Results from my Linux (5.10.6-arch1-10)
|
81
|
+
|
80
82
|
```
|
81
83
|
Comparison:
|
82
|
-
MiniPhone:
|
83
|
-
Phonelib:
|
84
|
-
|
84
|
+
MiniPhone: valid?: 33382.0 i/s
|
85
|
+
Phonelib: valid?: 422.8 i/s - 78.95x (± 0.00) slower
|
86
|
+
TelephoneNumber: valid?: 164.3 i/s - 203.13x (± 0.00) slower
|
85
87
|
|
86
|
-
```
|
87
88
|
Comparison:
|
88
|
-
MiniPhone:
|
89
|
-
Phonelib:
|
89
|
+
MiniPhone: e164: 41187.5 i/s
|
90
|
+
Phonelib: e164: 579.0 i/s - 71.14x (± 0.00) slower
|
91
|
+
TelephoneNumber: e164: 228.8 i/s - 179.99x (± 0.00) slower
|
90
92
|
```
|
91
93
|
|
92
94
|
## Installation
|
@@ -98,7 +100,7 @@ Comparison:
|
|
98
100
|
```
|
99
101
|
|
100
102
|
```sh
|
101
|
-
apt-get install -y libphonenumber # debian / ubuntu
|
103
|
+
apt-get install -y libphonenumber-dev # debian / ubuntu
|
102
104
|
```
|
103
105
|
|
104
106
|
2. Add this line to your application's Gemfile:
|
data/ext/mini_phone/extconf.rb
CHANGED
@@ -20,7 +20,7 @@ unless have_library('phonenumber')
|
|
20
20
|
| brew install libphonenumber |
|
21
21
|
| |
|
22
22
|
| On Debian / Ubuntu: |
|
23
|
-
| apt-get install -y libphonenumber
|
23
|
+
| apt-get install -y libphonenumber-dev |
|
24
24
|
| |
|
25
25
|
| 2. Retry installing the gem (i.e `bundle install`) |
|
26
26
|
| |
|
@@ -16,7 +16,7 @@ static RepeatedPtrField<NumberFormat> dasherized_national_format;
|
|
16
16
|
|
17
17
|
extern "C" struct PhoneNumberInfo { PhoneNumber *phone_number; };
|
18
18
|
|
19
|
-
extern "C" size_t phone_number_info_size(const void *data) { return sizeof(PhoneNumberInfo)
|
19
|
+
extern "C" size_t phone_number_info_size(const void *data) { return sizeof(PhoneNumberInfo); }
|
20
20
|
|
21
21
|
extern "C" void phone_number_info_free(void *data) {
|
22
22
|
PhoneNumberInfo *phone_number_info = static_cast<PhoneNumberInfo *>(data);
|
@@ -52,7 +52,13 @@ static inline VALUE is_phone_number_valid(VALUE self, VALUE str, VALUE cc) {
|
|
52
52
|
|
53
53
|
auto result = phone_util.ParseAndKeepRawInput(phone_number, country_code, &parsed_number);
|
54
54
|
|
55
|
-
if (result
|
55
|
+
if (result != PhoneNumberUtil::NO_PARSING_ERROR) {
|
56
|
+
return Qfalse;
|
57
|
+
}
|
58
|
+
|
59
|
+
if (country_code == "ZZ" && phone_util.IsValidNumber(parsed_number)) {
|
60
|
+
return Qtrue;
|
61
|
+
} else if (phone_util.IsValidNumberForRegion(parsed_number, country_code)) {
|
56
62
|
return Qtrue;
|
57
63
|
} else {
|
58
64
|
return Qfalse;
|
@@ -60,9 +66,9 @@ static inline VALUE is_phone_number_valid(VALUE self, VALUE str, VALUE cc) {
|
|
60
66
|
}
|
61
67
|
|
62
68
|
extern "C" VALUE rb_is_phone_number_valid(VALUE self, VALUE str) {
|
63
|
-
VALUE
|
69
|
+
VALUE input_region_code = rb_iv_get(rb_mMiniPhone, "@default_country");
|
64
70
|
|
65
|
-
return is_phone_number_valid(self, str,
|
71
|
+
return is_phone_number_valid(self, str, input_region_code);
|
66
72
|
}
|
67
73
|
|
68
74
|
extern "C" VALUE rb_normalize_digits_only(VALUE self, VALUE str) {
|
@@ -99,9 +105,9 @@ extern "C" VALUE rb_is_phone_number_possible(VALUE self, VALUE str) {
|
|
99
105
|
PhoneNumber parsed_number;
|
100
106
|
const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
|
101
107
|
|
102
|
-
VALUE
|
108
|
+
VALUE input_region_code = rb_iv_get(rb_mMiniPhone, "@default_country");
|
103
109
|
std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
|
104
|
-
std::string country_code(RSTRING_PTR(
|
110
|
+
std::string country_code(RSTRING_PTR(input_region_code), RSTRING_LEN(input_region_code));
|
105
111
|
|
106
112
|
auto result = phone_util.Parse(phone_number, country_code, &parsed_number);
|
107
113
|
|
@@ -159,45 +165,6 @@ static inline VALUE rb_phone_number_nullify_ivars(VALUE self) {
|
|
159
165
|
return Qtrue;
|
160
166
|
}
|
161
167
|
|
162
|
-
extern "C" VALUE rb_phone_number_initialize(int argc, VALUE *argv, VALUE self) {
|
163
|
-
VALUE str;
|
164
|
-
VALUE def_cc;
|
165
|
-
|
166
|
-
rb_scan_args(argc, argv, "11", &str, &def_cc);
|
167
|
-
|
168
|
-
if (NIL_P(def_cc)) {
|
169
|
-
def_cc = rb_iv_get(rb_mMiniPhone, "@default_country");
|
170
|
-
}
|
171
|
-
|
172
|
-
rb_iv_set(self, "@input", str);
|
173
|
-
|
174
|
-
if (FIXNUM_P(str)) {
|
175
|
-
str = rb_fix2str(str, 10);
|
176
|
-
} else if (!RB_TYPE_P(str, T_STRING)) {
|
177
|
-
return rb_phone_number_nullify_ivars(self);
|
178
|
-
}
|
179
|
-
|
180
|
-
PhoneNumberInfo *phone_number_info;
|
181
|
-
PhoneNumber parsed_number;
|
182
|
-
|
183
|
-
TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
|
184
|
-
|
185
|
-
const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
|
186
|
-
|
187
|
-
std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
|
188
|
-
std::string country_code(RSTRING_PTR(def_cc), RSTRING_LEN(def_cc));
|
189
|
-
|
190
|
-
auto result = phone_util.Parse(phone_number, country_code, &parsed_number);
|
191
|
-
|
192
|
-
if (result != PhoneNumberUtil::NO_PARSING_ERROR) {
|
193
|
-
rb_phone_number_nullify_ivars(self);
|
194
|
-
} else {
|
195
|
-
phone_number_info->phone_number->Swap(&parsed_number);
|
196
|
-
}
|
197
|
-
|
198
|
-
return self;
|
199
|
-
}
|
200
|
-
|
201
168
|
static inline VALUE rb_phone_number_format(VALUE self, PhoneNumberUtil::PhoneNumberFormat fmt) {
|
202
169
|
std::string formatted_number;
|
203
170
|
PhoneNumberInfo *phone_number_info;
|
@@ -315,28 +282,6 @@ extern "C" VALUE rb_phone_number_raw_international(VALUE self) {
|
|
315
282
|
return rb_iv_set(self, "@raw_international", result);
|
316
283
|
}
|
317
284
|
|
318
|
-
extern "C" VALUE rb_phone_number_valid_eh(VALUE self) {
|
319
|
-
if (rb_ivar_defined(self, rb_intern("@valid"))) {
|
320
|
-
return rb_iv_get(self, "@valid");
|
321
|
-
}
|
322
|
-
|
323
|
-
std::string formatted_number;
|
324
|
-
PhoneNumberInfo *phone_number_info;
|
325
|
-
TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
|
326
|
-
|
327
|
-
const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
|
328
|
-
|
329
|
-
if (phone_util.IsValidNumber(*phone_number_info->phone_number)) {
|
330
|
-
return rb_iv_set(self, "@valid", Qtrue);
|
331
|
-
} else {
|
332
|
-
return rb_iv_set(self, "@valid", Qfalse);
|
333
|
-
}
|
334
|
-
}
|
335
|
-
|
336
|
-
extern "C" VALUE rb_phone_number_invalid_eh(VALUE self) {
|
337
|
-
return rb_phone_number_valid_eh(self) == Qtrue ? Qfalse : Qtrue;
|
338
|
-
}
|
339
|
-
|
340
285
|
extern "C" VALUE rb_phone_number_possible_eh(VALUE self) {
|
341
286
|
if (rb_ivar_defined(self, rb_intern("@possible"))) {
|
342
287
|
return rb_iv_get(self, "@possible");
|
@@ -376,20 +321,24 @@ extern "C" VALUE rb_phone_number_region_code(VALUE self) {
|
|
376
321
|
return rb_iv_set(self, "@region_code", result);
|
377
322
|
}
|
378
323
|
|
379
|
-
extern "C" VALUE
|
380
|
-
if (!
|
324
|
+
extern "C" VALUE rb_phone_number_match_eh(VALUE self, VALUE other) {
|
325
|
+
if (!rb_obj_is_kind_of(other, rb_cPhoneNumber)) {
|
381
326
|
return Qfalse;
|
382
327
|
}
|
383
328
|
|
384
329
|
const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
|
385
330
|
|
386
|
-
PhoneNumberInfo *
|
387
|
-
TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type,
|
331
|
+
PhoneNumberInfo *self_info;
|
332
|
+
TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, self_info);
|
333
|
+
|
334
|
+
PhoneNumberInfo *other_info;
|
335
|
+
TypedData_Get_Struct(other, PhoneNumberInfo, &phone_number_info_type, other_info);
|
388
336
|
|
389
|
-
|
390
|
-
|
337
|
+
if (self_info->phone_number->raw_input() == other_info->phone_number->raw_input()) {
|
338
|
+
return Qtrue;
|
339
|
+
}
|
391
340
|
|
392
|
-
if (phone_util.IsNumberMatch(*
|
341
|
+
if (phone_util.IsNumberMatch(*other_info->phone_number, *self_info->phone_number) == PhoneNumberUtil::EXACT_MATCH) {
|
393
342
|
return Qtrue;
|
394
343
|
} else {
|
395
344
|
return Qfalse;
|
@@ -480,7 +429,19 @@ extern "C" VALUE rb_phone_number_area_code(VALUE self) {
|
|
480
429
|
return rb_iv_set(self, "@area_code", result);
|
481
430
|
}
|
482
431
|
|
483
|
-
extern "C" VALUE rb_phone_number_to_s(VALUE self) {
|
432
|
+
extern "C" VALUE rb_phone_number_to_s(VALUE self) {
|
433
|
+
PhoneNumberInfo *phone_number_info;
|
434
|
+
TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
|
435
|
+
PhoneNumber *phone_number = phone_number_info->phone_number;
|
436
|
+
|
437
|
+
if (phone_number == NULL) {
|
438
|
+
return Qnil;
|
439
|
+
}
|
440
|
+
|
441
|
+
std::string raw_input = phone_number->raw_input();
|
442
|
+
|
443
|
+
return rb_str_new(raw_input.c_str(), raw_input.size());
|
444
|
+
}
|
484
445
|
|
485
446
|
static inline void setup_formats() {
|
486
447
|
// Raw
|
@@ -494,6 +455,76 @@ static inline void setup_formats() {
|
|
494
455
|
dsh_fmt->set_format("$1-$2-$3");
|
495
456
|
}
|
496
457
|
|
458
|
+
extern "C" VALUE rb_phone_number_valid_eh(VALUE self) {
|
459
|
+
if (rb_ivar_defined(self, rb_intern("@valid"))) {
|
460
|
+
return rb_iv_get(self, "@valid");
|
461
|
+
}
|
462
|
+
|
463
|
+
std::string formatted_number;
|
464
|
+
PhoneNumberInfo *phone_number_info;
|
465
|
+
VALUE input_region_code = rb_iv_get(self, "@input_region_code");
|
466
|
+
TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
|
467
|
+
|
468
|
+
if (NIL_P(input_region_code)) {
|
469
|
+
input_region_code = rb_iv_get(rb_mMiniPhone, "@default_country");
|
470
|
+
}
|
471
|
+
|
472
|
+
const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
|
473
|
+
|
474
|
+
if (!rb_str_equal(input_region_code, rb_str_new_literal("ZZ")) &&
|
475
|
+
!rb_str_equal(rb_phone_number_region_code(self), input_region_code)) {
|
476
|
+
return rb_iv_set(self, "@valid", Qfalse);
|
477
|
+
}
|
478
|
+
|
479
|
+
if (phone_util.IsValidNumber(*phone_number_info->phone_number)) {
|
480
|
+
return rb_iv_set(self, "@valid", Qtrue);
|
481
|
+
} else {
|
482
|
+
return rb_iv_set(self, "@valid", Qfalse);
|
483
|
+
}
|
484
|
+
}
|
485
|
+
|
486
|
+
extern "C" VALUE rb_phone_number_invalid_eh(VALUE self) {
|
487
|
+
return rb_phone_number_valid_eh(self) == Qtrue ? Qfalse : Qtrue;
|
488
|
+
}
|
489
|
+
|
490
|
+
extern "C" VALUE rb_phone_number_initialize(int argc, VALUE *argv, VALUE self) {
|
491
|
+
VALUE str;
|
492
|
+
VALUE input_region_code;
|
493
|
+
|
494
|
+
rb_scan_args(argc, argv, "11", &str, &input_region_code);
|
495
|
+
rb_iv_set(self, "@input_region_code", input_region_code);
|
496
|
+
|
497
|
+
if (NIL_P(input_region_code)) {
|
498
|
+
input_region_code = rb_iv_get(rb_mMiniPhone, "@default_country");
|
499
|
+
}
|
500
|
+
|
501
|
+
if (FIXNUM_P(str)) {
|
502
|
+
str = rb_fix2str(str, 10);
|
503
|
+
} else if (!RB_TYPE_P(str, T_STRING)) {
|
504
|
+
return rb_phone_number_nullify_ivars(self);
|
505
|
+
}
|
506
|
+
|
507
|
+
PhoneNumberInfo *phone_number_info;
|
508
|
+
PhoneNumber parsed_number;
|
509
|
+
|
510
|
+
TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
|
511
|
+
|
512
|
+
const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
|
513
|
+
|
514
|
+
std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
|
515
|
+
std::string country_code(RSTRING_PTR(input_region_code), RSTRING_LEN(input_region_code));
|
516
|
+
|
517
|
+
auto result = phone_util.ParseAndKeepRawInput(phone_number, country_code, &parsed_number);
|
518
|
+
|
519
|
+
if (result != PhoneNumberUtil::NO_PARSING_ERROR) {
|
520
|
+
rb_phone_number_nullify_ivars(self);
|
521
|
+
}
|
522
|
+
|
523
|
+
phone_number_info->phone_number->Swap(&parsed_number);
|
524
|
+
|
525
|
+
return self;
|
526
|
+
}
|
527
|
+
|
497
528
|
extern "C" void Init_mini_phone(void) {
|
498
529
|
setup_formats();
|
499
530
|
|
@@ -547,5 +578,5 @@ extern "C" void Init_mini_phone(void) {
|
|
547
578
|
rb_define_method(rb_cPhoneNumber, "type", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_type), 0);
|
548
579
|
rb_define_method(rb_cPhoneNumber, "area_code", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_area_code), 0);
|
549
580
|
rb_define_method(rb_cPhoneNumber, "to_s", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_to_s), 0);
|
550
|
-
rb_define_method(rb_cPhoneNumber, "
|
581
|
+
rb_define_method(rb_cPhoneNumber, "==", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_match_eh), 1);
|
551
582
|
}
|
data/lib/mini_phone/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_phone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian Ker-Seymer
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Plugs directly in the the Google's native C++ [libphonenumber](https://github.com/google/libphonenumber)
|
14
14
|
for extemely _fast_ and _robust_ phone number parsing, validation, and formatting.
|
@@ -43,7 +43,7 @@ metadata:
|
|
43
43
|
homepage_uri: https://github.com/ianks/mini_phone
|
44
44
|
source_code_uri: https://github.com/ianks/mini_phone
|
45
45
|
changelog_uri: https://github.com/ianks/mini_phone/blob/master/CHANGELOG.md
|
46
|
-
post_install_message:
|
46
|
+
post_install_message:
|
47
47
|
rdoc_options: []
|
48
48
|
require_paths:
|
49
49
|
- lib
|
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
59
|
version: '0'
|
60
60
|
requirements: []
|
61
61
|
rubygems_version: 3.1.4
|
62
|
-
signing_key:
|
62
|
+
signing_key:
|
63
63
|
specification_version: 4
|
64
64
|
summary: Uses the Google libphonenumber C lib to parse, validate, and format phone
|
65
65
|
numbers
|