Module: GemCNS

Defined in:
lib/gemCNS.rb,
lib/gemCNS/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.generateObject

Your code goes here…



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
# File 'lib/gemCNS.rb', line 6

def GemCNS.generate

                gera0 = ( rand 3 ) + 1

                if gera0 == 3  
                    gera0 = ( rand 3 ) + 7
                end

                gera1 = ( rand 99999 ) + 1
                gera2 = ( rand 99999 ) + 1
  
                cns = gera0.to_s + gera1.to_s.rjust(5, '0') + gera2.to_s.rjust(5, '0')

                soma = toInteract(cns)
                resto = soma % 11
                dv = 11 - resto
  
                if dv == 11 
                    dv = 0
                end

                if dv == 10 
                   soma = toInteract(cns)
                   soma += 2
                   resto = soma % 11
                   dv = 11 - resto
                   cns += '001' + dv.to_s
                else
                   cns += '000' + dv.to_s
                end

            #puts cns

resultado = cns
end

.isValid(cns) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gemCNS.rb', line 43

def GemCNS.isValid(cns)
   
   if cns.length < 15
      false
   else
      tste = cns[0..10]
      soma = toInteract(tste)
      resto = soma % 11
      dv = 11 - resto
   
      if dv == 11 
         dv = 0
      end
 
      if dv == 10 
         soma = toInteract(tste)
         soma += 2
         resto = soma % 11
         dv = 11 - resto
         tste += '001' + dv.to_s
      else
         tste += '000' + dv.to_s
      end

      if tste == cns
         true
      else
         false
      end
   end

end