mini_phone 1.0.3 → 1.0.4

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: 8f19ec27523bfdcd42b72b4421441b4016b4bf86c94f91f527e10bf5a33d1a16
4
- data.tar.gz: 7ab2cf83930eaa1c35f7bf2a2faa7afded85238b8a69bf8822013f3f33ee274a
3
+ metadata.gz: ec6635c4a695331ec847d69f21e79578f5f9ebdddf2752a7f1074fabd33ccfac
4
+ data.tar.gz: 51aae23c0a2330a9599ee0336ad591e8fa4548c041b0a6aa1710f5834791b5f7
5
5
  SHA512:
6
- metadata.gz: 853cc1202032f2c6f2fec744de57bfdabac907116fd1495060b65c5d51da060dbf317f253ca3179787f2de9162c7961e83d3f1c39909bbf7ae252262dcfaf7e4
7
- data.tar.gz: 3965cf5f9798b3529eab55f0eb24b3532950f306005fca4655589d8d27c925028115a976fa623af6ca16e715aa6f8f9993cd8716779356f5c194b7cc5f7d4e6f
6
+ metadata.gz: 19540acad836a3102e52aab746c530c8f78613e4d3ac98ba3844c24e156d1a994aa92c5dcfaa3fd831ffe7d0a8ba26aa036a8e23f982545a81a08fac2e0cab6c
7
+ data.tar.gz: 747484c1f84802be42523c762f213eaa5044257d78546ac8a30c554c101053d6764e264c892fe29df74f265b44afa7829172a697f23996d1443835203266705b
data/Rakefile CHANGED
@@ -32,6 +32,11 @@ task :lint do
32
32
  sh 'bundle exec rubocop'
33
33
  end
34
34
 
35
+ task :format do
36
+ sh 'bundle exec rubocop -A'
37
+ sh 'clang-format -i ext/**/*.{h,cc}'
38
+ end
39
+
35
40
  task deploy: :default do
36
41
  sh 'code -w ./lib/mini_phone/version.rb'
37
42
  version = `ruby -r ./lib/mini_phone/version.rb -e 'print MiniPhone::VERSION'`.strip
@@ -20,15 +20,19 @@ extern "C" struct PhoneNumberInfo {
20
20
  };
21
21
 
