mini_phone 1.1.2 → 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45a73dd6dde16c5baff08c0d9cf225e4bb53ae15b8b040559e4555180070e8d1
4
- data.tar.gz: cbb196644cb95f7cf3a46648c176e22845708f5e3bab14256a8d80832e187175
3
+ metadata.gz: 4f3cfea7458b4c50503fd9b9786c3622c7018128ab2785cee254ed45b8da6008
4
+ data.tar.gz: 574028442c0b7bc1ce52ca558f2397b7a298fafaebc635b082bc211620235805
5
5
  SHA512:
6
- metadata.gz: b95b0e2e18d15b7cf7c17e901a85d0b42f2e5fd6d9947f6d0e63a97197c86b39ce8f6341669e67ab69a07a6356b336f3cf4659a328a9c603a6d03856ac7c325b
7
- data.tar.gz: 5db0ebb434cd3d427ae511e2a3e8bad13bcc7520c1147e1d7a8fafbf06c3213ae8521b30234b7691d0f06f634e0a503912bf638820f7dd37750ea12838944cf3
6
+ metadata.gz: ed571b1fd20e7fd99effc2a70b0f692e91962c0145bd47440b54ef374b60ef7705cf29952c2380ed491fa5fa8cb6df98a3460c3479014cbc4c510445b7b9f6fa
7
+ data.tar.gz: f56388c5750d5fc1446bdcae0dcfaf5b47edba6ffff33360c0dab2144088380bd9316fbc6ad7623005ec38ec9105dd94000ad7fd13f5540b0b4cb4d09d8159b3
data/Dockerfile.dev CHANGED
@@ -6,4 +6,4 @@ WORKDiR /app
6
6
  RUN apk add --no-cache libphonenumber-dev valgrind git make libffi-dev build-base
7
7
  COPY Gemfile Gemfile.lock mini_phone.gemspec ./
8
8
  COPY lib/mini_phone/version.rb ./lib/mini_phone/version.rb
9
- RUN bundle install -j4
9
+ RUN bundle install -j4
data/Gemfile CHANGED
@@ -17,4 +17,5 @@ gem 'rubocop', require: false
17
17
  group :bench do
18
18
  gem 'benchmark-ips'
19
19
  gem 'phonelib'
20
+ gem 'telephone_number'
20
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 40x to 50x faster than other Ruby phone number
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: valid?: 23111.4 i/s
83
- Phonelib: valid?: 482.3 i/s - 47.92x (± 0.00) slower
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: e164: 31567.0 i/s
89
- Phonelib: e164: 652.3 i/s - 48.39x (± 0.00) slower
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:
@@ -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) + sizeof(PhoneNumber); }
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 == PhoneNumberUtil::NO_PARSING_ERROR && phone_util.IsValidNumber(parsed_number)) {
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 def_cc = rb_iv_get(rb_mMiniPhone, "@default_country");
69
+ VALUE input_region_code = rb_iv_get(rb_mMiniPhone, "@default_country");
64
70
 
65
- return is_phone_number_valid(self, str, def_cc);
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 def_cc = rb_iv_get(rb_mMiniPhone, "@default_country");
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(def_cc), RSTRING_LEN(def_cc));
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");
@@ -363,17 +308,22 @@ extern "C" VALUE rb_phone_number_region_code(VALUE self) {
363
308
  if (rb_ivar_defined(self, rb_intern("@region_code"))) {
364
309
  return rb_iv_get(self, "@region_code");
365
310
  }
311
+ VALUE input_region_code = rb_iv_get(self, "@input_region_code");
366
312
 
367
- PhoneNumberInfo *phone_number_info;
368
- std::string code;
369
- TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
370
- const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
313
+ if (NIL_P(input_region_code)) {
314
+ PhoneNumberInfo *phone_number_info;
315
+ std::string code;
316
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
317
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
371
318
 
372
- phone_util.GetRegionCodeForCountryCode(phone_number_info->phone_number->country_code(), &code);
319
+ phone_util.GetRegionCodeForCountryCode(phone_number_info->phone_number->country_code(), &code);
373
320
 
374
- VALUE result = rb_str_new(code.c_str(), code.size());
321
+ VALUE result = rb_str_new(code.c_str(), code.size());
375
322
 
376
- return rb_iv_set(self, "@region_code", result);
323
+ return rb_iv_set(self, "@region_code", result);
324
+ } else {
325
+ return rb_iv_set(self, "@region_code", input_region_code);
326
+ }
377
327
  }
378
328
 
