Module: CpfTools

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

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.format(cpf, format: :masked) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/cpf_tools.rb', line 25

def format(cpf, format: :masked)
  tax_id = cpf_to_s(cpf)
  return '' unless tax_id.length == 11

  if format == :digits_only
    tax_id
  else
    "#{tax_id[0..2]}.#{tax_id[3..5]}.#{tax_id[6..8]}-#{tax_id[9..10]}"
  end
end

.return_valid(cpf, format: :masked) ⇒ Object



36
37
38
# File 'lib/cpf_tools.rb', line 36

def return_valid(cpf, format: :masked)
  valid?(cpf) ? format(cpf, format: format) : ''
end

.valid?(cpf) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cpf_tools.rb', line 8

def valid?(cpf)
  tax_id = cpf_to_s(cpf)

  if invalid_repetition?(tax_id)
    false
  elsif tax_id.length == 11
    id_array = tax_id.split('').map(&:to_i)

    first_digit  = id_multiplier(id_array[0..8])
    second_digit = id_multiplier(id_array[0..9])

    first_digit == id_array[9] && second_digit == id_array[10]
  else
    false
  end
end