Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,32 @@ The *normalize* method also takes a number as a string but returns it in the sta

Country info is stored in a hash of hashes with the CC being the key.

E164::CountryCodes['1'] #=> {:national_destination_codes => 3, :abbreviation => 'NANP', :description => 'North American Numbering Plan', :info => 'en.wikipedia.com.org/wiki/NANP'}
::CountryCodes['1'] #=> {:national_destination_codes => 3, :abbreviation => 'NANP', :description => 'North American Numbering Plan', :info => 'en.wikipedia.com.org/wiki/NANP'}

NDC information is available for:

* NANP
* Austria
* Germany
* Romania

Just like the CountryCodes, NDC info is stored in a hash of hashes and accessed through a constant. Some NDCs will have more information than others.

E164::NANP('303') #=> {:abbreviation => 'Colorado', :description => '[Colorado] (Boulder, Longmont, Aurora, Denver and central Colorado, overlays with [720])', :info => 'en.wikipedia.com.org/wiki/Area_codes_303_and_720'}
::NANP('303') #=> {:abbreviation => 'Colorado', :description => '[Colorado] (Boulder, Longmont, Aurora, Denver and central Colorado, overlays with [720])', :info => 'en.wikipedia.com.org/wiki/Area_codes_303_and_720'}

E164::Austria['1'] => {:description => 'Wien'}
::Austria['1'] => {:description => 'Wien'}

E164::Germany['10'] => {:description => 'Call-By-Call'}
::Germany['10'] => {:description => 'Call-By-Call'}

::Romania['256'] => {:description => 'Timișoara'}

You can also check if a phone number is a mobile phone or not.

E164.is_mobile?('+40720123123') #=> true
E164.is_mobile?('+40256123123') #=> false

This works for Romania and Austria.

ToDo
----

Expand Down
31 changes: 19 additions & 12 deletions lib/e164.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
module E164

Dir.glob(File.join(File.dirname(__FILE__), 'e164/*.rb')).each {|f| require f }

ValidFormat = /^\+([\d]{1,3})([\d]{1,14})$/
Identifiers = ['+','011', '00']
DefaultIdentifier = '+'
DefaultCountryCode = '1' # Override by E164.set_default_country_code!()

def self.normalize(num)
parse(num).unshift(DefaultIdentifier).join
end

def self.parse(num)
num = e164_clean(num)
identifier = Identifiers.include?(num.first) ? num.shift : nil

num = (identifier || num.join.start_with?(DefaultCountryCode)) ? join_country_code(num) : num.unshift(DefaultCountryCode)
num = join_national_destination_code(num)

[num.shift,num.shift,num.join]
end

def self.e164_clean(num)
num.scan(/^(?:#{Identifiers.map{|i| Regexp.escape(i) }.join('|')})|[\d]/)
end

def self.join_country_code(num)
potentials = (1..3).map {|l| num[0,l].join}
country_code = potentials.map {|x| CountryCodes[x] ? x : nil}.compact.first
Expand All @@ -33,13 +33,13 @@ def self.join_country_code(num)
country_code = DefaultCountryCode
num.unshift(country_code)
end

[num.shift(country_code.length).join, *num]
end

def self.join_national_destination_code(num)
country = CountryCodes[num[0]]

case (destination_codes = country[:national_destination_codes])
when Integer
destination_code_length = destination_codes
Expand All @@ -48,7 +48,7 @@ def self.join_national_destination_code(num)
destination_code = potentials.map {|x| destination_codes[x] ? x : nil}.compact.first
destination_code_length = (destination_code && destination_code.length) || destination_codes[:default]
end

[num.shift, num.shift(destination_code_length).join, *num]
end

Expand All @@ -57,4 +57,11 @@ def self.set_default_country_code!(country_code)
const_set(:DefaultCountryCode, country_code)
ret_value
end

def self.is_mobile?(num)
country=E164.parse(num)[0]
ndc=E164.parse(num)[1]
return false if CountryCodes[country][:national_destination_codes][ndc].nil?
CountryCodes[country][:national_destination_codes][ndc][:is_mobile]||false
end
end
2 changes: 1 addition & 1 deletion lib/e164/country_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'36' => {:national_destination_codes => 2, :abbreviation => '', :description => 'Hungary', :info => 'en.wikipedia.com.org/wiki/Hungary'},
'39' => {:national_destination_codes => 3, :abbreviation => '', :description => 'Italy', :info => 'en.wikipedia.com.org/wiki/Italy'},

'40' => {:national_destination_codes => 2, :abbreviation => '', :description => 'Romania', :info => 'en.wikipedia.com.org/wiki/Romania'},
'40' => {:national_destination_codes => Romania, :abbreviation => '', :description => 'Romania', :info => 'en.wikipedia.com.org/wiki/Romania'},
'41' => {:national_destination_codes => 2, :abbreviation => '', :description => 'Switzerland', :info => 'en.wikipedia.com.org/wiki/Switzerland'},
'43' => {:national_destination_codes => Austria, :abbreviation => '', :description => 'Austria', :info => 'en.wikipedia.com.org/wiki/Austria'},
'44' => {:national_destination_codes => 2, :abbreviation => '', :description => 'United Kingdom', :info => 'en.wikipedia.com.org/wiki/United_Kingdom'},
Expand Down
Loading