379
329
  extern "C" VALUE rb_phone_number_match_eh(VALUE self, VALUE other) {
@@ -381,14 +331,6 @@ extern "C" VALUE rb_phone_number_match_eh(VALUE self, VALUE other) {
381
331
  return Qfalse;
382
332
  }
383
333
 
384
- VALUE self_input = rb_iv_get(self, "@input");
385
- VALUE other_input = rb_iv_get(other, "@input");
386
-
387
- // If inputs are the exact same, the result is as well
388
- if (rb_eql(self_input, other_input)) {
389
- return Qtrue;
390
- }
391
-
392
334
  const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
393
335
 
394
336
  PhoneNumberInfo *self_info;
@@ -397,6 +339,10 @@ extern "C" VALUE rb_phone_number_match_eh(VALUE self, VALUE other) {
397
339
  PhoneNumberInfo *other_info;
398
340
  TypedData_Get_Struct(other, PhoneNumberInfo, &phone_number_info_type, other_info);
399
341
 
342
+ if (self_info->phone_number->raw_input() == other_info->phone_number->raw_input()) {
343
+ return Qtrue;
344
+ }
345
+
400
346
  if (phone_util.IsNumberMatch(*other_info->phone_number, *self_info->phone_number) == PhoneNumberUtil::EXACT_MATCH) {
401
347
  return Qtrue;
402
348
  } else {
@@ -488,7 +434,19 @@ extern "C" VALUE rb_phone_number_area_code(VALUE self) {
488
434
  return rb_iv_set(self, "@area_code", result);
489
435
  }
490
436
 
491
- extern "C" VALUE rb_phone_number_to_s(VALUE self) { return rb_iv_get(self, "@input"); }
437
+ extern "C" VALUE rb_phone_number_to_s(VALUE self) {
438
+ PhoneNumberInfo *phone_number_info;
439
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
440
+ PhoneNumber *phone_number = phone_number_info->phone_number;
441
+
442
+ if (phone_number == NULL) {
443
+ return Qnil;
444
+ }
445
+
446
+ std::string raw_input = phone_number->raw_input();
447
+
448
+ return rb_str_new(raw_input.c_str(), raw_input.size());
449
+ }
492
450
 
493
451
  static inline void setup_formats() {
494
452
  // Raw
@@ -502,6 +460,81 @@ static inline void setup_formats() {
502
460
  dsh_fmt->set_format("$1-$2-$3");
503
461
  }
504
462
 
463
+ extern "C" VALUE rb_phone_number_valid_eh(VALUE self) {
464
+ if (rb_ivar_defined(self, rb_intern("@valid"))) {
465
+ return rb_iv_get(self, "@valid");
466
+ }
467
+
468
+ std::string formatted_number;
469
+ PhoneNumberInfo *phone_number_info;
470
+ VALUE input_region_code = rb_iv_get(self, "@input_region_code");
471
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
472
+
473
+ if (NIL_P(input_region_code)) {
474
+ input_region_code = rb_iv_get(rb_mMiniPhone, "@default_country");
475
+ }
476
+
477
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
478
+
479
+ if (!rb_str_equal(input_region_code, rb_str_new_literal("ZZ"))) {
480
+ std::string country_code(RSTRING_PTR(input_region_code), RSTRING_LEN(input_region_code));
481
+
482
+ if (phone_util.IsValidNumberForRegion(*phone_number_info->phone_number, country_code)) {
483
+ return rb_iv_set(self, "@valid", Qtrue);
484
+ } else {
485
+ return rb_iv_set(self, "@valid", Qfalse);
486
+ }
487
+ }
488
+
489
+ if (phone_util.IsValidNumber(*phone_number_info->phone_number)) {
490
+ return rb_iv_set(self, "@valid", Qtrue);
491
+ } else {
492
+ return rb_iv_set(self, "@valid", Qfalse);
493
+ }
494
+ }
495
+
496
+ extern "C" VALUE rb_phone_number_invalid_eh(VALUE self) {
497
+ return rb_phone_number_valid_eh(self) == Qtrue ? Qfalse : Qtrue;
498
+ }
499
+
500
+ extern "C" VALUE rb_phone_number_initialize(int argc, VALUE *argv, VALUE self) {
501
+ VALUE str;
502
+ VALUE input_region_code;
503
+
504
+ rb_scan_args(argc, argv, "11", &str, &input_region_code);
505
+ rb_iv_set(self, "@input_region_code", input_region_code);
506
+
507
+ if (NIL_P(input_region_code)) {
508
+ input_region_code = rb_iv_get(rb_mMiniPhone, "@default_country");
509
+ }
510
+
511
+ if (FIXNUM_P(str)) {
512
+ str = rb_fix2str(str, 10);
513
+ } else if (!RB_TYPE_P(str, T_STRING)) {
514
+ return rb_phone_number_nullify_ivars(self);
515
+ }
516
+
517
+ PhoneNumberInfo *phone_number_info;
518
+ PhoneNumber parsed_number;
519
+
520
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
521
+
522
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
523
+
524
+ std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
525
+ std::string country_code(RSTRING_PTR(input_region_code), RSTRING_LEN(input_region_code));
526
+
527
+ auto result = phone_util.ParseAndKeepRawInput(phone_number, country_code, &parsed_number);
528
+
529
+ if (result != PhoneNumberUtil::NO_PARSING_ERROR) {
530
+ rb_phone_number_nullify_ivars(self);
531
+ }
532
+
533
+ phone_number_info->phone_number->Swap(&parsed_number);
534
+
535
+ return self;
536
+ }
537
+
505
538
  extern "C" void Init_mini_phone(void) {
506
539
  setup_formats();
507
540
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniPhone
4
- VERSION = '1.1.2'
4
+ VERSION = '1.1.7'
5
5
  end
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.2
4
+ version: 1.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-22 00:00:00.000000000 Z
11
+ date: 2021-03-10 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.