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, generate, 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



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

def asin
  fetch('code.asin')
end

.ean(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



52
53
54
55
56
57
58
# File 'lib/faker/default/code.rb', line 52

def ean(base: 13)
  case base
  when 8 then generate_base8_ean
  when 13 then generate_base13_ean
  else raise ArgumentError, 'base must be 8 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



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

def imei
  generate_imei
end

.isbn(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
# File 'lib/faker/default/code.rb', line 32

def isbn(base: 10)
  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 Identifier) 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(min_age: 18, max_age: 65) ⇒ String

Produces a random NRIC (National Registry Identity Card) code. By default generates a Singaporean NRIC ID for someone who is born between the age of 18 and 65.

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/faker/default/code.rb', line 94

def nric(min_age: 18, max_age: 65)
  birthyear = Date.birthday(min_age: min_age, max_age: max_age).year

  generate(:string) do |g|
    g.computed(name: :prefix) do
      if birthyear < 2000
        'S'
      else
        'T'
      end
    end
    g.computed(name: :yy) do
      birthyear.to_s[-2..]
    end
    g.int(name: :values, length: 5)
    g.computed(name: :check, deps: i[prefix yy values]) do |prefix, yy, values|
      generate_nric_check_alphabet("#{yy}#{values}", prefix)
    end
  end
end

.rutString

Produces a random RUT (Rol Unico Nacional) code.

Examples:

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

Returns:

  • (String)

Available since:

  • 1.9.4



69
70
71
72
73
# File 'lib/faker/default/code.rb', line 69

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



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

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