Class: RUT

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

Overview

All functions are just defined statically in this module

Class Method Summary collapse

Class Method Details

.format(raw_rut) ⇒ Object

This method will give a raw R.U.T. string its proper format adding the right points & hyphens



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/chilean_rut.rb', line 126

def self.format(raw_rut)
  rut = raw_rut.to_s.delete '.-'
  if rut.nil? || rut.empty?
    return rut
  end
  rut_end = rut[rut.length - 1, rut.length]
  rut_init_temp = rut[0, rut.length - 1]
  rut_init = ''
  while rut_init_temp.length > 3 do
    rut_init = '.' + rut_init_temp[rut_init_temp.length - 3, rut_init_temp.length] + rut_init
    rut_init_temp = rut_init_temp[0, rut_init_temp.length - 3]
  end
  rut = rut_init_temp+rut_init+'-'+rut_end
  return rut.upcase
end

.get_dv(rut) ⇒ Object

Every R.U.T. has a single valid Digito Verificador which is calculated based on its digits

Given a R.U.T. this function returns its Digito Verificador as a string



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chilean_rut.rb', line 15

def self.get_dv(rut)
  suma=0
  mul=2
  rut.reverse.split("").each do |c|
    suma=suma+c.to_i*mul
    if mul==7
      mul=2
    else
      mul+=1
    end
  end
  res=suma%11
  if res==1
    return 'k'
  elsif res==0
    return '0'
  else
    return 11-res
  end
end

.get_rut_and_dv(rut) ⇒ Object

This method will give a raw R.U.T. string this method returns boolean wether it no is valid or a hash if string is a valid rut



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/chilean_rut.rb', line 144

def self.get_rut_and_dv(rut)
  unless self.validate(rut)
    return false
  end

  rut = self.format(rut).split("-")

  out = {
    :rut => rut.first.delete("."),
    :dv => rut.last
  }
end

.obfuscated(rut, replace_string = 'X', obfuscated_len = 2, obfuscated_dv = true) ⇒ Object

obfuscated R.U.T with string

This function returns string with RUT obfuscated



40
41
42
43
44
45
46
47
48
49
# File 'lib/chilean_rut.rb', line 40

def self.obfuscated(rut, replace_string = 'X', obfuscated_len = 2, obfuscated_dv = true)
  rut = self.get_rut_and_dv(rut)

  rut[:rut][rut[:rut].length - obfuscated_len, rut[:rut].length] = replace_string * obfuscated_len
  rut[:dv] = (obfuscated_dv) ? replace_string : rut[:dv]

  rut = self.format("#{rut[:rut]}-#{rut[:dv]}")

  return rut
end

.remove_format(rut) ⇒ Object

Strips a R.U.T. format (points & hyphens)



73
74
75
76
# File 'lib/chilean_rut.rb', line 73

def self.remove_format(rut)
  rut=rut.delete "."
  rut=rut.delete "-"
end

.remove_hyphens(rut) ⇒ Object

Strips a R.U.T. format (only hyphens)



84
85
86
# File 'lib/chilean_rut.rb', line 84

def self.remove_hyphens(rut)
  rut=rut.delete "-"
end

.remove_points(rut) ⇒ Object

Strips a R.U.T. format (only points)



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

def self.remove_points(rut)
  rut=rut.delete "."
end

.valid_dv(dv) ⇒ Object

Returns boolean wether the given string is a valid Digito Verificador character or not



7
8
9
# File 'lib/chilean_rut.rb', line 7

def self.valid_dv(dv)
  ['0','1','2','3','4','5','6','7','8','9','k','K'].include?(dv)
end

.validate(texto) ⇒ Object

Given a R.U.T. (whatever the format, i.e. with or without points & hyphens) this method returns boolean wether it is valid or not



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/chilean_rut.rb', line 90

def self.validate(texto)
  texto=self.remove_format(texto)
  return false if texto.size < 2
  texto.split("").each do |c|
    return false if !valid_dv(c)
  end
  invertido=texto.reverse
  dtexto=invertido[0]+"-"
  cnt=0
  i=1
  j=2
  for x in 1...texto.size do
    if cnt==3
      dtexto=dtexto+"."
      j+=1
      dtexto=dtexto+invertido[x]
      cnt=1
    else
      dtexto=dtexto+invertido[x]
      cnt+=1
    end
    i+=1
    j+=1
  end
  invertido=""
  i=dtexto.size-1
  j=0
  (0..i).to_a.reverse.each do |y|
    invertido=invertido+dtexto[y]
    j+=1
  end
  return validate_dv(texto)
end

.validate_dv(crut) ⇒ Object

Given a R.U.T. including its Digito Verificador (whatever the format, i.e. with or without points & hyphens)

This function returns boolean wether the Digito Verificador matches the R.U.T. or not



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chilean_rut.rb', line 55

def self.validate_dv(crut)
  return false if crut.size < 2
  if crut.size > 2
    rut=crut[0...crut.size-1]
  else
    rut=crut[0]
  end
  dv=crut[crut.size-1]
  return false if !valid_dv(dv)
  if rut.nil? || dv.nil?
    return 0 #TODO ?
  end
  dvr=get_dv(rut)
  dvr.to_s==dv.downcase
end