mini_phone 1.0.0 → 1.0.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: 7aeb4f877c1a92925416a7642c331cc9229233bdc070b877cb1b8801241dd6f8
4
- data.tar.gz: 7262c82936a62c6d6e32123f23b2189a4eddc870cb708eb06784658785080e68
3
+ metadata.gz: e442f98e547a4059112a40d4a28d79b7438ded4ec9d697ed7a59e03447e073f3
4
+ data.tar.gz: b7ff1d3b6c32564197471098acb0654f214a035df703382500a5c7c825b80d9f
5
5
  SHA512:
6
- metadata.gz: e6bf08052c013aa7bbd6a49ebbe47ab8ee356c32866e7b0a2048751ca16c407e9efc65593a400b5fe2440bbc9c5ea424f1489561d05e0e107064d068b1f02a8a
7
- data.tar.gz: 121ef682de85616bf54075dd70246669b2228234795856fe12cc323a58fe7d89685a660084f3c7c5246e5a83bfdd55a4f17b4be08b44a12ae1ce5de21f3a7e21
6
+ metadata.gz: 90e2db831e7edf8ccbe083f3daad5830d8d65d6945598441a4b7bc38c0b191d5aec85c7356b9549375add822e3e6b8d27d550ed647d1721d29fb4113e50e6fc3
7
+ data.tar.gz: adc1643314e3f781e9c427ff6540ba406995c14e9429fa4c520b681375459a73711fa1cbf8072c51c7f2b79f781a44fd81d219e79c6388e0d6c25f3e56e0e781
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # MiniPhone
2
2
 
