fakie 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,65 @@
1
+ module Fakie
2
+ class PhoneNumber
3
+ include JavaScript
4
+
5
+ class << self
6
+ include JavaScript
7
+
8
+ # Parse a phone number
9
+ # @param phone_number [String] phone number to parse
10
+ # @option options default_country [String] ISO 3166-1 two-letter country code
11
+ # @return [PhoneNumber] phone number object
12
+ def parse(phone_number, options = {})
13
+ region_code = options[:default_country]
14
+ self.new(js_call('Fakie.parse', phone_number, region_code))
15
+ end
16
+ end
17
+
18
+ attr_reader :e164
19
+ attr_reader :country_code
20
+ attr_reader :national_number
21
+ attr_reader :raw_input
22
+ attr_reader :country_code_source
23
+ attr_reader :preferred_domestic_carrier_code
24
+ attr_reader :region_code
25
+ attr_reader :type
26
+
27
+ # @param hash [Hash] hash of phone number data
28
+ def initialize(hash)
29
+ @e164 = hash['e164']
30
+ @country_code = hash['country_code']
31
+ @national_number = hash['national_number']
32
+ @raw_input = hash['raw_input'].to_s
33
+ @country_code_source = hash['country_code_source']
34
+ @preferred_domestic_carrier_code = hash['preferred_domestic_carrier_code']
35
+ @is_possible = hash['is_possible']
36
+ @is_valid = hash['is_valid']
37
+ @region_code = hash['region']
38
+ @type = hash['type']
39
+ end
40
+
41
+ def is_possible?
42
+ @is_possible
43
+ end
44
+
45
+ def is_valid?
46
+ @is_valid
47
+ end
48
+
49
+ def international_format(region = self.region_code)
50
+ raise InvalidPhoneNumber unless self.is_valid?
51
+ @international_format ||= js_call('Fakie.formatInternational', region, self.e164)
52
+ end
53
+
54
+ def local_format(region = self.region_code)
55
+ raise InvalidPhoneNumber unless self.is_valid?
56
+ @local_format ||= js_call('Fakie.formatLocal', region, self.e164)
57
+ end
58
+
59
+ def country_name
60
+ return nil unless self.region_code
61
+ Fakie.country_name_for_region_code(self.region_code)
62
+ end
63
+ end
64
+ end
65
+
@@ -0,0 +1,3 @@
1
+ module Fakie
2
+ VERSION = '0.1.2'
3
+ end
data/lib/fakie.rb ADDED
@@ -0,0 +1,262 @@
1
+ require 'fakie/version'
2
+ require 'fakie/errors'
3
+ require 'fakie/java_script'
4
+ require 'fakie/phone_number'
5
+
6
+ module Fakie
7
+ module_function
8
+
9
+ # Parse a phone number
10
+ # @param phone_number [String] phone number to parse
11
+ # @option options default_country [String] ISO 3166-1 two-letter country code
12
+ # @return [PhoneNumber] phone number object
13
+ def parse(phone_number, options = {})
14
+ PhoneNumber.parse(phone_number, options)
15
+ end
16
+
17
+ def country_name_for_region_code(region_code)
18
+ @@_country_name_map ||= {
19
+ 'AF' => "Afghanistan",
20
+ 'AL' => "Albania",
21
+ 'DZ' => "Algeria",
22
+ 'AS' => "American Samoa",
23
+ 'AD' => "Andorra",
24
+ 'AO' => "Angola",
25
+ 'AI' => "Anguilla",
26
+ 'AQ' => "Antarctica",
27
+ 'AG' => "Antigua And Barbuda",
28
+ 'AR' => "Argentina",
29
+ 'AM' => "Armenia",
30
+ 'AW' => "Aruba",
31
+ 'AU' => "Australia",
32
+ 'AT' => "Austria",
33
+ 'AZ' => "Azerbaijan",
34
+ 'BS' => "Bahamas",
35
+ 'BH' => "Bahrain",
36
+ 'BD' => "Bangladesh",
37
+ 'BB' => "Barbados",
38
+ 'BY' => "Belarus",
39
+ 'BE' => "Belgium",
40
+ 'BZ' => "Belize",
41
+ 'BJ' => "Benin",
42
+ 'BM' => "Bermuda",
43
+ 'BT' => "Bhutan",
44
+ 'BO' => "Bolivia",
45
+ 'BA' => "Bosnia And Herzegovina",
46
+ 'BW' => "Botswana",
47
+ 'BV' => "Bouvet Island",
48
+ 'BR' => "Brazil",
49
+ 'IO' => "British Indian Ocean Territory",
50
+ 'BN' => "Brunei",
51
+ 'BG' => "Bulgaria",
52
+ 'BF' => "Burkina Faso",
53
+ 'BI' => "Burundi",
54
+ 'KH' => "Cambodia",
55
+ 'CM' => "Cameroon",
56
+ 'CA' => "Canada",
57
+ 'CV' => "Cape Verde",
58
+ 'KY' => "Cayman Islands",
59
+ 'CF' => "Central African Republic",
60
+ 'TD' => "Chad",
61
+ 'CL' => "Chile",
62
+ 'CN' => "China",
63
+ 'CX' => "Christmas Island",
64
+ 'CC' => "Cocos (Keeling) Islands",
65
+ 'CO' => "Columbia",
66
+ 'KM' => "Comoros",
67
+ 'CG' => "Congo",
68
+ 'CK' => "Cook Islands",
69
+ 'CR' => "Costa Rica",
70
+ 'CI' => "Cote D'Ivorie (Ivory Coast)",
71
+ 'HR' => "Croatia (Hrvatska)",
72
+ 'CU' => "Cuba",
73
+ 'CY' => "Cyprus",
74
+ 'CZ' => "Czech Republic",
75
+ 'CD' => "Democratic Republic Of Congo (Zaire)",
76
+ 'DK' => "Denmark",
77
+ 'DJ' => "Djibouti",
78
+ 'DM' => "Dominica",
79
+ 'DO' => "Dominican Republic",
80
+ 'TP' => "East Timor",
81
+ 'EC' => "Ecuador",
82
+ 'EG' => "Egypt",
83
+ 'SV' => "El Salvador",
84
+ 'GQ' => "Equatorial Guinea",
85
+ 'ER' => "Eritrea",
86
+ 'EE' => "Estonia",
87
+ 'ET' => "Ethiopia",
88
+ 'FK' => "Falkland Islands (Malvinas)",
89
+ 'FO' => "Faroe Islands",
90
+ 'FJ' => "Fiji",
91
+ 'FI' => "Finland",
92
+ 'FR' => "France",
93
+ 'FX' => "France, Metropolitan",
94
+ 'GF' => "French Guinea",
95
+ 'PF' => "French Polynesia",
96
+ 'TF' => "French Southern Territories",
97
+ 'GA' => "Gabon",
98
+ 'GM' => "Gambia",
99
+ 'GE' => "Georgia",
100
+ 'DE' => "Germany",
101
+ 'GH' => "Ghana",
102
+ 'GI' => "Gibraltar",
103
+ 'GR' => "Greece",
104
+ 'GL' => "Greenland",
105
+ 'GD' => "Grenada",
106
+ 'GP' => "Guadeloupe",
107
+ 'GU' => "Guam",
108
+ 'GT' => "Guatemala",
109
+ 'GN' => "Guinea",
110
+ 'GW' => "Guinea-Bissau",
111
+ 'GY' => "Guyana",
112
+ 'HT' => "Haiti",
113
+ 'HM' => "Heard And McDonald Islands",
114
+ 'HN' => "Honduras",
115
+ 'HK' => "Hong Kong",
116
+ 'HU' => "Hungary",
117
+ 'IS' => "Iceland",
118
+ 'IN' => "India",
119
+ 'ID' => "Indonesia",
120
+ 'IR' => "Iran",
121
+ 'IQ' => "Iraq",
122
+ 'IE' => "Ireland",
123
+ 'IL' => "Israel",
124
+ 'IT' => "Italy",
125
+ 'JM' => "Jamaica",
126
+ 'JP' => "Japan",
127
+ 'JO' => "Jordan",
128
+ 'KZ' => "Kazakhstan",
129
+ 'KE' => "Kenya",
130
+ 'KI' => "Kiribati",
131
+ 'KW' => "Kuwait",
132
+ 'KG' => "Kyrgyzstan",
133
+ 'LA' => "Laos",
134
+ 'LV' => "Latvia",
135
+ 'LB' => "Lebanon",
136
+ 'LS' => "Lesotho",
137
+ 'LR' => "Liberia",
138
+ 'LY' => "Libya",
139
+ 'LI' => "Liechtenstein",
140
+ 'LT' => "Lithuania",
141
+ 'LU' => "Luxembourg",
142
+ 'MO' => "Macau",
143
+ 'MK' => "Macedonia",
144
+ 'MG' => "Madagascar",
145
+ 'MW' => "Malawi",
146
+ 'MY' => "Malaysia",
147
+ 'MV' => "Maldives",
148
+ 'ML' => "Mali",
149
+ 'MT' => "Malta",
150
+ 'MH' => "Marshall Islands",
151
+ 'MQ' => "Martinique",
152
+ 'MR' => "Mauritania",
153
+ 'MU' => "Mauritius",
154
+ 'YT' => "Mayotte",
155
+ 'MX' => "Mexico",
156
+ 'FM' => "Micronesia",
157
+ 'MD' => "Moldova",
158
+ 'MC' => "Monaco",
159
+ 'MN' => "Mongolia",
160
+ 'MS' => "Montserrat",
161
+ 'MA' => "Morocco",
162
+ 'MZ' => "Mozambique",
163
+ 'MM' => "Myanmar (Burma)",
164
+ 'NA' => "Namibia",
165
+ 'NR' => "Nauru",
166
+ 'NP' => "Nepal",
167
+ 'NL' => "Netherlands",
168
+ 'AN' => "Netherlands Antilles",
169
+ 'NC' => "New Caledonia",
170
+ 'NZ' => "New Zealand",
171
+ 'NI' => "Nicaragua",
172
+ 'NE' => "Niger",
173
+ 'NG' => "Nigeria",
174
+ 'NU' => "Niue",
175
+ 'NF' => "Norfolk Island",
176
+ 'KP' => "North Korea",
177
+ 'MP' => "Northern Mariana Islands",
178
+ 'NO' => "Norway",
179
+ 'OM' => "Oman",
180
+ 'PK' => "Pakistan",
181
+ 'PW' => "Palau",
182
+ 'PA' => "Panama",
183
+ 'PG' => "Papua New Guinea",
184
+ 'PY' => "Paraguay",
185
+ 'PE' => "Peru",
186
+ 'PH' => "Philippines",
187
+ 'PN' => "Pitcairn",
188
+ 'PL' => "Poland",
189
+ 'PT' => "Portugal",
190
+ 'PR' => "Puerto Rico",
191
+ 'QA' => "Qatar",
192
+ 'RE' => "Reunion",
193
+ 'RO' => "Romania",
194
+ 'RU' => "Russia",
195
+ 'RW' => "Rwanda",
196
+ 'SH' => "Saint Helena",
197
+ 'KN' => "Saint Kitts And Nevis",
198
+ 'LC' => "Saint Lucia",
199
+ 'PM' => "Saint Pierre And Miquelon",
200
+ 'VC' => "Saint Vincent And The Grenadines",
201
+ 'SM' => "San Marino",
202
+ 'ST' => "Sao Tome And Principe",
203
+ 'SA' => "Saudi Arabia",
204
+ 'SN' => "Senegal",
205
+ 'SC' => "Seychelles",
206
+ 'SL' => "Sierra Leone",
207
+ 'SG' => "Singapore",
208
+ 'SK' => "Slovak Republic",
209
+ 'SI' => "Slovenia",
210
+ 'SB' => "Solomon Islands",
211
+ 'SO' => "Somalia",
212
+ 'ZA' => "South Africa",
213
+ 'GS' => "South Georgia And South Sandwich Islands",
214
+ 'KR' => "South Korea",
215
+ 'ES' => "Spain",
216
+ 'LK' => "Sri Lanka",
217
+ 'SD' => "Sudan",
218
+ 'SR' => "Suriname",
219
+ 'SJ' => "Svalbard And Jan Mayen",
220
+ 'SZ' => "Swaziland",
221
+ 'SE' => "Sweden",
222
+ 'CH' => "Switzerland",
223
+ 'SY' => "Syria",
224
+ 'TW' => "Taiwan",
225
+ 'TJ' => "Tajikistan",
226
+ 'TZ' => "Tanzania",
227
+ 'TH' => "Thailand",
228
+ 'TG' => "Togo",
229
+ 'TK' => "Tokelau",
230
+ 'TO' => "Tonga",
231
+ 'TT' => "Trinidad And Tobago",
232
+ 'TN' => "Tunisia",
233
+ 'TR' => "Turkey",
234
+ 'TM' => "Turkmenistan",
235
+ 'TC' => "Turks And Caicos Islands",
236
+ 'TV' => "Tuvalu",
237
+ 'UG' => "Uganda",
238
+ 'UA' => "Ukraine",
239
+ 'AE' => "United Arab Emirates",
240
+ 'GB' => "United Kingdom",
241
+ 'US' => "United States",
242
+ 'UM' => "United States Minor Outlying Islands",
243
+ 'UY' => "Uruguay",
244
+ 'UZ' => "Uzbekistan",
245
+ 'VU' => "Vanuatu",
246
+ 'VA' => "Vatican City (Holy See)",
247
+ 'VE' => "Venezuela",
248
+ 'VN' => "Vietnam",
249
+ 'VG' => "Virgin Islands (British)",
250
+ 'VI' => "Virgin Islands (US)",
251
+ 'WF' => "Wallis And Futuna Islands",
252
+ 'EH' => "Western Sahara",
253
+ 'WS' => "Western Samoa",
254
+ 'YE' => "Yemen",
255
+ 'YU' => "Yugoslavia",
256
+ 'ZM' => "Zambia",
257
+ 'ZW' => "Zimbabwe"
258
+ }
259
+
260
+ @@_country_name_map[region_code.upcase]
261
+ end
262
+ end
data/tasks/import.rake ADDED
@@ -0,0 +1,47 @@
1
+ SRCPATH = 'tmp/libphonenumber/javascript/i18n/phonenumbers'
2
+ CLOSUREPATH = 'tmp/closure-library/closure/goog'
3
+
4
+ desc 'Import and build the latest version of libphonenumber'
5
+ task :import do
6
+ unless File.exists?(SRCPATH)
7
+ puts 'Downloading libphonenumber...'
8
+ `svn export http://libphonenumber.googlecode.com/svn/trunk tmp/libphonenumber`
9
+ end
10
+
11
+ unless File.exists?(CLOSUREPATH)
12
+ puts 'Downloading closure-library...'
13
+ `svn export http://closure-library.googlecode.com/svn/trunk tmp/closure-library`
14
+ end
15
+
16
+ unless `which closure-compiler`.chomp.length > 0
17
+ puts 'Please `brew install closure-compiler` and then run this again.'
18
+ exit
19
+ end
20
+
21
+ puts 'Compiling...'
22
+ system "closure-compiler --jscomp_warning=deprecated \
23
+ --jscomp_warning=missingProperties \
24
+ --js #{CLOSUREPATH}/base.js \
25
+ --js #{CLOSUREPATH}/array/array.js \
26
+ --js #{CLOSUREPATH}/asserts/asserts.js \
27
+ --js #{CLOSUREPATH}/debug/error.js \
28
+ --js #{CLOSUREPATH}/object/object.js \
29
+ --js #{CLOSUREPATH}/proto2/descriptor.js \
30
+ --js #{CLOSUREPATH}/proto2/fielddescriptor.js \
31
+ --js #{CLOSUREPATH}/proto2/lazydeserializer.js \
32
+ --js #{CLOSUREPATH}/proto2/message.js \
33
+ --js #{CLOSUREPATH}/proto2/pbliteserializer.js \
34
+ --js #{CLOSUREPATH}/proto2/objectserializer.js \
35
+ --js #{CLOSUREPATH}/proto2/serializer.js \
36
+ --js #{CLOSUREPATH}/proto2/util.js \
37
+ --js #{CLOSUREPATH}/string/string.js \
38
+ --js #{CLOSUREPATH}/string/stringbuffer.js \
39
+ --js #{SRCPATH}/asyoutypeformatter.js \
40
+ --js #{SRCPATH}/metadata.js \
41
+ --js #{SRCPATH}/phonemetadata.pb.js \
42
+ --js #{SRCPATH}/phonenumber.pb.js \
43
+ --js #{SRCPATH}/phonenumberutil.js \
44
+ --js_output_file=lib/fakie/js/libphonenumber.js"
45
+
46
+ puts 'Done!'
47
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ module Fakie
4
+ class PhoneNumberTest < TestCase
5
+ def test_parse
6
+ phone_number = Fakie.parse('+1 415 555 0123')
7
+ assert_equal 1, phone_number.country_code
8
+ assert_equal 4155550123, phone_number.national_number
9
+ assert_equal '+1 415 555 0123', phone_number.raw_input
10
+ assert_equal '+14155550123', phone_number.e164
11
+ assert_equal 'US', phone_number.region_code
12
+ assert phone_number.is_possible?
13
+ assert phone_number.is_valid?
14
+ end
15
+
16
+ def test_parse_with_default_country
17
+ phone_number = Fakie.parse('(415) 555-0123', default_country: 'US')
18
+ assert_equal 1, phone_number.country_code
19
+ assert_equal 4155550123, phone_number.national_number
20
+ assert_equal '(415) 555-0123', phone_number.raw_input
21
+ assert_equal 'US', phone_number.region_code
22
+ assert phone_number.is_possible?
23
+ assert phone_number.is_valid?
24
+ end
25
+
26
+ def test_local_format
27
+ phone_number = Fakie.parse('+1 415-555-0123')
28
+ assert_equal '(415) 555-0123', phone_number.local_format
29
+ end
30
+
31
+ def test_international_format
32
+ phone_number = Fakie.parse('+14155550123')
33
+ assert_equal '+1 415-555-0123', phone_number.international_format
34
+
35
+ phone_number = Fakie.parse('415-555-0123', default_country: 'US')
36
+ assert_equal '+1 415-555-0123', phone_number.international_format
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ module Fakie
4
+ class FakieTest < TestCase
5
+ def test_parse
6
+ assert_kind_of PhoneNumber, Fakie.parse('+14155550123')
7
+ end
8
+ end
9
+ end
data/test/js.html ADDED
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Fakie</title>
6
+ </head>
7
+ <body>
8
+ <p>libphonenumber.js and fakie.js are loaded into this page. Open the console to play around.</p>
9
+
10
+ <script src="../lib/fakie/js/libphonenumber.js"></script>
11
+ <script src="../lib/fakie/js/fakie.js"></script>
12
+ </body>
13
+ </html>
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require :test
4
+
5
+ require 'simplecov'
6
+ SimpleCov.start
7
+
8
+ require 'minitest/autorun'
9
+ require 'fakie'
10
+
11
+ # Support files
12
+ Dir["#{File.expand_path(File.dirname(__FILE__))}/support/*.rb"].each do |file|
13
+ require file
14
+ end
15
+
16
+ class Fakie::TestCase < MiniTest::Unit::TestCase
17
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fakie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Sam Soffes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: execjs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: libphonenumber wrapper
28
+ email:
29
+ - sam@soff.es
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - .travis.yml
36
+ - Contributing.markdown
37
+ - Gemfile
38
+ - LICENSE
39
+ - Rakefile
40
+ - Readme.markdown
41
+ - fakie.gemspec
42
+ - lib/fakie.rb
43
+ - lib/fakie/errors.rb
44
+ - lib/fakie/java_script.rb
45
+ - lib/fakie/js/fakie.js
46
+ - lib/fakie/js/libphonenumber.js
47
+ - lib/fakie/phone_number.rb
48
+ - lib/fakie/version.rb
49
+ - tasks/import.rake
50
+ - test/fakie/phone_number_test.rb
51
+ - test/fakie_test.rb
52
+ - test/js.html
53
+ - test/test_helper.rb
54
+ homepage: https://github.com/seesawco/fakie
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: 1.8.7
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.0.0
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: libphonenumber wrapper with ExecJS
78
+ test_files:
79
+ - test/fakie/phone_number_test.rb
80
+ - test/fakie_test.rb
81
+ - test/js.html
82
+ - test/test_helper.rb
83
+ has_rdoc: