Module: ParanoidStarlight::Converters

Defined in:
lib/paranoid_starlight/converters.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert_telephone(num, prefixcc = 421) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/paranoid_starlight/converters.rb', line 3

def convert_telephone(num, prefixcc = 421)
  telephone = num.to_s.dup
  prefixcc = prefixcc.to_s.dup
  
  # convert logic
  # possible formats, with or without spaces
    
  # mobile (CZ/SK)
  # CZ/SK have always 9 numbers
  # (1) +421 949 123 456
  # (2) 00421 949 123 456
  # (3) 0949 123 456 (10 with leading zero, only SK)
  # (4) 949 123 456
  
  # landline always 9 numbers
  # other regions - 04x
  # (3) 045 / 6893121
  # bratislava - 02
  # (3) 02 / 44250320
  # (1) 421-2-44250320
  # (1) +421 (2) 44250320
  # (x) ()44 250 320 (no chance to guess NDC (geographic prefix here))
  # and other formats from above
  
    
  # output is string
  # +421949123456
  # make integer from it call with to_i
  
  # remove all non-number characters
  # I don't care for alphabetic or special characters
  # in number
  telephone.gsub!(/\D/, '')
  
  cc = '420|421'

  # + is stripped in all numbers
  if match = /\A((?:#{cc})\d{9})\z/.match(telephone)
    number = match[1]
  elsif match = /\A00((?:#{cc})\d{9})\z/.match(telephone)
    number = match[1]
  elsif match = /\A0(\d{9})\z/.match(telephone)
    number = prefixcc + match[1]
  # number cannot begin with 0, when has 9 numbers
  elsif match = /\A([1-9]\d{8})\z/.match(telephone)
    number = prefixcc + match[1]
    
  else
    raise("Number is invalid")
  end
  
  '+' + number
end

.one_liner(input) ⇒ Object



69
70
71
# File 'lib/paranoid_starlight/converters.rb', line 69

def one_liner(input)
  input.to_s.strip.gsub(/\s+/m, ' ')
end

.telephone_will_convert?(telephone) ⇒ Boolean

There will be no .valid? function because it would need to handle number separators and other characters in harder way.

This number needs to be converted to international format before using it!

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/paranoid_starlight/converters.rb', line 64

def telephone_will_convert?(telephone)
  valid = convert_telephone(telephone) rescue nil
  valid.nil? ? false : true 
end

Instance Method Details

#convert_telephone(num, prefixcc = 421) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/paranoid_starlight/converters.rb', line 3

def convert_telephone(num, prefixcc = 421)
  telephone = num.to_s.dup
  prefixcc = prefixcc.to_s.dup
  
  # convert logic
  # possible formats, with or without spaces
    
  # mobile (CZ/SK)
  # CZ/SK have always 9 numbers
  # (1) +421 949 123 456
  # (2) 00421 949 123 456
  # (3) 0949 123 456 (10 with leading zero, only SK)
  # (4) 949 123 456
  
  # landline always 9 numbers
  # other regions - 04x
  # (3) 045 / 6893121
  # bratislava - 02
  # (3) 02 / 44250320
  # (1) 421-2-44250320
  # (1) +421 (2) 44250320
  # (x) ()44 250 320 (no chance to guess NDC (geographic prefix here))
  # and other formats from above
  
    
  # output is string
  # +421949123456
  # make integer from it call with to_i
  
  # remove all non-number characters
  # I don't care for alphabetic or special characters
  # in number
  telephone.gsub!(/\D/, '')
  
  cc = '420|421'

  # + is stripped in all numbers
  if match = /\A((?:#{cc})\d{9})\z/.match(telephone)
    number = match[1]
  elsif match = /\A00((?:#{cc})\d{9})\z/.match(telephone)
    number = match[1]
  elsif match = /\A0(\d{9})\z/.match(telephone)
    number = prefixcc + match[1]
  # number cannot begin with 0, when has 9 numbers
  elsif match = /\A([1-9]\d{8})\z/.match(telephone)
    number = prefixcc + match[1]
    
  else
    raise("Number is invalid")
  end
  
  '+' + number
end

#one_liner(input) ⇒ Object



69
70
71
# File 'lib/paranoid_starlight/converters.rb', line 69

def one_liner(input)
  input.to_s.strip.gsub(/\s+/m, ' ')
end

#telephone_will_convert?(telephone) ⇒ Boolean

There will be no .valid? function because it would need to handle number separators and other characters in harder way.

This number needs to be converted to international format before using it!

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/paranoid_starlight/converters.rb', line 64

def telephone_will_convert?(telephone)
  valid = convert_telephone(telephone) rescue nil
  valid.nil? ? false : true 
end