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])/
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, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale

Class Method Details

.brazilian_citizen_number(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:



163
164
165
166
167
168
169
# File 'lib/faker/default/id_number.rb', line 163

def brazilian_citizen_number(formatted: false)
  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(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:



184
185
186
187
188
189
# File 'lib/faker/default/id_number.rb', line 184

def brazilian_id(formatted: false)
  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:



202
203
204
205
206
207
# File 'lib/faker/default/id_number.rb', line 202

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

  "#{digits}-#{verification_code}"
end

.croatian_id(international: false) ⇒ String

Produces a random Croatian ID number (OIB).

Examples:

Faker::IdNumber.croatian_id #=> "88467617508"
Faker::IdNumber.croatian_id(international: true) #=> "HR88467617508"

Parameters:

  • international (Boolean) (defaults to: false)

    Specifies whether to add international prefix.

Returns:



220
221
222
223
224
225
226
# File 'lib/faker/default/id_number.rb', line 220

def croatian_id(international: false)
  prefix = international ? 'HR' : ''
  digits = Faker::Number.number(digits: 10).to_s
  checksum_digit = croatian_id_checksum_digit(digits)

  "#{prefix}#{digits}#{checksum_digit}"
end

.danish_id_number(formatted: false, birthday: Faker::Date.birthday, gender: nil) ⇒ String

Produces a random Danish ID Number (CPR number). CPR number is 10 digits. Digit 1-6 is the birthdate (format “DDMMYY”). Digit 7-10 is a sequence number. Digit 7 digit is a control digit that determines the century of birth. Digit 10 reveals the gender: # even is female, odd is male.

Examples:

Faker::IdNumber.danish_id_number #=> "0503909980"
Faker::IdNumber.danish_id_number(formatted: true) #=> "050390-9980"
Faker::IdNumber.danish_id_number(birthday: Date.new(1990, 3, 5)) #=> "0503909980"
Faker::IdNumber.danish_id_number(gender: :female) #=> "0503909980"

Parameters:

  • formatted (Boolean) (defaults to: false)

    Specifies if the number is formatted with dividers.

  • birthday (Date) (defaults to: Faker::Date.birthday)

    Specifies the birthday for the id number.

  • gender (Symbol) (defaults to: nil)

    Specifies the gender for the id number. Must be one :male or :female if present.

Returns:



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/faker/default/id_number.rb', line 247

def danish_id_number(formatted: false, birthday: Faker::Date.birthday, gender: nil)
  valid_control_digits = danish_control_digits(birthday)
  control_digit = sample(valid_control_digits)
  digits = (0..9).to_a
  gender = gender.to_sym if gender.respond_to?(:to_sym)
  gender_digit = case gender
                 when nil
                   sample(digits)
                 when :male
                   sample(digits.select(&:odd?))
                 when :female
                   sample(digits.select(&:even?))
                 else
                   raise ArgumentError, "Invalid gender #{gender}. Must be one of male, female, or be omitted."
                 end

  [
    birthday.strftime('%d%m%y'),
    formatted ? '-' : '',
    control_digit,
    Faker::Number.number(digits: 2),
    gender_digit
  ].join
end

.french_insee_numberString

Produces a random French social security number (INSEE number).

Examples:

Faker::IdNumber.french_insee_number #=> "53290236-H"

Returns:



281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/faker/default/id_number.rb', line 281

def french_insee_number
  num = [
    [1, 2].sample(random: Faker::Config.random), # gender
    Faker::Number.between(from: 0, to: 99).to_s.rjust(2, '0'), # year of birth
    Faker::Number.between(from: 1, to: 12).to_s.rjust(2, '0'), # month of birth
    Faker::Number.number(digits: 5), # place of birth
    Faker::Number.number(digits: 3) # order number
  ].join
  mod = num.to_i % 97
  check = (97 - mod).to_s.rjust(2, '0')
  "#{num}#{check}"
end

.invalidString

Produces a random invalid US Social Security number.

Examples:

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

Returns:



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:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/faker/default/id_number.rb', line 135

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:



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

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:



93
94
95
96
97
98
99
100
101
# File 'lib/faker/default/id_number.rb', line 93

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/faker/default/id_number.rb', line 48

def ssn_valid
  generate(:string) do |g|
    g.computed(name: :first) do
      range = [1..665, 667..899].sample(random: Faker::Config.random)
      n = Faker::Base.rand(range)
      format('%03d', n)
    end
    g.lit('-')
    g.computed(name: :second) do
      n = Faker::Base.rand(1..99)
      format('%02d', n)
    end
    g.lit('-')
    g.computed(name: :third) do
      n = Faker::Base.rand(1..9999)
      format('%04d', n)
    end
  end
end

.validString

Produces a random valid US Social Security number.

Examples:

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

Returns:



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:



113
114
115
116
117
118
119
120
121
122
# File 'lib/faker/default/id_number.rb', line 113

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