Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 063667b4d2878711f9e2add8e27942edb17530e3 @joenoon joenoon committed Nov 14, 2011
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,7 @@
+source "http://rubygems.org"
+
+gemspec
+gem 'rake'
+gem 'execjs'
+gem 'coffee-script'
+gem 'minitest'
@@ -0,0 +1,22 @@
+Copyright (c) 2011 Joe Noon <joenoon@gmail.com>
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
+# libphonenumber-execjs
+
+ExecJS wrapper for Google's libphonenumber library
+
+## Installation
+
+Gemfile:
+
+ gem "libphonenumber-execjs"
+
+## Getting Started
+
+ libphonenumber = Libphonenumber.new
+
+Currently, only 3 functions are exposed directly:
+
+ libphonenumber.extractPossibleNumber("19255550100")
+ libphonenumber.normalize("19255550100")
+ libphonenumber.parse("9255550100", "US", 925)
+
+However, the entire js context of the library is available via the `context` accessor:
+
+ libphonenumber.context.call "i18n.phonenumbers.PhoneNumberUtil.extractPossibleNumber", "19255550100"
+
+And one additional function that I've put together to tie some of these things together in a useful way,
+with the goal of trying to get a valid E164 formatted number back or not in one call.
+
+You can pass:
+
+1. Only a string - in this case, it must be complete (e.g. e164 or something you've previously received back).
+2. A string and a region code (will accept numbers ("1") or ISO codes ("us", "US"). Will first try like (1), and if not, see if it can be parsed with the fallback region code.
+3. A string, a Region Code, and a National Destination Code. Will first try like (1) and (2), and if not, see if it can be parsed by prefixing the national part with the given ndc.
+
+Example (see tests for more examples):
+
+ libphonenumber.simple.get_e164_phone_number("5550100", "US", "925") # => "+19255550100"
+ libphonenumber.simple.get_e164_phone_number("+19255550100") # => "+19255550100"
+
+## Development/Recompiling sources
+
+There is a rake task `build_js` in the Rakefile. You will need the source for google closure, and libphonenumber checked out locally.
+
+See the Rakefile for details.
+
+## Information about libphonenumber
+
+Google's library for dealing with phone numbers
+
+[http://code.google.com/p/libphonenumber/](http://code.google.com/p/libphonenumber/)
+
+## Information about ExecJS
+
+[https://github.com/sstephenson/execjs](https://github.com/sstephenson/execjs)
@@ -0,0 +1,60 @@
+require 'bundler'
+Bundler.require(:default, :development)
+require "bundler/gem_tasks"
+
+desc "Build the javascript output for libphonenumber. Needs env variables CLOSURE_PATH and LIBPHONENUMBER_PATH."
+task :build_js do
+ closure = ENV["CLOSURE_PATH"]
+ libphonenumber = ENV["LIBPHONENUMBER_PATH"]
+ if !closure
+ raise "set CLOSURE_PATH"
+ end
+ closure = File.join(closure, "")
+ if !libphonenumber
+ raise "set LIBPHONENUMBER_PATH"
+ end
+ libphonenumber = File.join(libphonenumber, "")
+ cmd = %Q{
+ "#{closure}closure/bin/build/closurebuilder.py" \
+ --root="#{closure}" \
+ --input="#{libphonenumber}javascript/i18n/phonenumbers/phonemetadata.pb.js" \
+ --input="#{libphonenumber}javascript/i18n/phonenumbers/phonenumber.pb.js" \
+ --input="#{libphonenumber}javascript/i18n/phonenumbers/metadata.js" \
+ --input="#{libphonenumber}javascript/i18n/phonenumbers/phonenumberutil.js" \
+ --input="#{libphonenumber}javascript/i18n/phonenumbers/asyoutypeformatter.js" \
+ --namespace="i18n.phonenumbers.AsYouTypeFormatter" \
+ --namespace="i18n.phonenumbers.metadata" \
+ --namespace="i18n.phonenumbers.NumberFormat" \
+ --namespace="i18n.phonenumbers.PhoneNumberDesc" \
+ --namespace="i18n.phonenumbers.PhoneMetadata" \
+ --namespace="i18n.phonenumbers.PhoneMetadataCollection" \
+ --namespace="i18n.phonenumbers.PhoneNumber" \
+ --namespace="i18n.phonenumbers.PhoneNumber.CountryCodeSource" \
+ --namespace="i18n.phonenumbers.Error" \
+ --namespace="i18n.phonenumbers.PhoneNumberFormat" \
+ --namespace="i18n.phonenumbers.PhoneNumberType" \
+ --namespace="i18n.phonenumbers.PhoneNumberUtil" \
+ --output_mode=script \
+ "#{libphonenumber}javascript/i18n/phonenumbers/phonemetadata.pb.js" \
+ "#{libphonenumber}javascript/i18n/phonenumbers/phonenumber.pb.js" \
+ "#{libphonenumber}javascript/i18n/phonenumbers/metadata.js" \
+ "#{libphonenumber}javascript/i18n/phonenumbers/phonenumberutil.js" \
+ "#{libphonenumber}javascript/i18n/phonenumbers/asyoutypeformatter.js"
+ }
+ out = `#{cmd.strip}`
+ simple = CoffeeScript.compile(File.read(File.join(File.dirname(__FILE__), "support", "simple.coffee")))
+ out = %Q{
+(function() {
+this.goog = goog = this.goog || {};
+this.i18n = i18n = this.i18n || {};
+var CLOSURE_NO_DEPS = true;
+#{out}
+#{simple}
+}).call(this);
+}
+ outfile = File.join(File.dirname(__FILE__), "support", "libphonenumber.js")
+ File.open(outfile, "w") do |f|
+ f.write out
+ end
+ puts "Saved to #{outfile}"
+end
@@ -0,0 +1,2 @@
+require "libphonenumber-execjs/libphonenumber"
+require "libphonenumber-execjs/version"
@@ -0,0 +1,38 @@
+class Libphonenumber
+
+ attr_reader :context
+
+ def initialize
+ @context = ExecJS.compile(File.read(File.join(File.dirname(__FILE__), "..", "..", "support", "libphonenumber.js")))
+ end
+
+ def extractPossibleNumber(str="")
+ context.call "i18n.phonenumbers.PhoneNumberUtil.extractPossibleNumber", str
+ end
+
+ def normalize(str="")
+ context.call "i18n.phonenumbers.PhoneNumberUtil.normalize", str
+ end
+
+ def parse(str="", default_region="ZZ")
+ context.call "i18n.phonenumbers.PhoneNumberUtil.getInstance().parse", str, default_region
+ end
+
+ def simple
+ @simple ||= Simple.new(self)
+ end
+
+ class Simple
+
+ def initialize(libphonenumber)
+ @libphonenumber = libphonenumber
+ @context = @libphonenumber.context
+ end
+
+ def get_e164_phone_number(str, cc=nil, ndc=nil)
+ @context.call "getE164PhoneNumber", str, cc, ndc
+ end
+
+ end
+
+end
@@ -0,0 +1,3 @@
+class Libphonenumber
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "libphonenumber-execjs/version"
+
+Gem::Specification.new do |s|
+ s.name = "libphonenumber-execjs"
+ s.version = Libphonenumber::VERSION
+ s.authors = ["Joe Noon"]
+ s.email = ["joenoon@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{ExecJS wrapper for Google's libphonenumber library}
+ s.description = %q{ExecJS wrapper for Google's libphonenumber library}
+
+ s.rubyforge_project = "libphonenumber-execjs"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ # specify any dependencies here; for example:
+ # s.add_development_dependency "rspec"
+ s.add_runtime_dependency "execjs"
+end
Oops, something went wrong.

0 comments on commit 063667b

Please sign in to comment.