Class: Msisdn

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msisdn, default_country_code = '27') ⇒ Msisdn

leaving the default country code in to stop shit breaking where i used the old msisdn gem



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/msisdn.rb', line 9

def initialize(msisdn, default_country_code = '27')
  @original = msisdn
  # remove dashes and spaces
  msisdn.gsub!( /[\s()\-a-zA-Z]/, '') if msisdn
  # remove + prefix if there is one
  msisdn.gsub!(/\+/, '') if msisdn

  # check if the ZA international dialing code is included, and remove it
  # only remove the first occurence of 27, and only if its in the beginning of the string
  msisdn.sub!(/27/, '') if msisdn =~ /^27/
  # pad it on the left with zeros just in case
  @msisdn = msisdn.rjust(10, '0')
  @msisdn =~ /(\d{3})(\d{7})/
  # so now the first three digits *should* be the local dialing code
  @local_dialing_code, @national_number = $1, $2

  # for compatibility with armanddp's gem
  @dialing_code = @local_dialing_code
  @country_code = default_country_code
end

Instance Attribute Details

#country_codeObject

Returns the value of attribute country_code.



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

def country_code
  @country_code
end

#dialing_codeObject

Returns the value of attribute dialing_code.



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

def dialing_code
  @dialing_code
end

#local_dialing_codeObject

Returns the value of attribute local_dialing_code.



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

def local_dialing_code
  @local_dialing_code
end

#national_numberObject

Returns the value of attribute national_number.



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

def national_number
  @national_number
end

#originalObject

Returns the value of attribute original.



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

def original
  @original
end

Instance Method Details

#internationalObject

for compatibility with armanddp’s gem



31
# File 'lib/msisdn.rb', line 31

def international; international_number; end

#international_numberObject



33
34
35
# File 'lib/msisdn.rb', line 33

def international_number
  "27#{@local_dialing_code.gsub(/^0+/, '')}#{@national_number}"
end

#international_number_with_prefixObject



37
38
39
# File 'lib/msisdn.rb', line 37

def international_number_with_prefix
  "+#{international_number}"
end

#nationalObject



45
46
47
# File 'lib/msisdn.rb', line 45

def national
  @local_dialing_code + @national_number
end

#to_sObject



65
66
67
# File 'lib/msisdn.rb', line 65

def to_s
  international_number
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  valid_national_number? && valid_local_dialing_code?
end

#valid_local_dialing_code?Boolean

a valid local dialing code should be three digits long and start with a zero

Returns:

  • (Boolean)


61
62
63
# File 'lib/msisdn.rb', line 61

def valid_local_dialing_code?
  return (@local_dialing_code =~ /^[\d][^0]{2}$/) ? true : false
end

#valid_national?Boolean

for compatibility with armanddp’s gem

Returns:

  • (Boolean)


50
# File 'lib/msisdn.rb', line 50

def valid_national?; valid_national_number?; end

#valid_national_number?Boolean

a national number is valid if it has 7 digits and doesn’t start with zeroes

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/msisdn.rb', line 53

def valid_national_number?
  # removing the check for leading zero, since it is breaking things,
  # like the number 0730711059
  # @national_number =~ /^\d{7}$/ && @national_number !~ /^0/
  return (@national_number =~ /^\d{7}$/) ? true : false
end