module RandomDataDespegar module IdGenerator # cr ec pa pe ve mx co cl ar br def id_for(site) site ||= "generic" self.send("generate_id_for_#{site.downcase}") end # Valido 20314562222 def argentinean_cuil(gender = "m", dni = "31456222") generate_cuil(gender, dni) end private def generate_id_for_ar generate_dni end def generate_id_for_br cpf end def generate_id_for_cl generate_rut end def generate_id_for_generic Random.number_with_size(8) end # CPF valido 625.664.266-00 # def cpf n = "" until check_cpf(n) return n = 11.times.map{rand(0..9)}.join.to_s end end # CHILEAN RUT # valido DEPRECATED def generate_rut generate_chilean_rut end # Valido 5.954.720-8 59547208 def rut generate_chilean_rut end def check_cpf(cpf=nil) return false if cpf.nil? winvalidos = %w{12345678909 11111111111 22222222222 33333333333 44444444444 55555555555 66666666666 77777777777 88888888888 99999999999 00000000000} wvalor = cpf.scan /[0-9]/ if wvalor.length == 11 unless winvalidos.member?(wvalor.join) wvalor = wvalor.collect{|x| x.to_i} wsoma = 10*wvalor[0]+9*wvalor[1]+8*wvalor[2]+7*wvalor[3]+6*wvalor[4]+5*wvalor[5]+4*wvalor[6]+3*wvalor[7]+2*wvalor[8] wsoma = wsoma - (11 * (wsoma/11)) wresult1 = (wsoma == 0 or wsoma == 1) ? 0 : 11 - wsoma if wresult1 == wvalor[9] wsoma = wvalor[0]*11+wvalor[1]*10+wvalor[2]*9+wvalor[3]*8+wvalor[4]*7+wvalor[5]*6+wvalor[6]*5+wvalor[7]*4+wvalor[8]*3+wvalor[9]*2 wsoma = wsoma - (11 * (wsoma/11)) wresult2 = (wsoma == 0 or wsoma == 1) ? 0 : 11 - wsoma return true if wresult2 == wvalor[10] # CPF validado end end end return false # CPF invalidado end def generate_chilean_rut run = (rand * 20000000).round + 5000000 suma = 0 mul = 2 run.to_s.reverse.split('').each do |i| suma = suma + i.to_i * mul if mul == 7 mul = 2 else mul += 1 end end res = suma % 11 if res == 1 return "#{run}k" elsif res == 0 return "#{run}0" else return "#{run}#{11-res}" end end # TO DO - Ver de hacer un generador real # DNI Valido 31222333 # def generate_dni "#{rand(10..38)}#{rand(100..800)}#{rand(100..800)}" end # CUIL vĂ¡lido 20311412095 def generate_cuil(sexo, nro_dni) dniStc = nro_dni.to_s.rjust 8, "0" xyStc = 30 xyStc = 27 if sexo == "f" xyStc = 20 if sexo == "m" cDocum = xyStc.to_s + dniStc matriz = [5,4,3,2,7,6,5,4,3,2] xx = 0 10.times do |t| xx += cDocum[t].chr.to_i * matriz[t] end n = 11 - xx % 11 if n == 10 if (xyStc == 20 or xyStc == 27 or xyStc == 24) xyStc, digitoStc = 23, 9 else xyStc, digitoStc = 33, 9 end digitoStc = 4 if xyStc == 27 else digitoStc = (n == 11) ? 0 : n end return xyStc.to_s + dniStc + digitoStc.to_s end alias_method :generate_id_for_cr, :generate_id_for_generic alias_method :generate_id_for_ec, :generate_id_for_generic alias_method :generate_id_for_pa, :generate_id_for_generic alias_method :generate_id_for_pe, :generate_id_for_generic alias_method :generate_id_for_ve, :generate_id_for_generic alias_method :generate_id_for_mx, :generate_id_for_generic alias_method :generate_id_for_co, :generate_id_for_generic end end