Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

fix the database generator and Number#valid? #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
14 changes: 12 additions & 2 deletions lib/global_phone/database_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def example_numbers_selector
end

def example_number_types_to_exclude
%w( emergency shortCode )
%w( emergency shortCode pager voicemail )
end

def compile_region(territory_nodes, country_code)
Expand Down Expand Up @@ -101,10 +101,20 @@ def compile_territory(node)
territory_name(node),
pattern(node, "generalDesc possibleNumberPattern"),
pattern(node, "generalDesc nationalNumberPattern"),
squish(node["nationalPrefixFormattingRule"])
squish(node["nationalPrefixFormattingRule"]),
compile_possible_formats(node)
]
end

def compile_possible_formats(node)
%w( fixedLine mobile voip tollFree premiumRate sharedCost uan personalNumber ).map do |possible_format|
[
pattern(node, "#{possible_format} possibleNumberPattern"),
pattern(node, "#{possible_format} nationalNumberPattern")
].reject { |pattern| pattern.nil? || pattern =~ /^NA$/i }
end.reject(&:empty?)
end

def compile_formats(territory_nodes)
format_nodes_for(territory_nodes).map do |node|
truncate(compile_format(node))
Expand Down
12 changes: 11 additions & 1 deletion lib/global_phone/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def self.normalize(string)
def_delegator :territory, :country_code
def_delegator :territory, :national_prefix
def_delegator :territory, :national_pattern
def_delegator :territory, :possible_pattern
def_delegator :territory, :valid_number_formats

def initialize(territory, national_string)
@territory = territory
Expand Down Expand Up @@ -54,7 +56,9 @@ def international_format
end

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

def inspect
Expand All @@ -66,6 +70,12 @@ def to_s
end

protected
def match_any_valid_pattern?
valid_number_formats.any? do |formats|
formats.all? { |format| national_string =~ format }
end
end

def format
@format ||= find_format_for(national_string)
end
Expand Down
20 changes: 14 additions & 6 deletions lib/global_phone/territory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
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 1, :possible_pattern do |p| /^(#{p})$/ end
field 2, :national_pattern do |p| /^(#{p})$/ end
field 3, :national_prefix_formatting_rule
field 4, :valid_number_formats do |formats|
formats.map do |format|
format.map { |f| /^(#{f})$/ }
end
end

attr_reader :region

Expand All @@ -32,23 +37,26 @@ def inspect

protected
def strip_national_prefix(string)
if national_prefix_for_parsing
if string =~ national_prefix_for_parsing
transform_rule = national_prefix_transform_rule || ""
transform_rule = transform_rule.gsub("$", "\\")
string_without_prefix = string.sub(national_prefix_for_parsing, transform_rule)
elsif starts_with_national_prefix?(string)
string_without_prefix = string[national_prefix.length..-1]
end

possible?(string_without_prefix) ? string_without_prefix : string
national_number?(string_without_prefix) ? string_without_prefix : string
end

def normalize(string)
strip_national_prefix(Number.normalize(string))
end

def possible?(string)
string =~ possible_pattern
!!(string =~ possible_pattern)
end

def national_number?(string)
!!(string =~ national_pattern)
end

def starts_with_national_prefix?(string)
Expand Down
Loading