fakie 0.1.2 → 0.1.3

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
  SHA1:
3
- metadata.gz: bddcb20076c5823e1b26bc9bb9ef5ca10df05889
4
- data.tar.gz: 660b1e6cafbf3be204bb4854924706cc4a1ce61e
3
+ metadata.gz: 2281c84fa85ec11967eb067737b61863ac3843bc
4
+ data.tar.gz: 5f8d21f3033dcba1701a77ae65edc0af25cb987d
5
5
  SHA512:
6
- metadata.gz: b1a720adf32ab75f7a1808c56a9c6c4da4345864d9f6d03d7d2cea7d7518f21df05ea11ffd1d6d96d09abd1a5dce177a9c20010c0a539029f4e37032c075b10b
7
- data.tar.gz: c035c6cf7a5021c0496d23f8487ae8d654f687a2de81551ce8bb32e2d64a5646f91f9ab0fd0de00e41045854148f2f915b7c4c5373ecdcf616aead3c6e63c47c
6
+ metadata.gz: 2f0b3f168a97407a9b2aec652ab7f88084c145225b52adff88d01295501d278f8b49245f610e97abc2b4d10c2ec66398f8d76d2645f18df8b466872fab000db5
7
+ data.tar.gz: e252c165bfc50b32b8d8e18cb8a3013003c9481e88147bb1c5a03889bbd2e4749894cf2f1bb4553c6daf915df500b54531a9214c43f4c22a8b90c4545d85d81c
@@ -4,7 +4,7 @@ module Fakie
4
4
 
5
5
  # ExecJS Context
6
6
  # @return [ExecJS::ExternalRuntime::Context] context for executing JavaScript against libphonenumber and Fakie
7
- def js_context
7
+ def context
8
8
  @@_js_context ||= begin
9
9
  require 'execjs'
10
10
  js_dir = File.join(File.expand_path(File.dirname(__FILE__)), 'js')
@@ -17,14 +17,14 @@ module Fakie
17
17
  # Call a function against the context
18
18
  # @param function [String] function name
19
19
  # @param args [* String] list of arguments to send to the function
20
- def js_call(function, *args)
21
- js_context.call(function, *args)
20
+ def call(function, *args)
21
+ context.call(function, *args)
22
22
  end
23
23
 
24
24
  # Call a function against the context
25
25
  # @param function [String] JavaScript code to evaluate
26
- def js_eval(script)
27
- js_context.eval(script)
26
+ def eval(script)
27
+ context.eval(script)
28
28
  end
29
29
  end
30
30
  end
