Skip to content

Commit

Permalink
Merge pull request sstephenson#1 from flyerhzm/master
Browse files Browse the repository at this point in the history
These additions have been made -
* Ability to parse a phone number without territory being supplied.
* Ability to determine the type of the phone number. Possible types are - :fixed_line, :mobile, :fixed_line_or_mobile, :toll_free, :premium_rate, ...
* Methods to check if a number is fixed_line or mobile. If the type of the number is :fixed_line_or_mobile then both these methods will return true in such a case.
  • Loading branch information
platphorm committed Jun 13, 2013
2 parents ca0e315 + a6b321a commit 9df075a
Show file tree
Hide file tree
Showing 5 changed files with 5,317 additions and 180 deletions.
20 changes: 20 additions & 0 deletions lib/global_phone/database_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ def compile_territory(node)
territory_name(node),
pattern(node, "generalDesc possibleNumberPattern"),
pattern(node, "generalDesc nationalNumberPattern"),
pattern(node, "premiumRate possibleNumberPattern"),
pattern(node, "premiumRate nationalNumberPattern"),
pattern(node, "tollFree possibleNumberPattern"),
pattern(node, "tollFree nationalNumberPattern"),
pattern(node, "sharedCost possibleNumberPattern"),
pattern(node, "sharedCost nationalNumberPattern"),
pattern(node, "voip possibleNumberPattern"),
pattern(node, "voip nationalNumberPattern"),
pattern(node, "personalNumber possibleNumberPattern"),
pattern(node, "personalNumber nationalNumberPattern"),
pattern(node, "pager possibleNumberPattern"),
pattern(node, "pager nationalNumberPattern"),
pattern(node, "uan possibleNumberPattern"),
pattern(node, "uan nationalNumberPattern"),
pattern(node, "voicemail possibleNumberPattern"),
pattern(node, "voicemail nationalNumberPattern"),
pattern(node, "fixedLine possibleNumberPattern"),
pattern(node, "fixedLine nationalNumberPattern"),
pattern(node, "mobile possibleNumberPattern"),
pattern(node, "mobile nationalNumberPattern"),
squish(node["nationalPrefixFormattingRule"])
]
end
Expand Down
17 changes: 13 additions & 4 deletions lib/global_phone/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ def self.normalize(string)
gsub(NON_DIALABLE_CHARS, "")
end

attr_reader :territory, :national_string
attr_reader :territory, :national_string, :type

def_delegator :territory, :region
def_delegator :territory, :country_code
def_delegator :territory, :national_prefix
def_delegator :territory, :national_pattern
def_delegator :territory, :general_desc_national_pattern

def initialize(territory, national_string)
def initialize(territory, national_string, type)
@territory = territory
@national_string = national_string
@type = type
end

def national_format
Expand Down Expand Up @@ -54,7 +55,15 @@ def international_format
end

def valid?
!!(format && national_string =~ national_pattern)
!!(format && national_string =~ general_desc_national_pattern)
end

def fixed_line?
@type == :fixed_line
end

def mobile?
@type == :mobile
end

def inspect
Expand Down
56 changes: 51 additions & 5 deletions lib/global_phone/territory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,29 @@
module GlobalPhone
class Territory < Record
field 0, :name
field 1, :possible_pattern do |p| /^#{p}$/ end
field 2, :national_pattern do |p| /^#{p}$/ end
field 3, :national_prefix_formatting_rule
field 1, :general_desc_possible_pattern do |p| /^#{p}$/ end
field 2, :general_desc_national_pattern do |p| /^#{p}$/ end
field 3, :premium_rate_possible_pattern do |p| /^#{p}$/ end
field 4, :premium_rate_national_pattern do |p| /^#{p}$/ end
field 5, :toll_free_possible_pattern do |p| /^#{p}$/ end
field 6, :toll_free_national_pattern do |p| /^#{p}$/ end
field 7, :shared_cost_possible_pattern do |p| /^#{p}$/ end
field 8, :shared_cost_national_pattern do |p| /^#{p}$/ end
field 9, :voip_possible_pattern do |p| /^#{p}$/ end
field 10, :voip_national_pattern do |p| /^#{p}$/ end
field 11, :personal_number_possible_pattern do |p| /^#{p}$/ end
field 12, :personal_number_national_pattern do |p| /^#{p}$/ end
field 13, :pager_possible_pattern do |p| /^#{p}$/ end
field 14, :pager_national_pattern do |p| /^#{p}$/ end
field 15, :uan_possible_pattern do |p| /^#{p}$/ end
field 16, :uan_national_pattern do |p| /^#{p}$/ end
field 17, :voicemail_possible_pattern do |p| /^#{p}$/ end
field 18, :voicemail_national_pattern do |p| /^#{p}$/ end
field 19, :fixed_line_possible_pattern do |p| /^#{p}$/ end
field 20, :fixed_line_national_pattern do |p| /^#{p}$/ end
field 21, :mobile_possible_pattern do |p| /^#{p}$/ end
field 22, :mobile_national_pattern do |p| /^#{p}$/ end
field 23, :national_prefix_formatting_rule

attr_reader :region

Expand All @@ -23,7 +43,8 @@ def initialize(data, region)

def parse_national_string(string)
string = normalize(string)
Number.new(self, string) if possible?(string)
@number_type = number_type(string)
Number.new(self, string, @number_type) if possible?(string)
end

def inspect
Expand All @@ -48,7 +69,32 @@ def normalize(string)
end

def possible?(string)
string =~ possible_pattern
:unknown != number_type(string)
end

def number_type(string)
return :unknown if !general_desc_national_pattern || !number_matching_desc?(string, general_desc_possible_pattern, general_desc_national_pattern)
return :premium_rate if number_matching_desc?(string, premium_rate_possible_pattern, premium_rate_national_pattern)
return :toll_free if number_matching_desc?(string, toll_free_possible_pattern, toll_free_national_pattern)
return :shared_cost if number_matching_desc?(string, shared_cost_possible_pattern, shared_cost_national_pattern)
return :voip if number_matching_desc?(string, voip_possible_pattern, voip_national_pattern)
return :personal_number if number_matching_desc?(string, personal_number_possible_pattern, personal_number_national_pattern)
return :pager if number_matching_desc?(string, pager_possible_pattern, pager_national_pattern)
return :uan if number_matching_desc?(string, uan_possible_pattern, uan_national_pattern)
return :voicemail if number_matching_desc?(string, voicemail_possible_pattern, voicemail_national_pattern)
if number_matching_desc?(string, fixed_line_possible_pattern, fixed_line_national_pattern)
if number_matching_desc?(string, mobile_possible_pattern, mobile_national_pattern)
return :fixed_line_or_mobile
else
return :fixed_line
end
end
return :mobile if number_matching_desc?(string, mobile_possible_pattern, mobile_national_pattern)
:unknown
end

def number_matching_desc?(string, possible_pattern, national_pattern)
string =~ /^#{possible_pattern ? possible_pattern : general_desc_possible_pattern}$/ && string =~ /^#{national_pattern}$/
end

def starts_with_national_prefix?(string)
Expand Down
Loading

0 comments on commit 9df075a

Please sign in to comment.