3
- A Ruby gem which plugs directly in the the Google's native C++
4
- [libphonenumber](https://github.com/google/libphonenumber) for extemely
3
+ A Ruby gem which plugs directly into Google's native C++
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 t0 50x faster than other Ruby phone number
6
+ average, most methods are 40x to 50x faster than other Ruby phone number
7
7
  libraries.
8
8
 
9
9
  ## Usage
@@ -23,10 +23,12 @@ MiniPhone.default_country = 'US'
23
23
 
24
24
  phone_number = MiniPhone.parse('404-384-1399')
25
25
 
26
- phone_number.e164 # +14043841399
27
- phone_number.national # (404) 384-1399
28
- phone_number.international # +1 404-384-1399
29
- phone_number.rfc3966 # tel:+1-404-384-1384
26
+ phone_number.e164 # +14043841399
27
+ phone_number.national # (404) 384-1399
28
+ phone_number.raw_national # 4043841399
29
+ phone_number.dasherized_national # 404-384-1399
30
+ phone_number.international # +1 404-384-1399
31
+ phone_number.rfc3966 # tel:+1-404-384-1384
30
32
  ```
31
33
 
32
34
  ### Checking if a phone number is possible
@@ -41,7 +43,7 @@ phone_number.possible? # false
41
43
 
42
44
  ```ruby
43
45
  MiniPhone.parse('+12423570000').type # :mobile
44
- MiniPhone.parse['+12423651234').type # :fixed_line
46
+ MiniPhone.parse('+12423651234').type # :fixed_line
45
47
  ```
46
48
 
47
49
  The possible types are directly mapped from [this
@@ -65,14 +67,14 @@ enum](https://github.com/google/libphonenumber/blob/4e9954edea7cf263532c5dd3861a
65
67
  ## Compatibility with PhoneLib
66
68
 
67
69
  MiniPhone aims to be compatible with
68
- [Phonelib](https://github.com/daddyz/phonelib) so in many cases it can be a
69
- drop in replacement. It has a smaller feature set, so if you need it it not a
70
- drop in replacement for every use case. If there is a feature you need, open
70
+ [Phonelib](https://github.com/daddyz/phonelib). In many cases it can be a
71
+ drop in replacement. It has a smaller feature set, so it is not a
72
+ replacement for every use case. If there is a feature you need, open
71
73
  an issue and we will try to support it.
72
74
 
73
75
  ## Benchmarks
74
76
 
75
- On average, most methods are 40x t0 50x faster than other libraries. To run
77
+ On average, most methods are 40x to 50x faster than other libraries. To run
76
78
  the benchmarks locally, execute: `bundle exec rake bench`
77
79
 
78
80
  ```
@@ -132,10 +134,10 @@ push git commits and tags, and push the `.gem` file to
132
134
  ## Contributing
133
135
 
134
136
  Bug reports and pull requests are welcome on GitHub at
135
- https://github.com/[USERNAME]/mini_phone. This project is intended to be a
137
+ https://github.com/ianks/mini_phone. This project is intended to be a
136
138
  safe, welcoming space for collaboration, and contributors are expected to
137
139
  adhere to the [code of
138
- conduct](https://github.com/[USERNAME]/mini_phone/blob/master/CODE_OF_CONDUCT.md).
140
+ conduct](https://github.com/ianks/mini_phone/blob/master/CODE_OF_CONDUCT.md).
139
141
 
140
142
  ## License
141
143
 
@@ -146,4 +148,4 @@ License](https://opensource.org/licenses/MIT).
146
148
 
147
149
  Everyone interacting in the MiniPhone project's codebases, issue trackers,
148
150
  chat rooms and mailing lists is expected to follow the [code of
149
- conduct](https://github.com/[USERNAME]/mini_phone/blob/master/CODE_OF_CONDUCT.md).
151
+ conduct](https://github.com/ianks/mini_phone/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ RSpec::Core::RakeTask.new(:spec)
9
9
 
10
10
  task build: :compile
11
11
 
12
- task default: %i[clobber compile spec]
12
+ task default: %i[clobber compile spec lint]
13
13
 
14
14
  spec = Gem::Specification.load(File.expand_path('mini_phone.gemspec', __dir__))
15
15
 
@@ -28,7 +28,16 @@ task bench: %i[clobber compile] do
28
28
  end
29
29
  end
30
30
 
31
- task :deploy do
31
+ task :lint do
32
+ sh 'bundle exec rubocop'
33
+ end
34
+
35
+ task :format do
36
+ sh 'bundle exec rubocop -A'
37
+ sh 'clang-format -i ext/**/*.{h,cc}'
38
+ end
39
+
40
+ task deploy: :default do
32
41
  sh 'code -w ./lib/mini_phone/version.rb'
33
42
  version = `ruby -r ./lib/mini_phone/version.rb -e 'print MiniPhone::VERSION'`.strip
34
43
  sh "git commit -am 'Bump to v#{version} :confetti_ball:'"
@@ -63,11 +72,7 @@ namespace :publish do
63
72
  task non_native: [:gem] do
64
73
  g = "./pkg/mini_phone-#{MiniPhone::VERSION}.gem"
65
74
 
66
- results = []
67
-
68
- results << push_to_rubygems(g)
69
- results << push_to_github_registry(g)
70
-
71
- abort if results.all? { |r| r == true }
75
+ push_to_rubygems(g)
76
+ push_to_github_registry(g)
72
77
  end
73
78
  end
@@ -4,47 +4,6 @@
4
4
 
5
5
  require 'mkmf'
6
6
 
7
- LIBDIR = RbConfig::CONFIG['libdir']
8
-
9
- INCLUDEDIR = RbConfig::CONFIG['includedir']
10
-
11
- header_dirs = [
12
- # First search /opt/local for macports
13
- '/opt/local/include',
14
-
15
- # Then search /usr/local for people that installed from source
16
- '/usr/local/include',
17
-
18
- # Check the ruby install locations
19
- INCLUDEDIR,
20
-
21
- # Finally fall back to /usr
22
- '/usr/include'
23
- ]
24
-
25
- lib_dirs = [
26
- # First search /opt/local for macports
27
- '/opt/local/lib',
28
-
29
- # Then search /usr/local for people that installed from source
30
- '/usr/local/lib',
31
-
32
- # Check the ruby install locations
33
- LIBDIR,
34
-
35
- # Finally fall back to /usr
36
- '/usr/lib'
37
- ]
38
-
39
- # Detect homebrew installs
40
- if find_executable('brew')
41
- brew_prefix = `brew --prefix`.strip
42
- header_dirs.unshift "#{brew_prefix}/include"
43
- lib_dirs.unshift "#{brew_prefix}/lib"
44
- end
45
-
46
- dir_config('mini_phone', header_dirs, lib_dirs)
47
-
48
7
  unless have_library('phonenumber')
49
8
  abort <<~MSG
50
9
 
@@ -70,7 +29,9 @@ unless have_library('phonenumber')
70
29
  MSG
71
30
  end
72
31
 
73
- $CXXFLAGS += ' -std=c++11 '
32
+ dir_config('mini_phone')
33
+
34
+ $CXXFLAGS += ' -std=c++11 -ofast '
74
35
 
75
36
  create_makefile('mini_phone/mini_phone')
76
37
 
@@ -1,12 +1,19 @@
1
1
  #include "mini_phone.h"
2
+ #include "phonenumbers/phonemetadata.pb.h"
3
+ #include "phonenumbers/phonenumber.pb.h"
2
4
  #include "phonenumbers/phonenumberutil.h"
3
5
 
4
6
  using namespace ::i18n::phonenumbers;
5
7
 
8
+ using google::protobuf::RepeatedPtrField;
9
+
6
10
  static VALUE rb_mMiniPhone;
7
11
 
8
12
  static VALUE rb_cPhoneNumber;
9
13
 
14
+ static RepeatedPtrField<NumberFormat> raw_national_format;
15
+ static RepeatedPtrField<NumberFormat> dasherized_national_format;
16
+
10
17
  extern "C" struct PhoneNumberInfo {
11
18
  PhoneNumber phone_number;
12
19
  std::string raw_phone_number;
@@ -14,15 +21,19 @@ extern "C" struct PhoneNumberInfo {
14
21
  };
15
22
 
16
23
  static inline VALUE is_phone_number_valid(VALUE self, VALUE str, VALUE cc) {
24
+ if (NIL_P(str) || NIL_P(cc)) {
25
+ return Qfalse;
26
+ }
27
+
17
28
  PhoneNumber parsed_number;
18
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
29
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
19
30
 
20
31
  std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
21
32
  std::string country_code(RSTRING_PTR(cc), RSTRING_LEN(cc));
22
33
 
23
- auto result = phone_util->ParseAndKeepRawInput(phone_number, country_code, &parsed_number);
34
+ auto result = phone_util.ParseAndKeepRawInput(phone_number, country_code, &parsed_number);
24
35
 
25
- if (result == PhoneNumberUtil::NO_PARSING_ERROR && phone_util->IsValidNumber(parsed_number)) {
36
+ if (result == PhoneNumberUtil::NO_PARSING_ERROR && phone_util.IsValidNumber(parsed_number)) {
26
37
  return Qtrue;
27
38
  } else {
28
39
  return Qfalse;
@@ -35,6 +46,20 @@ extern "C" VALUE rb_is_phone_number_valid(VALUE self, VALUE str) {
35
46
  return is_phone_number_valid(self, str, def_cc);
36
47
  }
37
48
 
49
+ extern "C" VALUE rb_normalize_digits_only(VALUE self, VALUE str) {
50
+ if (NIL_P(str)) {
51
+ return Qnil;
52
+ }
53
+
54
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
55
+
56
+ std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
57
+
58
+ phone_util.NormalizeDigitsOnly(&phone_number);
59
+
60
+ return rb_str_new(phone_number.c_str(), phone_number.size());
61
+ }
62
+
38
63
  extern "C" VALUE rb_is_phone_number_valid_for_country(VALUE self, VALUE str, VALUE cc) {
39
64
  return is_phone_number_valid(self, str, cc);
40
65
  }
@@ -48,16 +73,20 @@ extern "C" VALUE rb_is_phone_number_invalid_for_country(VALUE self, VALUE str, V
48
73
  }
49
74
 
50
75
  extern "C" VALUE rb_is_phone_number_possible(VALUE self, VALUE str) {
76
+ if (NIL_P(str)) {
77
+ return Qnil;
78
+ }
79
+
51
80
  PhoneNumber parsed_number;
52
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
81
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
53
82
 
54
83
  VALUE def_cc = rb_iv_get(rb_mMiniPhone, "@default_country");
55
84
  std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
56
85
  std::string country_code(RSTRING_PTR(def_cc), RSTRING_LEN(def_cc));
57
86
 
58
- auto result = phone_util->Parse(phone_number, country_code, &parsed_number);
87
+ auto result = phone_util.Parse(phone_number, country_code, &parsed_number);
59
88
 
60
- if (result == PhoneNumberUtil::NO_PARSING_ERROR && phone_util->IsPossibleNumber(parsed_number)) {
89
+ if (result == PhoneNumberUtil::NO_PARSING_ERROR && phone_util.IsPossibleNumber(parsed_number)) {
61
90
  return Qtrue;
62
91
  } else {
63
92
  return Qfalse;
@@ -69,6 +98,10 @@ extern "C" VALUE rb_is_phone_number_impossible(VALUE self, VALUE str) {
69
98
  }
70
99
 
71
100
  extern "C" VALUE rb_set_default_country(VALUE self, VALUE str_code) {
101
+ if (NIL_P(str_code)) {
102
+ str_code = rb_str_new("ZZ", 2);
103
+ }
104
+
72
105
  return rb_iv_set(self, "@default_country", str_code);
73
106
  }
74
107
 
@@ -89,6 +122,8 @@ extern "C" VALUE rb_phone_number_alloc(VALUE self) {
89
122
 
90
123
  static inline VALUE rb_phone_number_nullify_ivars(VALUE self) {
91
124
  rb_iv_set(self, "@national", Qnil);
125
+ rb_iv_set(self, "@raw_national", Qnil);
126
+ rb_iv_set(self, "@dasherized_national", Qnil);
92
127
  rb_iv_set(self, "@international", Qnil);
93
128
  rb_iv_set(self, "@e164", Qnil);
94
129
  rb_iv_set(self, "@country_code", Qnil);
@@ -97,6 +132,7 @@ static inline VALUE rb_phone_number_nullify_ivars(VALUE self) {
97
132
  rb_iv_set(self, "@type", Qnil);
98
133
  rb_iv_set(self, "@valid", Qfalse);
99
134
  rb_iv_set(self, "@possible", Qfalse);
135
+ rb_iv_set(self, "@area_code", Qnil);
100
136
 
101
137
  return Qtrue;
102
138
  }
@@ -124,12 +160,12 @@ extern "C" VALUE rb_phone_number_initialize(int argc, VALUE *argv, VALUE self) {
124
160
 
125
161
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
126
162
 
127
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
163
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
128
164
 
129
165
  std::string phone_number(RSTRING_PTR(str), RSTRING_LEN(str));
130
166
  std::string country_code(RSTRING_PTR(def_cc), RSTRING_LEN(def_cc));
131
167
 
132
- auto result = phone_util->Parse(phone_number, country_code, &parsed_number);
168
+ auto result = phone_util.Parse(phone_number, country_code, &parsed_number);
133
169
 
134
170
  if (result != PhoneNumberUtil::NO_PARSING_ERROR) {
135
171
  rb_phone_number_nullify_ivars(self);
@@ -145,9 +181,9 @@ static inline VALUE rb_phone_number_format(VALUE self, PhoneNumberUtil::PhoneNum
145
181
  PhoneNumberInfo *phone_number_info;
146
182
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
147
183
 
148
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
184
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
149
185
  PhoneNumber parsed_number = phone_number_info->phone_number;
150
- phone_util->Format(parsed_number, fmt, &formatted_number);
186
+ phone_util.Format(parsed_number, fmt, &formatted_number);
151
187
 
152
188
  return rb_str_new(formatted_number.c_str(), formatted_number.size());
153
189
  }
@@ -185,6 +221,37 @@ extern "C" VALUE rb_phone_number_rfc3966(VALUE self) {
185
221
  return rb_iv_set(self, "@rfc3966", rb_phone_number_format(self, PhoneNumberUtil::PhoneNumberFormat::RFC3966));
186
222
  }
187
223
 
224
+ VALUE format_by_pattern_national(VALUE self, RepeatedPtrField<NumberFormat> format) {
225
+ std::string formatted_number;
226
+ PhoneNumberInfo *phone_number_info;
227
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
228
+ Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
229
+
230
+ phone_util.FormatByPattern(phone_number_info->phone_number, PhoneNumberUtil::NATIONAL, format, &formatted_number);
231
+
232
+ return rb_str_new(formatted_number.c_str(), formatted_number.size());
233
+ }
234
+
235
+ extern "C" VALUE rb_phone_number_raw_national(VALUE self) {
236
+ if (rb_ivar_defined(self, rb_intern("@raw_national"))) {
237
+ return rb_iv_get(self, "@raw_national");
238
+ }
239
+
240
+ VALUE result = format_by_pattern_national(self, raw_national_format);
241
+
242
+ return rb_iv_set(self, "@raw_national", result);
243
+ }
244
+
245
+ extern "C" VALUE rb_phone_number_dasherized_national(VALUE self) {
246
+ if (rb_ivar_defined(self, rb_intern("@dasherized_national"))) {
247
+ return rb_iv_get(self, "@dasherized_national");
248
+ }
249
+
250
+ VALUE result = format_by_pattern_national(self, dasherized_national_format);
251
+
252
+ return rb_iv_set(self, "@dasherized_national", result);
253
+ }
254
+
188
255
  extern "C" VALUE rb_phone_number_valid_eh(VALUE self) {
189
256
  if (rb_ivar_defined(self, rb_intern("@valid"))) {
190
257
  return rb_iv_get(self, "@valid");
@@ -194,9 +261,9 @@ extern "C" VALUE rb_phone_number_valid_eh(VALUE self) {
194
261
  PhoneNumberInfo *phone_number_info;
195
262
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
196
263
 
197
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
264
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
198
265
 
199
- if (phone_util->IsValidNumber(phone_number_info->phone_number)) {
266
+ if (phone_util.IsValidNumber(phone_number_info->phone_number)) {
200
267
  return rb_iv_set(self, "@valid", Qtrue);
201
268
  } else {
202
269
  return rb_iv_set(self, "@valid", Qfalse);
@@ -216,9 +283,9 @@ extern "C" VALUE rb_phone_number_possible_eh(VALUE self) {
216
283
  PhoneNumberInfo *phone_number_info;
217
284
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
218
285
 
219
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
286
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
220
287
 
221
- if (phone_util->IsPossibleNumber(phone_number_info->phone_number)) {
288
+ if (phone_util.IsPossibleNumber(phone_number_info->phone_number)) {
222
289
  return rb_iv_set(self, "@possible", Qtrue);
223
290
  } else {
224
291
  return rb_iv_set(self, "@possible", Qfalse);
@@ -237,9 +304,9 @@ extern "C" VALUE rb_phone_number_region_code(VALUE self) {
237
304
  PhoneNumberInfo *phone_number_info;
238
305
  std::string code;
239
306
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
240
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
307
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
241
308
 
242
- phone_util->GetRegionCodeForCountryCode(phone_number_info->phone_number.country_code(), &code);
309
+ phone_util.GetRegionCodeForCountryCode(phone_number_info->phone_number.country_code(), &code);
243
310
 
244
311
  VALUE result = rb_str_new(code.c_str(), code.size());
245
312
 
@@ -266,14 +333,14 @@ extern "C" VALUE rb_phone_number_eql_eh(VALUE self, VALUE other) {
266
333
  return Qfalse;
267
334
  }
268
335
 
269
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
336
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
270
337
 
271
338
  PhoneNumberInfo *self_phone_number_info;
272
339
  Data_Get_Struct(self, PhoneNumberInfo, self_phone_number_info);
273
340
 
274
341
  PhoneNumberInfo *other_phone_number_info;
275
342
  Data_Get_Struct(other, PhoneNumberInfo, other_phone_number_info);
276
- if (phone_util->IsNumberMatch(other_phone_number_info->phone_number, self_phone_number_info->phone_number)) {
343
+ if (phone_util.IsNumberMatch(other_phone_number_info->phone_number, self_phone_number_info->phone_number)) {
277
344
  return Qtrue;
278
345
  } else {
279
346
  return Qfalse;
@@ -287,13 +354,13 @@ extern "C" VALUE rb_phone_number_type(VALUE self) {
287
354
 
288
355
  PhoneNumberInfo *phone_number_info;
289
356
  Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
290
- PhoneNumberUtil *phone_util = PhoneNumberUtil::GetInstance();
357
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
291
358
 
292
359
  VALUE result;
293
360
 
294
361
  // @see
295
362
  // https://github.com/google/libphonenumber/blob/4e9954edea7cf263532c5dd3861a801104c3f012/cpp/src/phonenumbers/phonenumberutil.h#L91
296
- switch (phone_util->GetNumberType(phone_number_info->phone_number)) {
363
+ switch (phone_util.GetNumberType(phone_number_info->phone_number)) {
297
364
  case PhoneNumberUtil::PREMIUM_RATE:
298
365
  result = rb_intern("premium_rate");
299
366
  break;
@@ -335,7 +402,50 @@ extern "C" VALUE rb_phone_number_type(VALUE self) {
335
402
  return rb_iv_set(self, "@type", ID2SYM(result));
336
403
  }
337
404
 
405
+ extern "C" VALUE rb_phone_number_area_code(VALUE self) {
406
+ if (rb_ivar_defined(self, rb_intern("@area_code"))) {
407
+ return rb_iv_get(self, "@area_code");
408
+ }
409
+
410
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
411
+ PhoneNumberInfo *phone_number_info;
412
+ Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
413
+
414
+ PhoneNumber number = phone_number_info->phone_number;
415
+ string national_significant_number;
416
+ phone_util.GetNationalSignificantNumber(number, &national_significant_number);
417
+ string area_code;
418
+ string subscriber_number;
419
+
420
+ int area_code_length = phone_util.GetLengthOfGeographicalAreaCode(number);
421
+ if (area_code_length > 0) {
422
+ area_code = national_significant_number.substr(0, area_code_length);
423
+ subscriber_number = national_significant_number.substr(area_code_length, string::npos);
424
+ } else {
425
+ area_code = "";
426
+ subscriber_number = national_significant_number;
427
+ }
428
+
429
+ VALUE result = rb_str_new(area_code.c_str(), area_code.size());
430
+
431
+ return rb_iv_set(self, "@area_code", result);
432
+ }
433
+
434
+ static inline void setup_formats() {
435
+ // Raw
436
+ NumberFormat *raw_fmt = raw_national_format.Add();
437
+ raw_fmt->set_pattern("(\\d{3})(\\d{3})(\\d{4})");
438
+ raw_fmt->set_format("$1$2$3");
439
+
440
+ // Dasherized
441
+ NumberFormat *dsh_fmt = dasherized_national_format.Add();
442
+ dsh_fmt->set_pattern("(\\d{3})(\\d{3})(\\d{4})");
443
+ dsh_fmt->set_format("$1-$2-$3");
444
+ }
445
+
338
446
  extern "C" void Init_mini_phone(void) {
447
+ setup_formats();
448
+
339
449
  rb_mMiniPhone = rb_define_module("MiniPhone");
340
450
 
341
451
  // Unknown
@@ -350,11 +460,13 @@ extern "C" void Init_mini_phone(void) {
350
460
  rb_define_module_function(rb_mMiniPhone, "possible?", reinterpret_cast<VALUE (*)(...)>(rb_is_phone_number_valid), 1);
351
461
  rb_define_module_function(rb_mMiniPhone, "impossible?", reinterpret_cast<VALUE (*)(...)>(rb_is_phone_number_invalid),
352
462
  1);
353
- rb_define_module_function(rb_mMiniPhone,
354
- "default_country=", reinterpret_cast<VALUE (*)(...)>(rb_set_default_country), 1);
355
- rb_define_module_function(rb_mMiniPhone, "default_country",
356
- reinterpret_cast<VALUE (*)(...)>(rb_get_default_country), 0);
463
+ rb_define_module_function(rb_mMiniPhone, "default_country=", reinterpret_cast<VALUE (*)(...)>(rb_set_default_country),
464
+ 1);
465
+ rb_define_module_function(rb_mMiniPhone, "default_country", reinterpret_cast<VALUE (*)(...)>(rb_get_default_country),
466
+ 0);
357
467
  rb_define_module_function(rb_mMiniPhone, "parse", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_parse), -1);
468
+ rb_define_module_function(rb_mMiniPhone, "normalize_digits_only",
469
+ reinterpret_cast<VALUE (*)(...)>(rb_normalize_digits_only), 1);
358
470
 
359
471
  rb_cPhoneNumber = rb_define_class_under(rb_mMiniPhone, "PhoneNumber", rb_cObject);
360
472
 
@@ -367,11 +479,17 @@ extern "C" void Init_mini_phone(void) {
367
479
  rb_define_method(rb_cPhoneNumber, "impossible?", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_impossible_eh), 0);
368
480
  rb_define_method(rb_cPhoneNumber, "e164", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_e164), 0);
369
481
  rb_define_method(rb_cPhoneNumber, "national", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_national), 0);
482
+
483
+ rb_define_method(rb_cPhoneNumber, "raw_national", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_raw_national), 0);
484
+ rb_define_method(rb_cPhoneNumber, "dasherized_national",
485
+ reinterpret_cast<VALUE (*)(...)>(rb_phone_number_dasherized_national), 0);
370
486
  rb_define_method(rb_cPhoneNumber, "international", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_international),
371
487
  0);
372
488
  rb_define_method(rb_cPhoneNumber, "rfc3966", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_rfc3966), 0);
373
489
  rb_define_method(rb_cPhoneNumber, "region_code", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_region_code), 0);
490
+ rb_define_method(rb_cPhoneNumber, "country", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_region_code), 0);
374
491
  rb_define_method(rb_cPhoneNumber, "country_code", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_country_code), 0);
375
492
  rb_define_method(rb_cPhoneNumber, "type", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_type), 0);
493
+ rb_define_method(rb_cPhoneNumber, "area_code", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_area_code), 0);
376
494
  rb_define_method(rb_cPhoneNumber, "eql?", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_eql_eh), 1);
377
495
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniPhone
4
- VERSION = '1.0.0'
4
+ VERSION = '1.0.5'
5
5
  end
@@ -27,7 +27,10 @@ Gem::Specification.new do |spec|
27
27
  # Specify which files should be added to the gem when it is released.
28
28
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
29
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
30
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(bin|spec|bench)/}) }
30
+ `git ls-files -z`
31
+ .split("\x0")
32
+ .reject { |f| f.match(%r{^(bin|spec|bench)/}) }
33
+ .reject { |f| f.start_with?('.') }
31
34
  end
32
35
  spec.bindir = 'exe'
33
36
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
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.0.0
4
+ version: 1.0.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-08 00:00:00.000000000 Z
11
+ date: 2021-01-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.
@@ -20,13 +20,6 @@ extensions:
20
20
  - ext/mini_phone/extconf.rb
21
21
  extra_rdoc_files: []
22
22
  files:
23
- - ".clang-format"
24
- - ".github/workflows/ci.yml"
25
- - ".github/workflows/release.yml"
26
- - ".gitignore"
27
- - ".rspec"
28
- - ".rubocop.yml"
29
- - ".travis.yml"
30
23
  - CHANGELOG.md
31
24
  - CODE_OF_CONDUCT.md
32
25
  - Gemfile
@@ -1,3 +0,0 @@
1
- ---
2
- BasedOnStyle: LLVM
3
- ColumnLimit: 120
@@ -1,51 +0,0 @@
1
- ---
2
- name: CI
3
-
4
- on: push
5
-
6
- jobs:
7
- build:
8
- strategy:
9
- fail-fast: false
10
- matrix:
11
- ruby: ["2.5", "2.6", "2.7", "3.0"]
12
- os: [ubuntu-latest, macos-latest]
13
- experimental: [false]
14
- # include:
15
- # - ruby: "truffleruby"
16
- # os: ubuntu-latest
17
- # experimental: true
18
- # - ruby: "truffleruby"
19
- # os: macos-latest
20
- # experimental: true
21
- name: ${{ matrix.ruby }} on ${{ matrix.os }}
22
- runs-on: ${{ matrix.os }}
23
- continue-on-error: ${{ matrix.experimental }}
24
- env:
25
- CI_EXPERIMENTAL: ${{ matrix.experimental }}
26
- steps:
27
- - uses: actions/checkout@v2
28
-
29
- - uses: ruby/setup-ruby@v1
30
- with:
31
- ruby-version: ${{ matrix.ruby }}
32
-
33
- - name: Install dependencies (system)
34
- if: ${{ matrix.os == 'ubuntu-latest' }}
35
- run: sudo apt-get -yqq install libphonenumber-dev
36
-
37
- - name: Install dependencies (system)
38
- if: ${{ matrix.os == 'macos-latest' }}
39
- run: brew install --build-from-source libphonenumber && brew link libphonenumber
40
-
41
- - name: Install dependencies (ruby)
42
- run: gem install bundler && bundle install --jobs 4 --retry 3
43
-
44
- - name: Compile
45
- run: bundle exec rake compile
46
-
47
- - name: RSpec
48
- run: bundle exec rspec --format RSpec::Github::Formatter --format documentation
49
-
50
- - name: Rubocop
51
- run: bundle exec rubocop --format github
@@ -1,55 +0,0 @@
1
- ---
2
- name: Release
3
-
4
- on:
5
- push:
6
- tags:
7
- - "v*"
8
-
9
- jobs:
10
- build:
11
- strategy:
12
- fail-fast: false
13
- matrix:
14
- ruby: ["2.7"]
15
- os: [ubuntu-latest, macos-latest]
16
- name: ${{ matrix.os }}
17
- runs-on: ${{ matrix.os }}
18
- steps:
19
- - uses: actions/checkout@v2
20
-
21
- - uses: ruby/setup-ruby@v1
22
- with:
23
- ruby-version: ${{ matrix.ruby }}
24
-
25
- - name: Install dependencies (system)
26
- if: ${{ matrix.os == 'ubuntu-latest' }}
27
- run: sudo apt-get -yqq install libphonenumber-dev
28
-
29
- - name: Install dependencies (system)
30
- if: ${{ matrix.os == 'macos-latest' }}
31
- run: brew install --build-from-source libphonenumber && brew link libphonenumber
32
-
33
- - name: Install dependencies (ruby)
34
- run: gem install bundler && bundle install --jobs 4 --retry 3
35
-
36
- - name: Login
37
- run: |
38
- mkdir -p ~/.gem
39
-
40
- cat << EOF > ~/.gem/credentials
41
- ---
42
- :github: ${GITHUB_AUTH_TOKEN}
43
- :rubygems_api_key: ${RUBYGEMS_AUTH_TOKEN}
44
- EOF
45
-
46
- chmod 0600 ~/.gem/credentials
47
- env:
48
- GITHUB_AUTH_TOKEN: "Bearer ${{secrets.GITHUB_TOKEN}}"
49
- RUBYGEMS_AUTH_TOKEN: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
50
- OWNER: ${{ github.repository_owner }}
51
-
52
- - name: 🛳 Ship it
53
- run: |
54
- bundle exec rake publish:native
55
- bundle exec rake publish:non_native || true
data/.gitignore DELETED
@@ -1,19 +0,0 @@
1
- .bundle/
2
- .yardoc
3
- _yardoc/
4
- coverage/
5
- doc/
6
- pkg/
7
- spec/reports/
8
- tmp/
9
- *.bundle
10
- *.so
11
- *.o
12
- *.a
13
- mkmf.log
14
-
15
- # rspec failure tracking
16
- .rspec_status
17
- Gemfile.lock
18
- vendor/cache/
19
- *.gem
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
@@ -1,23 +0,0 @@
1
- # The behavior of RuboCop can be controlled via the .rubocop.yml
2
- # configuration file. It makes it possible to enable/disable
3
- # certain cops (checks) and to alter their behavior if they accept
4
- # any parameters. The file can be placed either in your home
5
- # directory or in some project directory.
6
- #
7
- # RuboCop will start looking for the configuration file in the directory
8
- # where the inspected file is and continue its way up to the root directory.
9
- #
10
- # See https://docs.rubocop.org/rubocop/configuration
11
- AllCops:
12
- TargetRubyVersion: 2.5
13
- NewCops: enable
14
- SuggestExtensions: false
15
- Exclude:
16
- - pkg/**/*
17
- - tmp/**/*
18
-
19
- Metrics/BlockLength:
20
- Exclude:
21
- - spec/**/*
22
- - Rakefile
23
- - Gemfile
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.7.1
6
- before_install: gem install bundler -v 2.1.4