@@ -69,35 +69,17 @@ var Fakie = {
69
69
 
70
70
  result['e164'] = phoneUtil.format(number, i18n.phonenumbers.PhoneNumberFormat.E164);
71
71
 
72
- return result;
73
- },
74
-
75
- countryForE164Number: function(phone) {
76
- try {
77
- var phone = Fakie.cleanPhone(phone);
78
- var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
79
- var number = phoneUtil.parseAndKeepRawInput(phone);
80
- return phoneUtil.getRegionCodeForNumber(number);
81
- } catch (e) {
82
- return "";
72
+ var nationalSignificantNumber = phoneUtil.getNationalSignificantNumber(number);
73
+ var areaCodeLength = phoneUtil.getLengthOfGeographicalAreaCode(number);
74
+ if (areaCodeLength > 0) {
75
+ result['area_code'] = nationalSignificantNumber.substring(0, areaCodeLength);
83
76
  }
84
- },
85
77
 
86
- formatE164: function(country, phone) {
87
- try {
88
- var phone = Fakie.cleanPhone(phone);
89
- var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
90
- var number = phoneUtil.parseAndKeepRawInput(phone, country);
91
- var PNF = i18n.phonenumbers.PhoneNumberFormat;
92
- return phoneUtil.format(number, PNF.E164);
93
- } catch (e) {
94
- return phone;
95
- }
78
+ return result;
96
79
  },
97
80
 
98
81
  formatInternational: function(country, phone) {
99
82
  try {
100
- var phone = Fakie.cleanPhone(phone);
101
83
  var formatter = new i18n.phonenumbers.AsYouTypeFormatter(country);
102
84
  var output = new goog.string.StringBuffer();
103
85
  for (var i = 0; i < phone.length; ++i) {
@@ -112,7 +94,6 @@ var Fakie = {
112
94
 
113
95
  formatLocal: function(country, phone) {
114
96
  try {
115
- var phone = Fakie.cleanPhone(phone);
116
97
  var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
117
98
  var number = phoneUtil.parseAndKeepRawInput(phone, country);
118
99
  if (phoneUtil.isValidNumberForRegion(number, country)) {
@@ -124,15 +105,5 @@ var Fakie = {
124
105
  } catch (e) {
125
106
  return Fakie.formatInternational(country, phone);
126
107
  }
127
- },
128
-
129
- cleanPhone: function(phone) {
130
- phone = phone.replace(/[^\d\+]/g,'');
131
- if (phone.substr(0, 1) == "+") {
132
- phone = "+" + phone.replace(/[^\d]/g,'');
133
- } else {
134
- phone = phone.replace(/[^\d]/g,'');
135
- }
136
- return phone;
137
108
  }
138
109
  };
@@ -1,23 +1,20 @@
1
1
  module Fakie
2
2
  class PhoneNumber
3
- include JavaScript
4
-
5
3
  class << self
6
- include JavaScript
7
-
8
4
  # Parse a phone number
9
5
  # @param phone_number [String] phone number to parse
10
6
  # @option options default_country [String] ISO 3166-1 two-letter country code
11
7
  # @return [PhoneNumber] phone number object
12
8
  def parse(phone_number, options = {})
13
9
  region_code = options[:default_country]
14
- self.new(js_call('Fakie.parse', phone_number, region_code))
10
+ self.new(JavaScript.call('Fakie.parse', phone_number, region_code))
15
11
  end
16
12
  end
17
13
 
18
14
  attr_reader :e164
19
15
  attr_reader :country_code
20
16
  attr_reader :national_number
17
+ attr_reader :area_code
21
18
  attr_reader :raw_input
22
19
  attr_reader :country_code_source
23
20
  attr_reader :preferred_domestic_carrier_code
@@ -29,6 +26,7 @@ module Fakie
29
26
  @e164 = hash['e164']
30
27
  @country_code = hash['country_code']
31
28
  @national_number = hash['national_number']
29
+ @area_code = hash['area_code']
32
30
  @raw_input = hash['raw_input'].to_s
33
31
  @country_code_source = hash['country_code_source']
34
32
  @preferred_domestic_carrier_code = hash['preferred_domestic_carrier_code']
@@ -48,12 +46,12 @@ module Fakie
48
46
 
49
47
  def international_format(region = self.region_code)
50
48
  raise InvalidPhoneNumber unless self.is_valid?
51
- @international_format ||= js_call('Fakie.formatInternational', region, self.e164)
49
+ @international_format ||= JavaScript.call('Fakie.formatInternational', region, self.e164)
52
50
  end
53
51
 
54
52
  def local_format(region = self.region_code)
55
53
  raise InvalidPhoneNumber unless self.is_valid?
56
- @local_format ||= js_call('Fakie.formatLocal', region, self.e164)
54
+ @local_format ||= JavaScript.call('Fakie.formatLocal', region, self.e164)
57
55
  end
58
56
 
59
57
  def country_name
data/lib/fakie/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fakie
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ module Fakie
4
+ class JavaScriptTest < TestCase
5
+ def test_eval
6
+ assert_equal '415', JavaScript.eval('Fakie.parse("+14155550123")["area_code"]')
7
+ end
8
+ end
9
+ end
@@ -6,9 +6,11 @@ module Fakie
6
6
  phone_number = Fakie.parse('+1 415 555 0123')
7
7
  assert_equal 1, phone_number.country_code
8
8
  assert_equal 4155550123, phone_number.national_number
9
+ assert_equal '415', phone_number.area_code
9
10
  assert_equal '+1 415 555 0123', phone_number.raw_input
10
11
  assert_equal '+14155550123', phone_number.e164
11
12
  assert_equal 'US', phone_number.region_code
13
+ assert_equal 'United States', phone_number.country_name
12
14
  assert phone_number.is_possible?
13
15
  assert phone_number.is_valid?
14
16
  end
data/test/fakie_test.rb CHANGED
@@ -5,5 +5,9 @@ module Fakie
5
5
  def test_parse
6
6
  assert_kind_of PhoneNumber, Fakie.parse('+14155550123')
7
7
  end
8
+
9
+ def test_invalid_country
10
+ refute Fakie.country_name_for_region_code('ZZ')
11
+ end
8
12
  end
9
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Soffes
@@ -47,6 +47,7 @@ files:
47
47
  - lib/fakie/phone_number.rb
48
48
  - lib/fakie/version.rb
49
49
  - tasks/import.rake
50
+ - test/fakie/java_script_test.rb
50
51
  - test/fakie/phone_number_test.rb
51
52
  - test/fakie_test.rb
52
53
  - test/js.html
@@ -76,6 +77,7 @@ signing_key:
76
77
  specification_version: 4
77
78
  summary: libphonenumber wrapper with ExecJS
78
79
  test_files:
80
+ - test/fakie/java_script_test.rb
79
81
  - test/fakie/phone_number_test.rb
80
82
  - test/fakie_test.rb
81
83
  - test/js.html