phoney 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -78,7 +78,7 @@ When given a symbol you can let the parser guess the best format and pass in one
78
78
  == TODOs
79
79
 
80
80
  - More test specs for different countries
81
- - Regions with dialout prefixes that use wild-cards are not yet implemented
81
+ - Correctly handle numbers that have some digits BEFORE the area_code!
82
82
 
83
83
  == Licence
84
84
 
@@ -2,4 +2,7 @@ require File.join(File.dirname(__FILE__), 'phoney', 'version')
2
2
  require File.join(File.dirname(__FILE__), 'phoney', 'utils')
3
3
  require File.join(File.dirname(__FILE__), 'phoney', 'region')
4
4
  require File.join(File.dirname(__FILE__), 'phoney', 'parser')
5
- require File.join(File.dirname(__FILE__), 'phoney', 'base')
5
+ require File.join(File.dirname(__FILE__), 'phoney', 'base')
6
+
7
+ # Load our region file when we require the library
8
+ PhoneNumber::Region.load
@@ -26,22 +26,28 @@ class PhoneNumber
26
26
  national_prefix = get_national_prefix(phone_number, region)
27
27
  dialout_region = get_dialout_region(phone_number, region)
28
28
  dialout_country = ''
29
- rule_sets = get_rule_sets_for_region(phone_number, dialout_region || region)
29
+
30
+ # No dialout prefix without a dialout region, and no dialout region without a prefix
31
+ if((dialout_region && dialout_prefix.empty?) || (!dialout_region && !dialout_prefix.empty?))
32
+ rule_sets = []
33
+ else
34
+ rule_sets = get_rule_sets_for_region(phone_number, dialout_region || region)
35
+ end
30
36
 
31
37
  # build our total prefix
32
38
  if dialout_region
33
- prefix = dialout_prefix.delete(' ') + dialout_region.country_code.to_s
34
- country_code = dialout_region.country_code.to_s
39
+ prefix = dialout_prefix.delete(' ') + dialout_region.country_code.to_s
40
+ country_code = dialout_region.country_code.to_s
35
41
  dialout_country = country_code
36
42
  else
37
43
  prefix = national_prefix
38
- prefix += dialout_prefix unless(dialout_prefix.empty?)
44
+ prefix += dialout_prefix.delete(' ') unless(dialout_prefix.empty?)
39
45
  end
40
46
 
41
47
  # strip the total prefix from the beginning of the number
42
48
  phone_number = phone_number[prefix.length..-1]
43
49
  number = phone_number
44
-
50
+
45
51
  prefered_type = 0 # for sorting the priority
46
52
 
47
53
  # if we're dialing out or using the national prefix
@@ -54,7 +60,7 @@ class PhoneNumber
54
60
  rule_sets.each do |rule_set|
55
61
  rule_set[:rules] = rule_set[:rules].sort_by do |rule|
56
62
  # [ prefered rule type ASC, total_digits ASC ]
57
- [ (rule[:type]==prefered_type) ? -1 : rule[:type], rule[:total_digits] ]
63
+ [ (rule[:type]==prefered_type) ? -1 : rule[:type], rule[:total_digits], rule[:index] ]
58
64
  end
59
65
  end
60
66
 
@@ -105,14 +111,16 @@ class PhoneNumber
105
111
  # strip possible whitespace
106
112
  phone_number.rstrip!
107
113
  phone_number.lstrip!
114
+ # remove possible non-numeric characters from the (invalid) number
115
+ number.gsub!(/[^0-9]/,'') if number
108
116
 
109
117
  # Finally...we can output our parts as a hash
110
- { :formatted_number => phone_number, :area_code => area_code, :country_code => country_code, :number => normalize(number) }
118
+ { :formatted_number => phone_number, :area_code => area_code, :country_code => country_code, :number => number }
111
119
  end
112
120
 
113
- private
121
+ # private
114
122
  def get_rule_sets_for_region(string, region)
115
- rule_sets = []
123
+ rule_sets = []
116
124
 
117
125
  if(region && region.rule_sets)
118
126
  rule_sets = region.rule_sets.select do |rule_set|
@@ -123,14 +131,15 @@ class PhoneNumber
123
131
  rule_sets
124
132
  end
125
133
 
126
- def find_matching_rule(string, rule_sets)
134
+ def find_matching_rule(number, rule_sets)
135
+ return nil if !number.match(/\A[0-9]+\Z/)
127
136
  match = nil
128
137
 
129
138
  # go through all our given rules
130
139
  for rule_set in rule_sets do
131
140
  digits = rule_set[:digits]
132
- prefix = string[0,digits].to_i
133
- rules = rule_set[:rules].select { |rule| rule[:total_digits] >= string.length }
141
+ prefix = number[0,digits].to_i
142
+ rules = rule_set[:rules].select { |rule| rule[:total_digits] >= number.length }
134
143
 
135
144
  rules.each do |rule|
136
145
  if(prefix >= rule[:min] && prefix <= rule[:max])
@@ -50,6 +50,16 @@ class PhoneNumber
50
50
  @dialout_prefixes = hash[:dialout_prefixes]
51
51
 
52
52
  @rule_sets = hash[:rule_sets]
53
+
54
+ if(@rule_sets)
55
+ for rule_set in @rule_sets do
56
+ if(rule_set[:rules])
57
+ rule_set[:rules].each_with_index do |rule,index|
58
+ rule.merge!(:index => index)
59
+ end
60
+ end
61
+ end
62
+ end
53
63
  end
54
64
 
55
65
  def to_s
@@ -3,7 +3,7 @@ class PhoneNumber
3
3
  module VERSION #:nodoc:
4
4
  MAJOR = 0
5
5
  MINOR = 0
6
- TINY = 3
6
+ TINY = 4
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
9
9
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{phoney}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jan Habermann"]
@@ -8,6 +8,8 @@ describe PhoneNumber::Parser do
8
8
  end
9
9
 
10
10
  it "should format a [:br] (Brazil) phone number" do
11
+ PhoneNumber::Parser.parse("03001234567").should == "0300 123-4567"
12
+
11
13
  PhoneNumber::Parser.parse("123").should == "123"
12
14
  PhoneNumber::Parser.parse("1234").should == "123-4"
13
15
  PhoneNumber::Parser.parse("12345").should == "123-45"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phoney
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Habermann