Class: Faker::Code

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

Constant Summary

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

.asinString

Retrieves a real Amazon ASIN code from archive.org/details/asin_listing

Examples:

Faker::Code.asin #=> "B000MZW1GE"

Returns:

  • (String)

Available since:

  • 1.9.4


138
139
140
# File 'lib/faker/default/code.rb', line 138

def asin
  fetch('code.asin')
end

.ean(legacy_base = NOT_GIVEN, base: 13) ⇒ String

Produces a random EAN (European Article Number) code.

Examples:

Faker::Code.ean(base: 8) #=> "36941070"
Faker::Code.ean #=> "9941880131907"

Parameters:

  • base (Integer) (defaults to: 13)

    the length of the code to generate (either 8 or 13)

Returns:

  • (String)

Available since:

  • 2.2.0


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/faker/default/code.rb', line 56

def ean(legacy_base = NOT_GIVEN, base: 13)
  warn_for_deprecated_arguments do |keywords|
    keywords << :base if legacy_base != NOT_GIVEN
  end

  case base
  when 8 then generate_base8_ean
  when 13 then generate_base13_ean
  else raise ArgumentError, 'base must be 3 or 13'
  end
end

.imeiString

Produces a random IMEI (International Mobile Equipment Number) code.

Examples:

Faker::Code.imei #=> "492033129092256"

Returns:

  • (String)

Available since:

  • 1.9.4


125
126
127
# File 'lib/faker/default/code.rb', line 125

def imei
  generate_imei
end

.isbn(legacy_base = NOT_GIVEN, base: 10) ⇒ String

Produces a random ISBN (International Standard Book Number) code.

Examples:

Faker::Code.isbn(base: 13) #=> "896579606969-8"
Faker::Code.isbn #=> "170366802-2"

Parameters:

  • base (Integer) (defaults to: 10)

    the length of the code to generate (either 10 or 13)

Returns:

  • (String)

Available since:

  • 2.2.0


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/faker/default/code.rb', line 32

def isbn(legacy_base = NOT_GIVEN, base: 10)
  warn_for_deprecated_arguments do |keywords|
    keywords << :base if legacy_base != NOT_GIVEN
  end

  case base
  when 10 then generate_base10_isbn
  when 13 then generate_base13_isbn
  else raise ArgumentError, 'base must be 10 or 13'
  end
end

.npiString

Produces a random NPI (National Provider Identifer) code.

Examples:

Faker::Code.npi #=> "9804062802"

Returns:

  • (String)

Available since:

  • 1.9.4


16
17
18
# File 'lib/faker/default/code.rb', line 16

def npi
  rand(10**10).to_s.rjust(10, '0')
end

.nric(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18, max_age: 65) ⇒ String

By default generates a Singaporean NRIC ID for someone who is born between the age of 18 and 65.

Produces a random NRIC (National Registry Identity Card) code.

Examples:

Faker::Code.nric(min_age: 25, max_age: 50) #=> "S9347283G"
Faker::Code.nric(max_age: 55) #=> "S7876903C"
Faker::Code.nric(min_age: 25) #=> "S6281697Z"
Faker::Code.nric #=> "S6372958B"

Parameters:

  • min_age (Integer) (defaults to: 18)

    the min age of the person in years

  • max_age (Integer) (defaults to: 65)

    the max age of the person in years

Returns:

  • (String)

Available since:

  • 2.2.0


102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/faker/default/code.rb', line 102

def nric(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18, max_age: 65)
  warn_for_deprecated_arguments do |keywords|
    keywords << :min_age if legacy_min_age != NOT_GIVEN
    keywords << :max_age if legacy_max_age != NOT_GIVEN
  end

  birthyear = Date.birthday(min_age: min_age, max_age: max_age).year
  prefix = birthyear < 2000 ? 'S' : 'T'
  values = birthyear.to_s[-2..-1]
  values << regexify(/\d{5}/)
  check_alpha = generate_nric_check_alphabet(values, prefix)
  "#{prefix}#{values}#{check_alpha}"
end

.rutString

Produces a random RUT (Rol Unico Nacional) code.

Examples:

Faker::Code.rut #=> "91611842-2"

Returns:

  • (String)

Available since:

  • 1.9.4


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

def rut
  value = Number.number(digits: 8).to_s
  vd = rut_verificator_digit(value)
  value << "-#{vd}"
end

.sinString

Produces a random SIN (Social Insurance Number for Canada) code.

Examples:

Faker::Code.sin #=> "996586962"

Returns:

  • (String)

Available since:

  • 1.9.4


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/faker/default/code.rb', line 151

def sin
  # 1   - province or temporary resident
  # 2-8 - random numbers
  # 9   - checksum

  # 1st digit. 8,0 are not used
  registry = Faker::Base.sample([1, 2, 3, 4, 5, 6, 7, 9]).to_s

  # generate 2nd to 8th
  partial = Array.new(7) { Faker::Config.random.rand(0..9) }.join

  # Generate 9th digit
  check_digit = generate_sin_check_digit(registry + partial + '0').to_s

  registry + partial + check_digit
end