Module: E164
- Defined in:
- lib/e164.rb
Constant Summary collapse
- ValidFormat =
/^\+([\d]{1,3})([\d]{1,14})$/
- Identifiers =
['+','011', '00']
- DefaultIdentifier =
'+'
- DefaultCountryCode =
Override by E164.set_default_country_code!()
'1'
Class Method Summary collapse
- .e164_clean(num) ⇒ Object
- .join_country_code(num) ⇒ Object
- .join_national_destination_code(num) ⇒ Object
- .normalize(num) ⇒ Object
- .parse(num) ⇒ Object
- .set_default_country_code!(country_code) ⇒ Object
Class Method Details
.e164_clean(num) ⇒ Object
24 25 26 |
# File 'lib/e164.rb', line 24 def self.e164_clean(num) num.scan(/^(?:#{Identifiers.map{|i| Regexp.escape(i) }.join('|')})|[\d]/) end |
.join_country_code(num) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/e164.rb', line 28 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 unless country_code country_code = DefaultCountryCode num.unshift(country_code) end [num.shift(country_code.length).join, *num] end |
.join_national_destination_code(num) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/e164.rb', line 40 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 when Hash potentials = destination_codes[:range].map {|l| num[1,l].join} 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 |
.normalize(num) ⇒ Object
10 11 12 |
# File 'lib/e164.rb', line 10 def self.normalize(num) parse(num).unshift(DefaultIdentifier).join end |
.parse(num) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/e164.rb', line 14 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 |
.set_default_country_code!(country_code) ⇒ Object
55 56 57 58 59 |
# File 'lib/e164.rb', line 55 def self.set_default_country_code!(country_code) ret_value = remove_const(:DefaultCountryCode) const_set(:DefaultCountryCode, country_code) ret_value end |