Class: BigPhoney::PhoneNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/big_phoney/phone_number.rb

Constant Summary collapse

MIN_US_LENGTH =
10
MINLEN =
7
EXTENSION =
/\s*(?:(?:ext|ex|xt|x)[\s.:]*(\d+))/i
COUNTRY_CODES =
%w[
  1  7  20  27  30  31  32  33  34
  36  39  40  41  43  44  45  46  47
  48  49  51  52  53  54  55  56  57
  58  60  61  62  63  64  65  66  81
  82  84  86  90  91  92  93  94  95
  98  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
  290  291  297  298  299  350  351  352  353
  354  355  356  357  358  359  370  371  372
  373  374  375  376  377  378  380  381  385
  386  387  388  389  420  421  423  500  501
  502  503  504  505  506  507  508  509  590
  591  592  593  594  595  596  597  598  599
  670  672  673  674  675  676  677  678  679
  680  681  682  683  684  685  686  687  688
  689  690  691  692  800  808  850  852  853
  655  856  870  871  872  873  874  878  880
  881  882  886  960  961  962  963  964  965
  966  967  968  970  971  972  973  974  975
  976  977  979  991  992  993  994  995  996
  998
]
AREA_CODES =
{
  :canada     => %w[204 250 289 306 403 416 418 450 506 514 519 604 613 647 705 709 778 780 807 819 867 902 905],
  :puero_rico => %w[787 939]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ PhoneNumber

Returns a new instance of PhoneNumber.



43
44
45
46
47
# File 'lib/big_phoney/phone_number.rb', line 43

def initialize(number)
  @number = number.to_s.strip
  parse_extension
  parse_country_code
end

Instance Attribute Details

#country_codeObject

Returns the value of attribute country_code.



41
42
43
# File 'lib/big_phoney/phone_number.rb', line 41

def country_code
  @country_code
end

#extObject Also known as: extension

Returns the value of attribute ext.



41
42
43
# File 'lib/big_phoney/phone_number.rb', line 41

def ext
  @ext
end

#numberObject

Returns the value of attribute number.



41
42
43
# File 'lib/big_phoney/phone_number.rb', line 41

def number
  @number
end

Class Method Details

.assume_us=(us) ⇒ Object



39
# File 'lib/big_phoney/phone_number.rb', line 39

def self.assume_us=(us) ; @assume_us = us end

.assume_us?Boolean

Returns:

  • (Boolean)


38
# File 'lib/big_phoney/phone_number.rb', line 38

def self.assume_us? ; @assume_us end

Instance Method Details

#area_codeObject



67
68
69
# File 'lib/big_phoney/phone_number.rb', line 67

def area_code
  number[0..2]
end

#errorsObject



79
80
81
# File 'lib/big_phoney/phone_number.rb', line 79

def errors
  @errors ||= []
end

#prefixObject



71
72
73
# File 'lib/big_phoney/phone_number.rb', line 71

def prefix
  number[3..5]
end

#restObject



75
76
77
# File 'lib/big_phoney/phone_number.rb', line 75

def rest
  number[6..9]
end

#to_sObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/big_phoney/phone_number.rb', line 49

def to_s
  result = '(%s) %s-%s' % [area_code, prefix, rest]

  if country_code != '1'
    result = '+%s %s' % [country_code, result]
  end

  if extension
    result = '%s ext. %s' % [result, extension]
  end

  result
end

#valid?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/big_phoney/phone_number.rb', line 63

def valid?
  errors.empty?
end