Class: NT54::PhoneNumber

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePhoneNumber

Returns a new instance of PhoneNumber.



7
8
9
10
11
# File 'lib/nt54/phone_number.rb', line 7

def initialize
  [:country_code, :area_code, :local_prefix, :local_number, :special_sequence].each do |var|
    instance_variable_set :"@#{var}", ""
  end
end

Instance Attribute Details

#area_codeObject

Returns the value of attribute area_code.



4
5
6
# File 'lib/nt54/phone_number.rb', line 4

def area_code
  @area_code
end

#cityObject

Returns the value of attribute city.



4
5
6
# File 'lib/nt54/phone_number.rb', line 4

def city
  @city
end

#country_codeObject

Returns the value of attribute country_code.



4
5
6
# File 'lib/nt54/phone_number.rb', line 4

def country_code
  @country_code
end

#local_numberObject

Returns the value of attribute local_number.



4
5
6
# File 'lib/nt54/phone_number.rb', line 4

def local_number
  @local_number
end

#local_prefixObject

Returns the value of attribute local_prefix.



4
5
6
# File 'lib/nt54/phone_number.rb', line 4

def local_prefix
  @local_prefix
end

#mobileObject

Returns the value of attribute mobile.



4
5
6
# File 'lib/nt54/phone_number.rb', line 4

def mobile
  @mobile
end

#province_codeObject

Returns the value of attribute province_code.



4
5
6
# File 'lib/nt54/phone_number.rb', line 4

def province_code
  @province_code
end

#specialObject

Returns the value of attribute special.



4
5
6
# File 'lib/nt54/phone_number.rb', line 4

def special
  @special
end

#special_sequenceObject

Returns the value of attribute special_sequence.



4
5
6
# File 'lib/nt54/phone_number.rb', line 4

def special_sequence
  @special_sequence
end

Instance Method Details

#areaObject



13
14
15
16
# File 'lib/nt54/phone_number.rb', line 13

def area
  @area ||= Area.get(area_code)
rescue Ambry::NotFoundError
end

#fix!Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nt54/phone_number.rb', line 63

def fix!
  if real_length == 3 && special_sequence == ""
    @special_sequence = "#{area_code}#{local_prefix}"
    @area_code = ""
    @local_prefix = ""
    return self
  end

  @country_code = "54" if @country_code == ""
  # If we didn't dial an area code, then we don't really know how long the
  # prefix/local numbers are, so fake it.
  unless area_code
    until @local_number.length >= @local_prefix.length
      @local_number.insert(0, @local_prefix.slice!(-1))
    end
  end
  self
end

#format_internationalObject



31
32
33
# File 'lib/nt54/phone_number.rb', line 31

def format_international
  "+#{country_code} #{mobile? ? 9 : ''} (#{area_code}) #{local_prefix}-#{local_number}".squeeze(' ')
end

#format_international_smsObject



35
36
37
# File 'lib/nt54/phone_number.rb', line 35

def format_international_sms
  "+#{country_code} (#{area_code}) #{local_prefix}-#{local_number}"
end

#format_localObject



43
44
45
# File 'lib/nt54/phone_number.rb', line 43

def format_local
  "#{mobile? ? 15 : ''} #{local_prefix}-#{local_number}".squeeze(' ')
end

#format_nationalObject



39
40
41
# File 'lib/nt54/phone_number.rb', line 39

def format_national
  "(0#{area_code}) #{mobile? ? 15 : ''} #{local_prefix}-#{local_number}".squeeze(' ')
end

#mobile?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/nt54/phone_number.rb', line 18

def mobile?
  !! mobile
end

#real_lengthObject



47
48
49
# File 'lib/nt54/phone_number.rb', line 47

def real_length
  area_code.length + local_prefix.length + local_number.length + special_sequence.length
end

#special?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/nt54/phone_number.rb', line 27

def special?
  !! special
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  if special? && real_length == 3
    return true
  end
  return false unless area_code
  return false unless area
  return false unless local_prefix.length >= 2
  return false unless local_number.length >= 3
  return false unless real_length == 10
  true
end