mini_phone 1.1.0 → 1.1.5

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: 89fa88b742202101e6bb0204bad2fd20545946a19447d227f61312dd1f9e251c
4
- data.tar.gz: 49a1d96d9a61aa00c24b88c7e6d3b51dab2929f9ec12043ce2508fc6af374da2
3
+ metadata.gz: c3a42ec155de4fb7b2a8ea675ac0380274f23b38795c0eb0e73c0efb3ed4d8a5
4
+ data.tar.gz: aaec470245e26e61d277a329340833eef93fa22939395273eabba719f977408e
5
5
  SHA512:
6
- metadata.gz: 5e719bb7cade41a127cd601f06ca5f2529dae2994112c89bf56ace37202230a2c4a3fae7c0738632d78d032cae0f6b7d1e20365a48f57f0ff046bc0241b2f023
7
- data.tar.gz: f4ba0f376645bea9b1c25c94f5ba696d744988a1e11a44ff4081af0435165b7a573e24f3101b12dd652add2dccf2e23596bf75ed21b43a5e2fd0a44b1772ef35
6
+ metadata.gz: 99128e918a87c4e875191b47177507da77be675aafb7a7bec430457ab420c4b5081872407b31d07de04fc24233e57d14e711a019f49939d3db89e757eeeab1a1
7
+ data.tar.gz: a9676a2d17fd3a5e0d178159ed9b413ea73469790e5219040cfa80e5679fce578ac0e80e95b60366cdd23f4961744eba65740464e6bd05bb1549805547862b1f
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
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
@@ -78,15 +78,15 @@ 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
80
  ```
81
+ # Results from my Linux (5.10.6-arch1-10)
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
+ Phonelib: valid?: 426.0 i/s
85
+ MiniPhone: valid?: 34707.9 i/s - 81.47x faster
85
86
 
86
- ```
87
87
  Comparison:
88
- MiniPhone: e164: 31567.0 i/s
89
- Phonelib: e164: 652.3 i/s - 48.39x (± 0.00) slower
88
+ Phonelib: e164: 580.3 i/s
89
+ MiniPhone: e164: 43385.9 i/s - 74.76x faster
90
90
  ```
91
91
 
92
92
  ## Installation
@@ -98,7 +98,7 @@ Comparison:
98
98
  ```
99
99
 
100
100
  ```sh
101
- apt-get install -y libphonenumber # debian / ubuntu
101
+ apt-get install -y libphonenumber-dev # debian / ubuntu
102
102
  ```
103
103
 
104
104
  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,6 +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
 
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
+ std::string raw_input = phone_number->raw_input();
437
+
438
+ if (raw_input == "") {
439
+ return Qnil;
440
+ } else {
441
+ return rb_str_new(raw_input.c_str(), raw_input.size());
442
+ }
443
+ }
444
+
483
445
  static inline void setup_formats() {
484
446
  // Raw
485
447
  NumberFormat *raw_fmt = raw_national_format.Add();
@@ -492,6 +454,76 @@ static inline void setup_formats() {
492
454
  dsh_fmt->set_format("$1-$2-$3");
493
455
  }
494
456
 
457
+ extern "C" VALUE rb_phone_number_valid_eh(VALUE self) {
458
+ if (rb_ivar_defined(self, rb_intern("@valid"))) {
459
+ return rb_iv_get(self, "@valid");
460
+ }
461
+
462
+ std::string formatted_number;
463
+ PhoneNumberInfo *phone_number_info;
464
+ VALUE input_region_code = rb_iv_get(self, "@input_region_code");
465
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
466
+
467
+ if (NIL_P(input_region_code)) {
468
+ input_region_code = rb_iv_get(rb_mMiniPhone, "@default_country");
469
+ }
470
+
471
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
472
+
473
+ if (!rb_str_equal(input_region_code, rb_str_new_literal("ZZ")) &&
474
+ !rb_str_equal(rb_phone_number_region_code(self), input_region_code)) {
475
+ return rb_iv_set(self, "@valid", Qfalse);
476
+ }
477
+
478
+ if (phone_util.IsValidNumber(*phone_number_info->phone_number)) {
479
+ return rb_iv_set(self, "@valid", Qtrue);
480
+ } else {
481
+ return rb_iv_set(self, "@valid", Qfalse);
482
+ }
483
+ }
484
+
485
+ extern "C" VALUE rb_phone_number_invalid_eh(VALUE self) {
486
+ return rb_phone_number_valid_eh(self) == Qtrue ? Qfalse : Qtrue;
487
+ }
488
+
489
+ extern "C" VALUE rb_phone_number_initialize(int argc, VALUE *argv, VALUE self) {
490
+ VALUE str;
491
+ VALUE input_region_code;
492
+
493
+ rb_scan_args(argc, argv, "11", &str, &input_region_code);
494
+ rb_iv_set(self, "@input_region_code", input_region_code);
495
+
496
+ if (NIL_P(input_region_code)) {
497
+ input_region_code = rb_iv_get(rb_mMiniPhone, "@default_country");
498
+ }
499
+
500
+ if (FIXNUM_P(str)) {
501
+ str = rb_fix2str(str, 10);
502
+ } else if (!RB_TYPE_P(str, T_STRING)) {
503
+ return rb_phone_number_nullify_ivars(self);
504
+ }
505
+
506
+ PhoneNumberInfo *phone_number_info;
507
+ PhoneNumber parsed_number;
508
+
509
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
510
+
511
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
512
+
513
+ std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
514
+ std::string country_code(RSTRING_PTR(input_region_code), RSTRING_LEN(input_region_code));
515
+
516
+ auto result = phone_util.ParseAndKeepRawInput(phone_number, country_code, &parsed_number);
517
+
518
+ if (result != PhoneNumberUtil::NO_PARSING_ERROR) {
519
+ rb_phone_number_nullify_ivars(self);
520
+ } else {
521
+ phone_number_info->phone_number->Swap(&parsed_number);
522
+ }
523
+
524
+ return self;
525
+ }
526
+
495
527
  extern "C" void Init_mini_phone(void) {
496
528
  setup_formats();
497
529
 
@@ -544,5 +576,7 @@ extern "C" void Init_mini_phone(void) {
544
576
  rb_define_method(rb_cPhoneNumber, "country_code", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_country_code), 0);
545
577
  rb_define_method(rb_cPhoneNumber, "type", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_type), 0);
546
578
  rb_define_method(rb_cPhoneNumber, "area_code", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_area_code), 0);
547
- rb_define_method(rb_cPhoneNumber, "eql?", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_eql_eh), 1);
579
+ rb_define_method(rb_cPhoneNumber, "to_s", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_to_s), 0);
580
+ rb_define_method(rb_cPhoneNumber, "raw_input", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_to_s), 0);
581
+ rb_define_method(rb_cPhoneNumber, "==", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_match_eh), 1);
548
582
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniPhone
4
- VERSION = '1.1.0'
4
+ VERSION = '1.1.5'
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.0
4
+ version: 1.1.5
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-16 00:00:00.000000000 Z
11
+ date: 2021-02-11 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.