Class: Faker::IDNumber

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/id_number.rb

Constant Summary collapse

CHECKS =
'TRWAGMYFPDXBNJZSQVHLCKE'
INVALID_SSN =
[
  /0{3}-\d{2}-\d{4}/,
  /\d{3}-0{2}-\d{4}/,
  /\d{3}-\d{2}-0{4}/,
  /666-\d{2}-\d{4}/,
  /9\d{2}-\d{2}-\d{4}/
].freeze
ZA_RACE_DIGIT =
'8'
ZA_CITIZENSHIP_DIGITS =
%w[0 1].freeze

Constants inherited from Base

Base::Letters, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.brazilian_citizen_number(formatted: false) ⇒ Object


78
79
80
81
82
83
84
# File 'lib/faker/default/id_number.rb', line 78

def brazilian_citizen_number(formatted: false)
  digits = Faker::Number.leading_zero_number(9) until digits&.match(/(\d)((?!\1)\d)+/)
  first_digit = brazilian_citizen_number_checksum_digit(digits)
  second_digit = brazilian_citizen_number_checksum_digit(digits + first_digit)
  number = [digits, first_digit, second_digit].join
  formatted ? format('%s.%s.%s-%s', *number.scan(/\d{2,3}/).flatten) : number
end

.invalidObject


21
22
23
# File 'lib/faker/default/id_number.rb', line 21

def invalid
  _translate('invalid')
end

.invalid_south_african_id_numberObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/faker/default/id_number.rb', line 61

def invalid_south_african_id_number
  invalid_date_of_birth = [
    Faker::Number.number(2),
    Faker::Number.between(13, 99),
    Faker::Number.between(32, 99)
  ].map(&:to_s).join

  id_number = [
    invalid_date_of_birth,
    Faker::Number.number(4),
    ZA_CITIZENSHIP_DIGITS.sample(random: Faker::Config.random),
    ZA_RACE_DIGIT
  ].join

  [id_number, south_african_id_checksum_digit(id_number)].join
end

.spanish_citizen_numberObject


31
32
33
34
35
36
# File 'lib/faker/default/id_number.rb', line 31

def spanish_citizen_number
  num = Faker::Number.number(8)
  mod = num.to_i % 23
  check = CHECKS[mod]
  "#{num}-#{check}"
end

.spanish_foreign_citizen_numberObject


38
39
40
41
42
43
44
45
46
# File 'lib/faker/default/id_number.rb', line 38

def spanish_foreign_citizen_number
  code = 'XYZ'
  digits = Faker::Number.number(7)
  prefix = code[rand(code.length)]
  prefix_val = 'XYZ'.index(prefix).to_s
  mod = "#{prefix_val}#{digits}".to_i % 23
  check = CHECKS[mod]
  "#{prefix}-#{digits}-#{check}"
end

.ssn_validObject


25
26
27
28
29
# File 'lib/faker/default/id_number.rb', line 25

def ssn_valid
  ssn = regexify(/[0-8]\d{2}-\d{2}-\d{4}/)
  # We could still have all 0s in one segment or another
  INVALID_SSN.any? { |regex| regex =~ ssn } ? ssn_valid : ssn
end

.validObject


17
18
19
# File 'lib/faker/default/id_number.rb', line 17

def valid
  _translate('valid')
end

.valid_south_african_id_numberObject Also known as: south_african_id_number


48
49
50
51
52
53
54
55
56
57
# File 'lib/faker/default/id_number.rb', line 48

def valid_south_african_id_number
  id_number = [
    Faker::Date.birthday.strftime('%y%m%d'),
    Faker::Number.number(4),
    ZA_CITIZENSHIP_DIGITS.sample(random: Faker::Config.random),
    ZA_RACE_DIGIT
  ].join

  [id_number, south_african_id_checksum_digit(id_number)].join
end