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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9fb40430cfcb4a6d8cc44557e7fb84622fe20cf14819c14f10a919dc5b9880e7
4
- data.tar.gz: 5ad868da4e69d3b0ef3b91687863547ea876ec01e0bd2d7ae2e9bea1c8e16668
3
+ metadata.gz: 48fbcbb306ad72fdcbd33b68c46e1c3bc586d24a7ba931f8c4a059e22f65b3a7
4
+ data.tar.gz: cbdc2cadfa4c1438312de09177b2c54d476f68e5fc585248eeb046212d71025a
5
5
  SHA512:
6
- metadata.gz: df02fd332486b4b1c2775420377d2b478324b1b25de950268279ce6dd1875373dd4c7bc798300b0446cee72d7581d7338f4a0725d811a0ff317b7209bdebb4a7
7
- data.tar.gz: a4130f908c91d8369665ec31174857ab6a2e7dc07b85f97008560a3d057db23a49dc39b85e53d0f61a290e0f89c8b320b2f5023041b0b19704bf66f9363ed672
6
+ metadata.gz: 0cb7432992d4c61aaa8ccb0774e48aad98a61a575555dc6e4d509560938fd4fb4267bd62d90cdd2f589d93ed1eb5ded12503fe213023f10972ed399ded5f863c
7
+ data.tar.gz: 4dd9e4ae33cde41d27e308fc1d5249cc7676274162ecc20d79e1088f029b6aab330bfe1dc424fb74515fbb42ab744c375fd60ef6078ad6c9ba6e315ca07a71f8
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
@@ -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 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");
@@ -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 rb_phone_number_eql_eh(VALUE self, VALUE other) {
380
- if (!rb_obj_is_instance_of(other, rb_cPhoneNumber)) {
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 *self_phone_number_info;
387
- TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, self_phone_number_info);
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
- PhoneNumberInfo *other_phone_number_info;
390
- TypedData_Get_Struct(other, PhoneNumberInfo, &phone_number_info_type, other_phone_number_info);
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(*other_phone_number_info->phone_number, *self_phone_number_info->phone_number)) {
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) { return rb_iv_get(self, "@input"); }
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, "eql?", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_eql_eh), 1);
581
+ rb_define_method(rb_cPhoneNumber, "==", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_match_eh), 1);
551
582
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniPhone
4
- VERSION = '1.1.1'
4
+ VERSION = '1.1.6'
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.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-01-19 00:00:00.000000000 Z
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