Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library Updated & Carrier+Region Added #4

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ ExecJS wrapper for Google's libphonenumber library

Gemfile:

gem "libphonenumber-execjs"
gem 'libphonenumber-execjs', :github => 'hrishabhz/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)

libphonenumber.parse("7893461649", "IN") # => {"util"=>{"values_"=>{1=>91, 2=>7893461642.0}, "fields_"=>{1=>{"tag_"=>"1", "name_"=>"country_code", "isRepeated_"=>false, "isRequired_"=>true, "fieldType_"=>5, "deserializationConversionPermitted_"=>false, "defaultValue_"=>nil}, 2=>{"tag_"=>"2", "name_"=>"national_number", "isRepeated_"=>false, "isRequired_"=>true, "fieldType_"=>4, "deserializationConversionPermitted_"=>true, "defaultValue_"=>nil}, 3=>{"tag_"=>"3", "name_"=>"extension", "isRepeated_"=>false, "isRequired_"=>false, "fieldType_"=>9, "deserializationConversionPermitted_"=>false, "defaultValue_"=>nil}, 4=>{"tag_"=>"4", "name_"=>"italian_leading_zero", "isRepeated_"=>false, "isRequired_"=>false, "fieldType_"=>8, "deserializationConversionPermitted_"=>false, "defaultValue_"=>nil}, 5=>{"tag_"=>"5", "name_"=>"raw_input", "isRepeated_"=>false, "isRequired_"=>false, "fieldType_"=>9, "deserializationConversionPermitted_"=>false, "defaultValue_"=>nil}, 6=>{"tag_"=>"6", "name_"=>"country_code_source", "isRepeated_"=>false, "isRequired_"=>false, "fieldType_"=>14, "nativeType_"=>{"FROM_NUMBER_WITH_PLUS_SIGN"=>1, "FROM_NUMBER_WITH_IDD"=>5, "FROM_NUMBER_WITHOUT_PLUS_SIGN"=>10, "FROM_DEFAULT_COUNTRY"=>20}, "deserializationConversionPermitted_"=>false, "defaultValue_"=>1}, 7=>{"tag_"=>"7", "name_"=>"preferred_domestic_carrier_code", "isRepeated_"=>false, "isRequired_"=>false, "fieldType_"=>9, "deserializationConversionPermitted_"=>false, "defaultValue_"=>nil}, 8=>{"tag_"=>"8", "name_"=>"number_of_leading_zeros", "isRepeated_"=>false, "isRequired_"=>false, "fieldType_"=>5, "deserializationConversionPermitted_"=>false, "defaultValue_"=>1}}, "lazyDeserializer_"=>nil, "deserializedFields_"=>nil}, "country"=>"IN", "type"=>1, "e164_number"=>"+917893461642", "national_format"=>"+91 78 93 461642", "is_possible_number"=>true, "validation_reasult"=>"IS_POSSIBLE", "is_valid"=>true, "out_of_country_us_format"=>"011 91 78 93 461642"}

libphonenumber.region # => Andhra Pradesh
libphonenumber.carrier # => Airtel

However, the entire js context of the library is available via the `context` accessor:

libphonenumber.context.call "i18n.phonenumbers.PhoneNumberUtil.extractPossibleNumber", "19255550100"
Expand Down
83 changes: 81 additions & 2 deletions lib/libphonenumber-execjs/libphonenumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,86 @@ def normalize(str="")
end

def parse(str="", default_region="ZZ")
context.call "i18n.phonenumbers.PhoneNumberUtil.getInstance().parse", str, default_region
#context.call "i18n.phonenumbers.PhoneNumberUtil.getInstance().parse", str, default_region
@parse = context.call '(function (a,b) {
try{
var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
var number = phoneUtil.parseAndKeepRawInput(a, b);
var VR = i18n.phonenumbers.PhoneNumberUtil.ValidationResult;
var _r = phoneUtil.isPossibleNumberWithReason(number);
var _isValid = phoneUtil.isValidNumber(number);
var _is_reason = null;
for(reason in VR){
if (_r == VR[reason]){
_is_reason = reason;
break;
}
}
m = phoneUtil.parse(a,b);
parsing_result = {}
for(f in m.fields_){
parsing_result[m.fields_[f].name_] = m.values_[m.fields_[f].tag_] || null
}
return {
util: {values_ : m.values_},
country: phoneUtil.getRegionCodeForNumber(number),
type: phoneUtil.getNumberType(number),
e164_number: phoneUtil.format(number, 0),
national_format: phoneUtil.format(number, 1),
is_possible_number: phoneUtil.isPossibleNumber(number),
validation_reasult: _is_reason,
is_valid: _isValid,
out_of_country_us_format: phoneUtil.formatOutOfCountryCallingNumber(number, "US"),
parsing_result: parsing_result
};
}catch(e){
return {
message: e,
error: true
}
}
})
', str, default_region
if @parse['util']
tmp = []
@parse['util']['values_'].map{|k,v| tmp[k] = v}
@parse['util']['values_'] = tmp
end
@parse.to_openstruct
end

def read_data(t='geocoding',a=nil, b=nil)
self.parse(a,b) if @parse.nil?
region_of_number = 'UNKNOWN'
if @parse['e164_number'].nil?
else
cc = @parse['util']['values_'][1]
code = @parse['e164_number']
region_file = File.join(File.dirname(__FILE__), "..", "..", "support", t, "en", "#{cc}.txt")
if File.exists? region_file
content = File.read(region_file)
content = content.split("\n\n")
content = content.last.split("\n").reverse
content.each { |e|
_e = e.split('|')
# puts "#{_e[0]}=#{_e[1]}"
if code[1, _e[0].to_s.length] == _e[0]
region_of_number = _e[1]
break
end
}
else
end
end
region_of_number
end

def region(a=nil,b=nil)
read_data('geocoding',a,b)
end

def carrier(a=nil,b=nil)
read_data('carrier',a,b)
end

def simple
Expand All @@ -39,4 +118,4 @@ def get_e164_phone_number_with_meta(str, cc=nil, ndc=nil)

end

end
end
Loading