22
22
  static inline VALUE is_phone_number_valid(VALUE self, VALUE str, VALUE cc) {
23
+ if (NIL_P(str) || NIL_P(cc)) {
24
+ return Qfalse;
25
+ }
26
+
23
27
  PhoneNumber parsed_number;
24
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
28
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
25
29
 
26
30
  std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
27
31
  std::string country_code(RSTRING_PTR(cc), RSTRING_LEN(cc));
28
32
 
29
- auto result = phone_util->ParseAndKeepRawInput(phone_number, country_code, &parsed_number);
33
+ auto result = phone_util.ParseAndKeepRawInput(phone_number, country_code, &parsed_number);
30
34
 
31
- if (result == PhoneNumberUtil::NO_PARSING_ERROR && phone_util->IsValidNumber(parsed_number)) {
35
+ if (result == PhoneNumberUtil::NO_PARSING_ERROR && phone_util.IsValidNumber(parsed_number)) {
32
36
  return Qtrue;
33
37
  } else {
34
38
  return Qfalse;
@@ -41,6 +45,20 @@ extern "C" VALUE rb_is_phone_number_valid(VALUE self, VALUE str) {
41
45
  return is_phone_number_valid(self, str, def_cc);
42
46
  }
43
47
 
48
+ extern "C" VALUE rb_normalize_digits_only(VALUE self, VALUE str) {
49
+ if (NIL_P(str)) {
50
+ return Qnil;
51
+ }
52
+
53
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
54
+
55
+ std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
56
+
57
+ phone_util.NormalizeDigitsOnly(&phone_number);
58
+
59
+ return rb_str_new(phone_number.c_str(), phone_number.size());
60
+ }
61
+
44
62
  extern "C" VALUE rb_is_phone_number_valid_for_country(VALUE self, VALUE str, VALUE cc) {
45
63
  return is_phone_number_valid(self, str, cc);
46
64
  }
@@ -54,16 +72,20 @@ extern "C" VALUE rb_is_phone_number_invalid_for_country(VALUE self, VALUE str, V
54
72
  }
55
73
 
56
74
  extern "C" VALUE rb_is_phone_number_possible(VALUE self, VALUE str) {
75
+ if (NIL_P(str)) {
76
+ return Qnil;
77
+ }
78
+
57
79
  PhoneNumber parsed_number;
58
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
80
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
59
81
 
60
82
  VALUE def_cc = rb_iv_get(rb_mMiniPhone, "@default_country");
61
83
  std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
62
84
  std::string country_code(RSTRING_PTR(def_cc), RSTRING_LEN(def_cc));
63
85
 
64
- auto result = phone_util->Parse(phone_number, country_code, &parsed_number);
86
+ auto result = phone_util.Parse(phone_number, country_code, &parsed_number);
65
87
 
66
- if (result == PhoneNumberUtil::NO_PARSING_ERROR && phone_util->IsPossibleNumber(parsed_number)) {
88
+ if (result == PhoneNumberUtil::NO_PARSING_ERROR && phone_util.IsPossibleNumber(parsed_number)) {
67
89
  return Qtrue;
68
90
  } else {
69
91
  return Qfalse;
@@ -75,6 +97,10 @@ extern "C" VALUE rb_is_phone_number_impossible(VALUE self, VALUE str) {
75
97
  }
76
98
 
77
99
  extern "C" VALUE rb_set_default_country(VALUE self, VALUE str_code) {
100
+ if (NIL_P(str_code)) {
101
+ str_code = rb_str_new("ZZ", 2);
102
+ }
103
+
78
104
  return rb_iv_set(self, "@default_country", str_code);
79
105
  }
80
106
 
@@ -104,6 +130,7 @@ static inline VALUE rb_phone_number_nullify_ivars(VALUE self) {
104
130
  rb_iv_set(self, "@type", Qnil);
105
131
  rb_iv_set(self, "@valid", Qfalse);
106
132
  rb_iv_set(self, "@possible", Qfalse);
133
+ rb_iv_set(self, "@area_code", Qnil);
107
134
 
108
135
  return Qtrue;
109
136
  }
@@ -131,12 +158,12 @@ extern "C" VALUE rb_phone_number_initialize(int argc, VALUE *argv, VALUE self) {
131
158
 
132
159
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
133
160
 
134
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
161
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
135
162
 
136
163
  std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
137
164
  std::string country_code(RSTRING_PTR(def_cc), RSTRING_LEN(def_cc));
138
165
 
139
- auto result = phone_util->Parse(phone_number, country_code, &parsed_number);
166
+ auto result = phone_util.Parse(phone_number, country_code, &parsed_number);
140
167
 
141
168
  if (result != PhoneNumberUtil::NO_PARSING_ERROR) {
142
169
  rb_phone_number_nullify_ivars(self);
@@ -152,9 +179,9 @@ static inline VALUE rb_phone_number_format(VALUE self, PhoneNumberUtil::PhoneNum
152
179
  PhoneNumberInfo *phone_number_info;
153
180
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
154
181
 
155
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
182
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
156
183
  PhoneNumber parsed_number = phone_number_info->phone_number;
157
- phone_util->Format(parsed_number, fmt, &formatted_number);
184
+ phone_util.Format(parsed_number, fmt, &formatted_number);
158
185
 
159
186
  return rb_str_new(formatted_number.c_str(), formatted_number.size());
160
187
  }
@@ -199,11 +226,11 @@ extern "C" VALUE rb_phone_number_raw_national(VALUE self) {
199
226
 
200
227
  std::string formatted_number;
201
228
  PhoneNumberInfo *phone_number_info;
202
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
229
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
203
230
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
204
231
 
205
- phone_util->FormatByPattern(phone_number_info->phone_number, PhoneNumberUtil::NATIONAL, raw_national_format,
206
- &formatted_number);
232
+ phone_util.FormatByPattern(phone_number_info->phone_number, PhoneNumberUtil::NATIONAL, raw_national_format,
233
+ &formatted_number);
207
234
 
208
235
  VALUE result = rb_str_new(formatted_number.c_str(), formatted_number.size());
209
236
 
@@ -219,9 +246,9 @@ extern "C" VALUE rb_phone_number_valid_eh(VALUE self) {
219
246
  PhoneNumberInfo *phone_number_info;
220
247
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
221
248
 
222
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
249
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
223
250
 
224
- if (phone_util->IsValidNumber(phone_number_info->phone_number)) {
251
+ if (phone_util.IsValidNumber(phone_number_info->phone_number)) {
225
252
  return rb_iv_set(self, "@valid", Qtrue);
226
253
  } else {
227
254
  return rb_iv_set(self, "@valid", Qfalse);
@@ -241,9 +268,9 @@ extern "C" VALUE rb_phone_number_possible_eh(VALUE self) {
241
268
  PhoneNumberInfo *phone_number_info;
242
269
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
243
270
 
244
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
271
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
245
272
 
246
- if (phone_util->IsPossibleNumber(phone_number_info->phone_number)) {
273
+ if (phone_util.IsPossibleNumber(phone_number_info->phone_number)) {
247
274
  return rb_iv_set(self, "@possible", Qtrue);
248
275
  } else {
249
276
  return rb_iv_set(self, "@possible", Qfalse);
@@ -262,9 +289,9 @@ extern "C" VALUE rb_phone_number_region_code(VALUE self) {
262
289
  PhoneNumberInfo *phone_number_info;
263
290
  std::string code;
264
291
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
265
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
292
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
266
293
 
267
- phone_util->GetRegionCodeForCountryCode(phone_number_info->phone_number.country_code(), &code);
294
+ phone_util.GetRegionCodeForCountryCode(phone_number_info->phone_number.country_code(), &code);
268
295
 
269
296
  VALUE result = rb_str_new(code.c_str(), code.size());
270
297
 
@@ -291,14 +318,14 @@ extern "C" VALUE rb_phone_number_eql_eh(VALUE self, VALUE other) {
291
318
  return Qfalse;
292
319
  }
293
320
 
294
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
321
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
295
322
 
296
323
  PhoneNumberInfo *self_phone_number_info;
297
324
  Data_Get_Struct(self, PhoneNumberInfo, self_phone_number_info);
298
325
 
299
326
  PhoneNumberInfo *other_phone_number_info;
300
327
  Data_Get_Struct(other, PhoneNumberInfo, other_phone_number_info);
301
- if (phone_util->IsNumberMatch(other_phone_number_info->phone_number, self_phone_number_info->phone_number)) {
328
+ if (phone_util.IsNumberMatch(other_phone_number_info->phone_number, self_phone_number_info->phone_number)) {
302
329
  return Qtrue;
303
330
  } else {
304
331
  return Qfalse;
@@ -312,13 +339,13 @@ extern "C" VALUE rb_phone_number_type(VALUE self) {
312
339
 
313
340
  PhoneNumberInfo *phone_number_info;
314
341
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
315
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
342
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
316
343
 
317
344
  VALUE result;
318
345
 
319
346
  // @see
320
347
  // https://github.com/google/libphonenumber/blob/4e9954edea7cf263532c5dd3861a801104c3f012/cpp/src/phonenumbers/phonenumberutil.h#L91
321
- switch (phone_util->GetNumberType(phone_number_info->phone_number)) {
348
+ switch (phone_util.GetNumberType(phone_number_info->phone_number)) {
322
349
  case PhoneNumberUtil::PREMIUM_RATE:
323
350
  result = rb_intern("premium_rate");
324
351
  break;
@@ -360,6 +387,35 @@ extern "C" VALUE rb_phone_number_type(VALUE self) {
360
387
  return rb_iv_set(self, "@type", ID2SYM(result));
361
388
  }
362
389
 
390
+ extern "C" VALUE rb_phone_number_area_code(VALUE self) {
391
+ if (rb_ivar_defined(self, rb_intern("@area_code"))) {
392
+ return rb_iv_get(self, "@area_code");
393
+ }
394
+
395
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
396
+ PhoneNumberInfo *phone_number_info;
397
+ Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
398
+
399
+ PhoneNumber number = phone_number_info->phone_number;
400
+ string national_significant_number;
401
+ phone_util.GetNationalSignificantNumber(number, &national_significant_number);
402
+ string area_code;
403
+ string subscriber_number;
404
+
405
+ int area_code_length = phone_util.GetLengthOfGeographicalAreaCode(number);
406
+ if (area_code_length > 0) {
407
+ area_code = national_significant_number.substr(0, area_code_length);
408
+ subscriber_number = national_significant_number.substr(area_code_length, string::npos);
409
+ } else {
410
+ area_code = "";
411
+ subscriber_number = national_significant_number;
412
+ }
413
+
414
+ VALUE result = rb_str_new(area_code.c_str(), area_code.size());
415
+
416
+ return rb_iv_set(self, "@area_code", result);
417
+ }
418
+
363
419
  extern "C" void Init_mini_phone(void) {
364
420
  rb_mMiniPhone = rb_define_module("MiniPhone");
365
421
 
@@ -380,6 +436,8 @@ extern "C" void Init_mini_phone(void) {
380
436
  rb_define_module_function(rb_mMiniPhone, "default_country", reinterpret_cast<VALUE (*)(...)>(rb_get_default_country),
381
437
  0);
382
438
  rb_define_module_function(rb_mMiniPhone, "parse", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_parse), -1);
439
+ rb_define_module_function(rb_mMiniPhone, "normalize_digits_only",
440
+ reinterpret_cast<VALUE (*)(...)>(rb_normalize_digits_only), 1);
383
441
 
384
442
  rb_cPhoneNumber = rb_define_class_under(rb_mMiniPhone, "PhoneNumber", rb_cObject);
385
443
 
@@ -401,7 +459,9 @@ extern "C" void Init_mini_phone(void) {
401
459
  0);
402
460
  rb_define_method(rb_cPhoneNumber, "rfc3966", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_rfc3966), 0);
403
461
  rb_define_method(rb_cPhoneNumber, "region_code", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_region_code), 0);
462
+ rb_define_method(rb_cPhoneNumber, "country", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_region_code), 0);
404
463
  rb_define_method(rb_cPhoneNumber, "country_code", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_country_code), 0);
405
464
  rb_define_method(rb_cPhoneNumber, "type", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_type), 0);
465
+ rb_define_method(rb_cPhoneNumber, "area_code", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_area_code), 0);
406
466
  rb_define_method(rb_cPhoneNumber, "eql?", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_eql_eh), 1);
407
467
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniPhone
4
- VERSION = '1.0.3'
4
+ VERSION = '1.0.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_phone
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer