module Chilean::Rutify

Chilean rut utils module

Constants

VERSION

Public Class Methods

format_rut(raw_rut) click to toggle source

returns the rut value with the chilean format

# File lib/chilean/rutify.rb, line 61
def format_rut(raw_rut)
  rut = normalize_rut(raw_rut)
  return unless rut.present? && valid_rut_values?(rut)

  verifier = rut[-1]
  temp_rut = rut[0..-2]
  init_rut = ""

  while temp_rut.length > 3
    init_rut = "." + temp_rut[(temp_rut.size - 3)..temp_rut.size] + init_rut
    temp_rut = temp_rut[0..-4]
  end

  rut = temp_rut + init_rut + "-" + verifier
  rut.upcase
end
get_verifier(raw_rut) click to toggle source

returns the rut verifier value

# File lib/chilean/rutify.rb, line 17
def get_verifier(raw_rut)
  rut = stringify_rut(raw_rut)
  return unless rut.present? && valid_rut_values?(rut)

  sum = 0
  mul = 2

  rut.reverse.split("").each do |i|
    sum += i.to_i * mul
    mul = mul == 7 ? 2 : mul + 1
  end

  res = sum % 11

  case res
  when 1
    "K"
  when 0
    "0"
  else
    (11 - res).to_s
  end
end
normalize_rut(raw_rut) click to toggle source

returns the rut value without any sepecial caracter and upcased

# File lib/chilean/rutify.rb, line 51
def normalize_rut(raw_rut)
  rut = stringify_rut(raw_rut)
  return unless rut.present? && valid_rut_values?(rut)

  rut = rut.delete "."
  rut = rut.delete "-"
  rut.upcase
end
stringify_rut(raw_rut) click to toggle source

checks rut minimum requirements and transform value to string if posible

# File lib/chilean/rutify.rb, line 99
def stringify_rut(raw_rut)
  return if raw_rut.nil? || (!raw_rut.is_a?(String) && !raw_rut.is_a?(Integer))

  raw_rut.to_s
end
valid_rut?(raw_rut) click to toggle source

checks if the passed rut is valid

# File lib/chilean/rutify.rb, line 79
def valid_rut?(raw_rut)
  rut = normalize_rut(raw_rut)
  return false unless rut.present? && valid_rut_values?(rut)

  valid_rut_verifier?(rut)
end
valid_rut_value?(rv) click to toggle source

checks if the passed value is valid as a rut character

# File lib/chilean/rutify.rb, line 12
def valid_rut_value?(rv)
  ['0','1','2','3','4','5','6','7','8','9','k','K','.','-'].include?(rv.to_s)
end
valid_rut_values?(raw_rut) click to toggle source

checks if all the rut values are valid

# File lib/chilean/rutify.rb, line 87
def valid_rut_values?(raw_rut)
  rut = stringify_rut(raw_rut)
  return false unless rut.present?

  rut.split("").each do |i|
    return false unless valid_rut_value?(i)
  end

  true
end
valid_rut_verifier?(raw_rut) click to toggle source

checks if the passed rut has a proper verifier value

# File lib/chilean/rutify.rb, line 42
def valid_rut_verifier?(raw_rut)
  rut = normalize_rut(raw_rut)
  return false unless rut.present? && valid_rut_values?(rut)

  r = rut[0..(rut.size - 2)]
  get_verifier(r) == rut[-1]
end

Private Instance Methods

format_rut(raw_rut) click to toggle source

returns the rut value with the chilean format

# File lib/chilean/rutify.rb, line 61
def format_rut(raw_rut)
  rut = normalize_rut(raw_rut)
  return unless rut.present? && valid_rut_values?(rut)

  verifier = rut[-1]
  temp_rut = rut[0..-2]
  init_rut = ""

  while temp_rut.length > 3
    init_rut = "." + temp_rut[(temp_rut.size - 3)..temp_rut.size] + init_rut
    temp_rut = temp_rut[0..-4]
  end

  rut = temp_rut + init_rut + "-" + verifier
  rut.upcase
end
get_verifier(raw_rut) click to toggle source

returns the rut verifier value

# File lib/chilean/rutify.rb, line 17
def get_verifier(raw_rut)
  rut = stringify_rut(raw_rut)
  return unless rut.present? && valid_rut_values?(rut)

  sum = 0
  mul = 2

  rut.reverse.split("").each do |i|
    sum += i.to_i * mul
    mul = mul == 7 ? 2 : mul + 1
  end

  res = sum % 11

  case res
  when 1
    "K"
  when 0
    "0"
  else
    (11 - res).to_s
  end
end
normalize_rut(raw_rut) click to toggle source

returns the rut value without any sepecial caracter and upcased

# File lib/chilean/rutify.rb, line 51
def normalize_rut(raw_rut)
  rut = stringify_rut(raw_rut)
  return unless rut.present? && valid_rut_values?(rut)

  rut = rut.delete "."
  rut = rut.delete "-"
  rut.upcase
end
stringify_rut(raw_rut) click to toggle source

checks rut minimum requirements and transform value to string if posible

# File lib/chilean/rutify.rb, line 99
def stringify_rut(raw_rut)
  return if raw_rut.nil? || (!raw_rut.is_a?(String) && !raw_rut.is_a?(Integer))

  raw_rut.to_s
end
valid_rut?(raw_rut) click to toggle source

checks if the passed rut is valid

# File lib/chilean/rutify.rb, line 79
def valid_rut?(raw_rut)
  rut = normalize_rut(raw_rut)
  return false unless rut.present? && valid_rut_values?(rut)

  valid_rut_verifier?(rut)
end
valid_rut_value?(rv) click to toggle source

checks if the passed value is valid as a rut character

# File lib/chilean/rutify.rb, line 12
def valid_rut_value?(rv)
  ['0','1','2','3','4','5','6','7','8','9','k','K','.','-'].include?(rv.to_s)
end
valid_rut_values?(raw_rut) click to toggle source

checks if all the rut values are valid

# File lib/chilean/rutify.rb, line 87
def valid_rut_values?(raw_rut)
  rut = stringify_rut(raw_rut)
  return false unless rut.present?

  rut.split("").each do |i|
    return false unless valid_rut_value?(i)
  end

  true
end
valid_rut_verifier?(raw_rut) click to toggle source

checks if the passed rut has a proper verifier value

# File lib/chilean/rutify.rb, line 42
def valid_rut_verifier?(raw_rut)
  rut = normalize_rut(raw_rut)
  return false unless rut.present? && valid_rut_values?(rut)

  r = rut[0..(rut.size - 2)]
  get_verifier(r) == rut[-1]
end