phones 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/phones/parser.rb CHANGED
@@ -37,6 +37,10 @@ module Phones
37
37
  @number
38
38
  end
39
39
 
40
+ def self.verbose!
41
+ @verbose = true
42
+ end
43
+
40
44
  private
41
45
 
42
46
  def self.try_without_delimiters!
@@ -44,27 +48,47 @@ module Phones
44
48
  # Let's find out if there is a country code
45
49
  country_code = "+1"
46
50
  # It's easiest to tell when it starts with a plus, so let's start with that
51
+ log "Number: #{self.number}"
47
52
  if self.number[0] == "+"
48
53
  # Estados Unidos!
49
54
  if self.number[1] == '1' && self.number.length == "+19252008843".length
50
55
  parts = [self.number[2..4], self.number[5..-1]]
51
56
  # 3 Digit country code?
52
- elsif COUNTRY_CODES.include?(self.number[1..4])
57
+ elsif COUNTRY_CODES.include?(self.number[1..3])
58
+ log "Three Digit Country Code: #{self.number[1..3]}"
53
59
  country_code = self.number[0..4]
54
60
  parts = [nil, self.number[5..-1]]
55
61
  # 2 Digit country code?
56
62
  elsif COUNTRY_CODES.include?(self.number[1..2])
63
+ log "Two Digit Country Code: #{self.number[1..2]}"
57
64
  country_code = self.number[0..2]
58
65
  parts = [nil, self.number[4..-1]]
59
66
  else
60
67
  # IDK, man
61
68
  raise ArgumentError, "Can't parse phone number"
62
69
  end
70
+ elsif self.number[0..1] == "00"
71
+ @number = self.number[2..-1]
72
+ if COUNTRY_CODES.include?(self.number[0..2])
73
+ log "Three Digit Country Code: #{self.number[0..2]}"
74
+ country_code = "+" + self.number[0..2]
75
+ parts = [nil, self.number[3..-1]]
76
+ # 2 Digit country code?
77
+ elsif COUNTRY_CODES.include?(self.number[0..1])
78
+ log "Two Digit Country Code: #{self.number[0..1]}"
79
+ country_code = "+" + self.number[0..1]
80
+ parts = [nil, self.number[2..-1]]
81
+ else
82
+ # IDK, man
83
+ raise ArgumentError, "Can't parse phone number"
84
+ end
63
85
  elsif self.number[0] == '1' && self.number.length == "19252008843".length
64
86
  parts = [self.number[1..3], self.number[4..-1]]
65
87
  elsif self.number.length == "9252008843".length
66
88
  parts = [self.number[0..2], self.number[3..-1]]
67
89
  end
90
+ log "Area Code: #{parts[0]}"
91
+ log "Local Code: #{parts[1]}"
68
92
 
69
93
  Phones::Phone.new(:country_code => country_code, :area_code => parts[0], :local_code => parts[1])
70
94
  rescue
@@ -74,7 +98,11 @@ module Phones
74
98
  self.number.gsub!(IGNORABLE_CHARACTERS, '')
75
99
  end
76
100
 
101
+ def self.log(message)
102
+ puts message if @verbose
103
+ end
104
+
77
105
  # Source: http://en.wikipedia.org/wiki/List_of_country_calling_codes
78
- COUNTRY_CODES = %w(20 210 211 212 213 216 218 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 260 261 262 263 264 265 266 267 268 269 27 290 291 297 298 299 30 31 32 33 34 350 351 352 353 354 355 356 357 358 359 36 370 371 372 373 374 375 376 377 378 379 380 381 382 385 386 387 388 389 40 41 420 421 423 43 44 45 46 47 48 49 500 501 502 503 504 505 506 507 508 509 51 52 53 54 55 56 57 58 590 591 592 593 594 595 596 597 598 599 60 61 62 63 64 65 66 670 672 673 674 675 676 677 678 679 680 681 682 683 685 686 687 688 689 690 691 692 800 808 81 82 84 850 852 853 855 856 86 870 878 880 881 882 883 886 888 90 91 92 93 94 95 960 961 962 963 964 965 966 967 968 970 971 972 973 974 975 976 977 979 98 991 992 993 994 995 996 998).freeze
106
+ COUNTRY_CODES = %w(77 20 210 211 212 213 216 218 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 260 261 262 263 264 265 266 267 268 269 27 290 291 297 298 299 30 31 32 33 34 350 351 352 353 354 355 356 357 358 359 36 370 371 372 373 374 375 376 377 378 379 380 381 382 385 386 387 388 389 40 41 420 421 423 43 44 45 46 47 48 49 500 501 502 503 504 505 506 507 508 509 51 52 53 54 55 56 57 58 590 591 592 593 594 595 596 597 598 599 60 61 62 63 64 65 66 670 672 673 674 675 676 677 678 679 680 681 682 683 685 686 687 688 689 690 691 692 800 808 81 82 84 850 852 853 855 856 86 870 878 880 881 882 883 886 888 90 91 92 93 94 95 960 961 962 963 964 965 966 967 968 970 971 972 973 974 975 976 977 979 98 991 992 993 994 995 996 998).freeze
79
107
  end
80
108
  end
data/lib/phones/phone.rb CHANGED
@@ -59,7 +59,7 @@ module Phones
59
59
 
60
60
  def validate_local_code!
61
61
  raise ArgumentError, "Local code: #{@local_code} contains restricted characters" if self.local_code =~ RESTRICTED_CHARACTERS
62
- raise ArugmentError, "Local code cannot be blank" if self.local_code.nil? || self.local_code.length == 0
62
+ raise ArgumentError, "Local code cannot be blank" if self.local_code.nil? || self.local_code.length == 0
63
63
  end
64
64
 
65
65
  end
@@ -1,3 +1,3 @@
1
1
  module Phones
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phones
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-20 00:00:00.000000000 Z
12
+ date: 2013-02-21 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Phones is a barebones phone parsing and formatting library
15
15
  email: