chilean_rut 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/chilean_rut.rb +127 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 022c6440fefaaa336e005865a7970c4d96fcedec
|
4
|
+
data.tar.gz: d1c90f9a28a7acdc23ee34a4e03fdb88e340494f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 621d9df78166cc88081ec89ff87d0aba4d362628a5fec8000e2474e5a7827aece0509c936df6d9650a560c689bf9f6b16e59b920914eb27e1327832d44bddcd4
|
7
|
+
data.tar.gz: d24f91336346c5e48051a2ca22af53b1ab5fb682296d5a386d4098c7ce844d2b17c5d96d7d5e3543c999827172b3436543cbe0b34f82f7b5ef9f51afc271797a
|
data/lib/chilean_rut.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
##
|
2
|
+
#All functions are just defined statically in this module
|
3
|
+
class RUT
|
4
|
+
|
5
|
+
##
|
6
|
+
#Returns boolean wether the given string is a valid Digito Verificador character or not
|
7
|
+
def self.valid_dv(dv)
|
8
|
+
['0','1','2','3','4','5','6','7','8','9','k','K'].include?(dv)
|
9
|
+
end
|
10
|
+
|
11
|
+
##
|
12
|
+
#Every R.U.T. has a single valid Digito Verificador which is calculated based on its digits
|
13
|
+
#
|
14
|
+
#Given a R.U.T. this function returns its Digito Verificador as a string
|
15
|
+
def self.get_dv(rut)
|
16
|
+
suma=0
|
17
|
+
mul=2
|
18
|
+
rut.reverse.split("").each do |c|
|
19
|
+
suma=suma+c.to_i*mul
|
20
|
+
if mul==7
|
21
|
+
mul=2
|
22
|
+
else
|
23
|
+
mul+=1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
res=suma%11
|
27
|
+
if res==1
|
28
|
+
return 'k'
|
29
|
+
elsif res==0
|
30
|
+
return '0'
|
31
|
+
else
|
32
|
+
return 11-res
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
#Given a R.U.T. including its Digito Verificador (whatever the format, i.e. with or without points & hyphens)
|
38
|
+
#
|
39
|
+
#This function returns boolean wether the Digito Verificador matches the R.U.T. or not
|
40
|
+
def self.validate_dv(crut)
|
41
|
+
return false if crut.size < 2
|
42
|
+
if crut.size > 2
|
43
|
+
rut=crut[0...crut.size-1]
|
44
|
+
else
|
45
|
+
rut=crut[0]
|
46
|
+
end
|
47
|
+
dv=crut[crut.size-1]
|
48
|
+
return false if !valid_dv(dv)
|
49
|
+
if rut.nil? || dv.nil?
|
50
|
+
return 0 #TODO ?
|
51
|
+
end
|
52
|
+
dvr=get_dv(rut)
|
53
|
+
dvr.to_s==dv.downcase
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
#Strips a R.U.T. format (points & hyphens)
|
58
|
+
def self.remove_format(rut)
|
59
|
+
rut=rut.delete "."
|
60
|
+
rut=rut.delete "-"
|
61
|
+
end
|
62
|
+
|
63
|
+
#Strips a R.U.T. format (only points)
|
64
|
+
def self.remove_points(rut)
|
65
|
+
rut=rut.delete "."
|
66
|
+
end
|
67
|
+
|
68
|
+
#Strips a R.U.T. format (only hyphens)
|
69
|
+
def self.remove_hyphens(rut)
|
70
|
+
rut=rut.delete "-"
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
#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
|
75
|
+
def self.validate(texto)
|
76
|
+
texto=self.remove_format(texto)
|
77
|
+
return false if texto.size < 2
|
78
|
+
texto.split("").each do |c|
|
79
|
+
return false if !valid_dv(c)
|
80
|
+
end
|
81
|
+
invertido=texto.reverse
|
82
|
+
dtexto=invertido[0]+"-"
|
83
|
+
cnt=0
|
84
|
+
i=1
|
85
|
+
j=2
|
86
|
+
for x in 1...texto.size do
|
87
|
+
if cnt==3
|
88
|
+
dtexto=dtexto+"."
|
89
|
+
j+=1
|
90
|
+
dtexto=dtexto+invertido[x]
|
91
|
+
cnt=1
|
92
|
+
else
|
93
|
+
dtexto=dtexto+invertido[x]
|
94
|
+
cnt+=1
|
95
|
+
end
|
96
|
+
i+=1
|
97
|
+
j+=1
|
98
|
+
end
|
99
|
+
invertido=""
|
100
|
+
i=dtexto.size-1
|
101
|
+
j=0
|
102
|
+
(0..i).to_a.reverse.each do |y|
|
103
|
+
invertido=invertido+dtexto[y]
|
104
|
+
j+=1
|
105
|
+
end
|
106
|
+
return validate_dv(texto)
|
107
|
+
end
|
108
|
+
|
109
|
+
##
|
110
|
+
#This method will give a raw R.U.T. string its proper format adding the right points & hyphens
|
111
|
+
def self.format(raw_rut)
|
112
|
+
rut = raw_rut.to_s.delete '.-'
|
113
|
+
if rut.nil? || rut.empty?
|
114
|
+
return rut
|
115
|
+
end
|
116
|
+
rut_end = rut[rut.length - 1, rut.length]
|
117
|
+
rut_init_temp = rut[0, rut.length - 1]
|
118
|
+
rut_init = ''
|
119
|
+
while rut_init_temp.length > 3 do
|
120
|
+
rut_init = '.' + rut_init_temp[rut_init_temp.length - 3, rut_init_temp.length] + rut_init
|
121
|
+
rut_init_temp = rut_init_temp[0, rut_init_temp.length - 3]
|
122
|
+
end
|
123
|
+
rut = rut_init_temp+rut_init+'-'+rut_end
|
124
|
+
return rut.upcase
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chilean_rut
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.2'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gonzalo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: test-unit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.2.1
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.2.1
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: pry
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 0.10.4
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.10.4
|
47
|
+
description:
|
48
|
+
email: contacto@gonzalorios.cl
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- lib/chilean_rut.rb
|
54
|
+
homepage: https://github.com/xalupeao/chilean_rut
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
metadata: {}
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 2.6.6
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: Formato y validacion de RUT Chileno
|
78
|
+
test_files: []
|