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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bddcb20076c5823e1b26bc9bb9ef5ca10df05889
4
+ data.tar.gz: 660b1e6cafbf3be204bb4854924706cc4a1ce61e
5
+ SHA512:
6
+ metadata.gz: b1a720adf32ab75f7a1808c56a9c6c4da4345864d9f6d03d7d2cea7d7518f21df05ea11ffd1d6d96d09abd1a5dce177a9c20010c0a539029f4e37032c075b10b
7
+ data.tar.gz: c035c6cf7a5021c0496d23f8487ae8d654f687a2de81551ce8bb32e2d64a5646f91f9ab0fd0de00e41045854148f2f915b7c4c5373ecdcf616aead3c6e63c47c
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ bundler_args: --without development
3
+ rvm:
4
+ - 1.8.7
5
+ - 1.9.2
6
+ - 1.9.3
7
+ - 2.0.0
8
+ - jruby-19mode
9
+ - rbx-19mode
@@ -0,0 +1,19 @@
1
+ ## Submitting a Pull Request
2
+
3
+ 1. [Fork the repository.][fork]
4
+ 2. [Create a topic branch.][branch]
5
+ 3. Add tests for your unimplemented feature or bug fix.
6
+ 4. Run `bundle exec rake`. If your tests pass, return to step 3.
7
+ 5. Implement your feature or bug fix.
8
+ 6. Run `bundle exec rake`. If your tests fail, return to step 5.
9
+ 7. Run `open coverage/index.html`. If your changes are not completely covered
10
+ by your tests, return to step 3.
11
+ 8. Add documentation for your feature or bug fix.
12
+ 9. Run `bundle exec rake doc`. If your changes are not 100% documented, go
13
+ back to step 8.
14
+ 10. Add, commit, and push your changes.
15
+ 11. [Submit a pull request.][pr]
16
+
17
+ [fork]: http://help.github.com/fork-a-repo/
18
+ [branch]: http://learn.github.com/p/branching.html
19
+ [pr]: http://help.github.com/send-pull-requests/
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake', :group => [:development, :test]
6
+
7
+ # Development dependencies
8
+ group :development do
9
+ gem 'yard'
10
+ gem 'redcarpet', :platform => 'ruby'
11
+ end
12
+
13
+ # Testing dependencies
14
+ group :test do
15
+ gem 'minitest'
16
+ gem 'minitest-wscolor' if RUBY_VERSION >= '1.9.3'
17
+ gem 'simplecov', :require => false
18
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Seesaw Decisions Corporation
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ task :default => :test
10
+
11
+ begin
12
+ require 'yard'
13
+ YARD::Rake::YardocTask.new(:doc) do |task|
14
+ task.files = ['Readme.markdown', 'LICENSE', 'lib/**/*.rb']
15
+ task.options = [
16
+ '--output-dir', 'doc',
17
+ '--markup', 'markdown',
18
+ ]
19
+ end
20
+ rescue LoadError
21
+ end
22
+
23
+ Dir.glob('tasks/*.rake').each { |task| import task }
data/Readme.markdown ADDED
@@ -0,0 +1,48 @@
1
+ # Fakie
2
+
3
+ Ruby [libphonenumber](http://code.google.com/p/libphonenumber) wrapper with [ExecJS](https://github.com/sstephenson/execjs).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ``` ruby
10
+ gem 'fakie'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fakie
20
+
21
+ ## Usage
22
+
23
+ ``` ruby
24
+ > phone = Fakie.parse('415-555-0123', default_country: 'US')
25
+ #=> <Fakie::PhoneNumber @country_code=1, @national_number=4155550123, @raw_input="415-555-0123", @country_code_source=20, @preferred_domestic_carrier_code="", @is_possible=true, @is_valid=true, @region_code="US", @type="FIXED_LINE_OR_MOBILE">
26
+ > phone.e164
27
+ => "+14155550123"
28
+ > phone.country_code
29
+ > 1
30
+ > phone.local_format
31
+ => "(415) 555-0123"
32
+ > phone.internation_format
33
+ => "+1 415-555-0123"
34
+ > phone.country_name
35
+ => "United States"
36
+ ```
37
+
38
+ ## Supported Ruby Versions
39
+
40
+ Fakie is tested under 1.8.7, 1.9.2, 1.9.3, 2.0.0, JRuby 1.7.2 (1.9 mode), and Rubinius 2.0.0 (1.9 mode).
41
+
42
+ [![Build Status](https://travis-ci.org/seesawco/fakie.png?branch=master)](https://travis-ci.org/seesawco/fakie)
43
+
44
+ ## Contributing
45
+
46
+ See the [contributing guide](Contributing.markdown).
47
+
48
+ To update libphonenumber, simply run `rake import`. It will download and build the neccessary files. You'll need closure-compiler installed to do it.
data/fakie.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fakie/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'fakie'
8
+ gem.version = Fakie::VERSION
9
+ gem.authors = ['Sam Soffes']
10
+ gem.email = ['sam@soff.es']
11
+ gem.description = 'libphonenumber wrapper'
12
+ gem.summary = 'libphonenumber wrapper with ExecJS'
13
+ gem.homepage = 'https://github.com/seesawco/fakie'
14
+ gem.license = 'MIT'
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ['lib']
20
+
21
+ gem.required_ruby_version = '>= 1.8.7'
22
+ gem.add_dependency 'execjs'
23
+ end
@@ -0,0 +1,4 @@
1
+ module Fakie
2
+ class Error < Exception; end
3
+ class InvalidPhoneNumber < Error; end
4
+ end
@@ -0,0 +1,30 @@
1
+ module Fakie
2
+ module JavaScript
3
+ module_function
4
+
5
+ # ExecJS Context
6
+ # @return [ExecJS::ExternalRuntime::Context] context for executing JavaScript against libphonenumber and Fakie
7
+ def js_context
8
+ @@_js_context ||= begin
9
+ require 'execjs'
10
+ js_dir = File.join(File.expand_path(File.dirname(__FILE__)), 'js')
11
+ source = File.open(File.join(js_dir, 'libphonenumber.js')).read
12
+ source += File.open(File.join(js_dir, 'fakie.js')).read
13
+ ExecJS.compile(source)
14
+ end
15
+ end
16
+
17
+ # Call a function against the context
18
+ # @param function [String] function name
19
+ # @param args [* String] list of arguments to send to the function
20
+ def js_call(function, *args)
21
+ js_context.call(function, *args)
22
+ end
23
+
24
+ # Call a function against the context
25
+ # @param function [String] JavaScript code to evaluate
26
+ def js_eval(script)
27
+ js_context.eval(script)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,138 @@
1
+ var Fakie = {
2
+ parse: function(phoneNumber, regionCode) {
3
+ var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
4
+ var number = phoneUtil.parseAndKeepRawInput(phoneNumber, regionCode);
5
+ var result = new goog.proto2.ObjectSerializer(goog.proto2.ObjectSerializer.KeyOption.NAME).serialize(number)
6
+
7
+ var isPossible = phoneUtil.isPossibleNumber(number);
8
+ result['is_possible'] = isPossible;
9
+
10
+ if (!isPossible) {
11
+ var PNV = i18n.phonenumbers.PhoneNumberUtil.ValidationResult;
12
+ switch (phoneUtil.isPossibleNumberWithReason(number)) {
13
+ case PNV.INVALID_COUNTRY_CODE:
14
+ result['invalid_reason'] = 'INVALID_COUNTRY_CODE'
15
+ break;
16
+ case PNV.TOO_SHORT:
17
+ result['invalid_reason'] = 'TOO_SHORT';
18
+ break;
19
+ case PNV.TOO_LONG:
20
+ result['invalid_reason'] = 'TOO_LONG';
21
+ break;
22
+ }
23
+ } else {
24
+ var isNumberValid = phoneUtil.isValidNumber(number);
25
+ result['is_valid'] = isNumberValid;
26
+
27
+ if (isNumberValid && regionCode && regionCode != 'ZZ') {
28
+ result['is_valid_for_region'] = phoneUtil.isValidNumberForRegion(number, regionCode);
29
+ }
30
+
31
+ result['region'] = phoneUtil.getRegionCodeForNumber(number);
32
+ var PNT = i18n.phonenumbers.PhoneNumberType;
33
+ switch (phoneUtil.getNumberType(number)) {
34
+ case PNT.FIXED_LINE:
35
+ result['type'] = 'FIXED_LINE';
36
+ break;
37
+ case PNT.MOBILE:
38
+ result['type'] = 'MOBILE';
39
+ break;
40
+ case PNT.FIXED_LINE_OR_MOBILE:
41
+ result['type'] = 'FIXED_LINE_OR_MOBILE';
42
+ break;
43
+ case PNT.TOLL_FREE:
44
+ result['type'] = 'TOLL_FREE';
45
+ break;
46
+ case PNT.PREMIUM_RATE:
47
+ result['type'] = 'PREMIUM_RATE';
48
+ break;
49
+ case PNT.SHARED_COST:
50
+ result['type'] = 'SHARED_COST';
51
+ break;
52
+ case PNT.VOIP:
53
+ result['type'] = 'VOIP';
54
+ break;
55
+ case PNT.PERSONAL_NUMBER:
56
+ result['type'] = 'PERSONAL_NUMBER';
57
+ break;
58
+ case PNT.PAGER:
59
+ result['type'] = 'PAGER';
60
+ break;
61
+ case PNT.UAN:
62
+ result['type'] = 'UAN';
63
+ break;
64
+ case PNT.UNKNOWN:
65
+ result['type'] = 'UNKNOWN';
66
+ break;
67
+ }
68
+ }
69
+
70
+ result['e164'] = phoneUtil.format(number, i18n.phonenumbers.PhoneNumberFormat.E164);
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 "";
83
+ }
84
+ },
85
+
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
+ }
96
+ },
97
+
98
+ formatInternational: function(country, phone) {
99
+ try {
100
+ var phone = Fakie.cleanPhone(phone);
101
+ var formatter = new i18n.phonenumbers.AsYouTypeFormatter(country);
102
+ var output = new goog.string.StringBuffer();
103
+ for (var i = 0; i < phone.length; ++i) {
104
+ var inputChar = phone.charAt(i);
105
+ output = (formatter.inputDigit(inputChar));
106
+ }
107
+ return output.toString();
108
+ } catch (e) {
109
+ return phone;
110
+ }
111
+ },
112
+
113
+ formatLocal: function(country, phone) {
114
+ try {
115
+ var phone = Fakie.cleanPhone(phone);
116
+ var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
117
+ var number = phoneUtil.parseAndKeepRawInput(phone, country);
118
+ if (phoneUtil.isValidNumberForRegion(number, country)) {
119
+ var PNF = i18n.phonenumbers.PhoneNumberFormat;
120
+ return phoneUtil.format(number, PNF.NATIONAL);
121
+ } else {
122
+ return Fakie.formatInternational(country, phone);
123
+ }
124
+ } catch (e) {
125
+ return Fakie.formatInternational(country, phone);
126
+ }
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
+ }
138
+ };