Class: Faker::SpanishDocuments

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

Constant Summary collapse

NIF_LETTERS =
'TRWAGMYFPDXBNJZSQVHLCKE'.freeze
NIE_LETTERS =
%w[X Y Z].freeze
CIF_LETTERS =
%w[A B C D E F G H J N P Q R S U V W].freeze
CIF_LETTERS_NUMBER =
'PQSW'.freeze
CIF_CONTROL_LETTERS =
'JABCDEFGHI'.freeze

Class Method Summary collapse

Class Method Details

.cif(letter = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/faker_spanish_documents.rb', line 27

def cif(letter=nil)
  first_letter = letter && CIF_LETTERS.include?(letter) ? letter : CIF_LETTERS.sample
  province_code = Faker::Number.number(digits: 2)
  random_number = Faker::Number.number(digits: 5)
  number = "#{province_code}#{random_number}"

  control_code = cif_control_code(number, first_letter)

  "#{first_letter}#{number}#{control_code}"
end

.dniObject



13
14
15
16
# File 'lib/faker_spanish_documents.rb', line 13

def dni
  number = Faker::Number.number(digits: 8).to_i
  "#{number}#{nif_letter(number)}"
end

.nie(letter = nil) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/faker_spanish_documents.rb', line 18

def nie(letter=nil)
  first_letter = letter && NIE_LETTERS.include?(letter) ? letter : NIE_LETTERS.sample
  first_number = NIE_LETTERS.find_index { |e| e == first_letter }
  number_part = Faker::Number.number(digits: 7)
  number_for_calculation = "#{first_number}#{number_part}".to_i

  "#{first_letter}#{number_part}#{nif_letter(number_for_calculation)}"
end