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
BRAZILIAN_ID_FORMAT =
/(\d{1,2})(\d{3})(\d{3})([\dX])/.freeze
BRAZILIAN_ID_FROM =
10_000_000
BRAZILIAN_ID_TO =
99_999_999
CHILEAN_MODULO =
11

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, 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(legacy_formatted = NOT_GIVEN, formatted: false) ⇒ String Also known as: brazilian_cpf

Produces a random Brazilian Citizen Number (CPF).

Examples:

Faker::IDNumber.brazilian_citizen_number #=> "53540542221"
Faker::IDNumber.brazilian_citizen_number(formatted: true) #=> "535.405.422-21"

Parameters:

  • formatted (Boolean) (defaults to: false)

    Specifies if the number is formatted with dividers.

Returns:

  • (String)

Available since:

  • 1.9.2


149
150
151
152
153
154
155
156
157
158
159
# File 'lib/faker/default/id_number.rb', line 149

def brazilian_citizen_number(legacy_formatted = NOT_GIVEN, formatted: false)
  warn_for_deprecated_arguments do |keywords|
    keywords << :formatted if legacy_formatted != NOT_GIVEN
  end

  digits = Faker::Number.leading_zero_number(digits: 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

.brazilian_id(legacy_formatted = NOT_GIVEN, formatted: false) ⇒ String Also known as: brazilian_rg

Produces a random Brazilian ID Number (RG).

Examples:

Faker::IDNumber.brazilian_id #=> "493054029"
Faker::IDNumber.brazilian_id(formatted: true) #=> "49.305.402-9"

Parameters:

  • formatted (Boolean) (defaults to: false)

    Specifies if the number is formatted with dividers.

Returns:

  • (String)

Available since:

  • 2.1.2


174
175
176
177
178
179
180
181
182
183
# File 'lib/faker/default/id_number.rb', line 174

def brazilian_id(legacy_formatted = NOT_GIVEN, formatted: false)
  warn_for_deprecated_arguments do |keywords|
    keywords << :formatted if legacy_formatted != NOT_GIVEN
  end

  digits = Faker::Number.between(to: BRAZILIAN_ID_FROM, from: BRAZILIAN_ID_TO).to_s
  check_digit = brazilian_id_checksum_digit(digits)
  number = [digits, check_digit].join
  formatted ? format('%s.%s.%s-%s', *number.scan(BRAZILIAN_ID_FORMAT).flatten) : number
end

.chilean_idString

Produces a random Chilean ID (Rut with 8 digits).

Examples:

Faker::IDNumber.chilean_id #=> "15620613-K"

Returns:

  • (String)

Available since:

  • 2.1.2


196
197
198
199
200
201
# File 'lib/faker/default/id_number.rb', line 196

def chilean_id
  digits = Faker::Number.number(digits: 8)
  verification_code = chilean_verification_code(digits)

  digits.to_s + '-' + verification_code.to_s
end

.invalidString

Produces a random invalid US Social Security number.

Examples:

Faker::IDNumber.invalid #=> "311-72-0000"

Returns:

  • (String)

Available since:

  • 1.6.0


44
45
46
# File 'lib/faker/default/id_number.rb', line 44

def invalid
  _translate('invalid')
end

.invalid_south_african_id_numberString

Produces a random invalid South African ID Number.

Examples:

Faker::IDNumber.invalid_south_african_id_number #=> "1642972065088"

Returns:

  • (String)

Available since:

  • 1.9.2


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/faker/default/id_number.rb', line 121

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

  id_number = [
    invalid_date_of_birth,
    Faker::Number.number(digits: 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_numberString

Produces a random Spanish citizen identifier (DNI).

Examples:

Faker::IDNumber.spanish_citizen_number #=> "53290236-H"

Returns:

  • (String)

Available since:

  • 1.9.0


63
64
65
66
67
68
# File 'lib/faker/default/id_number.rb', line 63

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

.spanish_foreign_citizen_numberString

Produces a random Spanish foreign born citizen identifier (NIE).

Examples:

Faker::IDNumber.spanish_foreign_citizen_number #=> "Z-1600870-Y"

Returns:

  • (String)

Available since:

  • 1.9.0


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

def spanish_foreign_citizen_number
  code = 'XYZ'
  digits = Faker::Number.number(digits: 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


48
49
50
51
52
# File 'lib/faker/default/id_number.rb', line 48

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

.validString

Produces a random valid US Social Security number.

Examples:

Faker::IDNumber.valid #=> "552-56-3593"

Returns:

  • (String)

Available since:

  • 1.6.0


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

def valid
  _translate('valid')
end

.valid_south_african_id_numberString Also known as: south_african_id_number

Produces a random valid South African ID Number.

Examples:

Faker::IDNumber.south_african_id_number #=> "8105128870184"
Faker::IDNumber.valid_south_african_id_number #=> "8105128870184"

Returns:

  • (String)

Available since:

  • 1.9.2


99
100
101
102
103
104
105
106
107
108
# File 'lib/faker/default/id_number.rb', line 99

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

  [id_number, south_african_id_checksum_digit(id_number)].join
end