Class: Faker::IdNumber
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
-
.brazilian_citizen_number(formatted: false) ⇒ String
(also: brazilian_cpf)
Produces a random Brazilian Citizen Number (CPF).
-
.brazilian_id(formatted: false) ⇒ String
(also: brazilian_rg)
Produces a random Brazilian ID Number (RG).
-
.chilean_id ⇒ String
Produces a random Chilean ID (Rut with 8 digits).
-
.croatian_id(international: false) ⇒ String
Produces a random Croatian ID number (OIB).
-
.danish_id_number(formatted: false, birthday: Faker::Date.birthday, gender: nil) ⇒ String
Produces a random Danish ID Number (CPR number).
-
.french_insee_number ⇒ String
Produces a random French social security number (INSEE number).
-
.invalid ⇒ String
Produces a random invalid US Social Security number.
-
.invalid_south_african_id_number ⇒ String
Produces a random invalid South African ID Number.
-
.spanish_citizen_number ⇒ String
Produces a random Spanish citizen identifier (DNI).
-
.spanish_foreign_citizen_number ⇒ String
Produces a random Spanish foreign born citizen identifier (NIE).
- .ssn_valid ⇒ Object
-
.valid ⇒ String
Produces a random valid US Social Security number.
-
.valid_south_african_id_number ⇒ String
(also: south_african_id_number)
Produces a random valid South African ID Number.
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).
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).
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_id ⇒ String
Produces a random Chilean ID (Rut with 8 digits).
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).
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.
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_number ⇒ String
Produces a random French social security number (INSEE number).
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 |
.invalid ⇒ String
Produces a random invalid US Social Security number.
44 45 46 |
# File 'lib/faker/default/id_number.rb', line 44 def invalid _translate('invalid') end |
.invalid_south_african_id_number ⇒ String
Produces a random invalid South African ID Number.
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_number ⇒ String
Produces a random Spanish citizen identifier (DNI).
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_number ⇒ String
Produces a random Spanish foreign born citizen identifier (NIE).
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_valid ⇒ Object
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 |
.valid ⇒ String
Produces a random valid US Social Security number.
31 32 33 |
# File 'lib/faker/default/id_number.rb', line 31 def valid _translate('valid') end |
.valid_south_african_id_number ⇒ String Also known as: south_african_id_number
Produces a random valid South African ID Number.
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 |