Skip to content

Commit

Permalink
add Number#type, Number#mobile? and Number#fixed_line? method
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Jun 13, 2013
1 parent 682aed2 commit a6b321a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
13 changes: 11 additions & 2 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, :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 @@ -57,6 +58,14 @@ def valid?
!!(format && national_string =~ general_desc_national_pattern)
end

def fixed_line?
@type == :fixed_line
end

def mobile?
@type == :mobile
end

def inspect
"#<#{self.class.name} territory=#{territory.inspect} national_string=#{national_string.inspect}>"
end
Expand Down
13 changes: 10 additions & 3 deletions lib/global_phone/territory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,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 Down Expand Up @@ -81,13 +82,19 @@ def number_type(string)
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)
return :fixed_line if number_matching_desc?(string, fixed_line_possible_pattern, fixed_line_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
string =~ /^#{possible_pattern ? possible_pattern : general_desc_possible_pattern}$/ && string =~ /^#{national_pattern}$/
end

def starts_with_national_prefix?(string)
Expand Down
21 changes: 21 additions & 0 deletions test/number_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ class NumberTest < TestCase
assert number.valid?
end

test "type" do
number = context.parse("(312) 555-1212")
assert_equal :fixed_line_or_mobile, number.type
end

test "fixed_line?" do
number = context.parse("+8602152821021")
assert number.fixed_line?

number = context.parse("+8615800681509")
assert !number.fixed_line?
end

test "mobile?" do
number = context.parse("+8602152821021")
assert !number.mobile?

number = context.parse("+8615800681509")
assert number.mobile?
end

test "country_code" do
number = context.parse("(312) 555-1212")
assert_equal "1", number.country_code
Expand Down

0 comments on commit a6b321a

Please sign in to comment.