module Generators

Public Instance Methods

get_random_currency_symbol() click to toggle source
# File lib/cuculungwa/generators.rb, line 7
def get_random_currency_symbol
  currencies = %w(
    PLN EUR USD GBP CZK)
  currencies.sample
end
get_unique_email(prefix = 'email', domain = 'test.com') click to toggle source
# File lib/cuculungwa/generators.rb, line 2
def get_unique_email(prefix = 'email', domain = 'test.com')
  # returns unique test email
  "#{prefix}-#{(Time.now.to_f * 1_000_000).to_i}@#{domain}"
end
nip_generator(data_type) click to toggle source
# File lib/cuculungwa/generators.rb, line 48
def nip_generator(data_type)
  # returns polish business registration number
  if data_type.eql? 'positive_data_set'
    weights = [6, 5, 7, 2, 3, 4, 5, 6, 7]
    code = rand(998 - 101) + 101
    number = rand(1_000_000)
    nip = code.to_s + number.to_s.rjust(6, '0')

    c = nip.split(//).map!(&:to_i)
    sum = c.zip(weights).map! { |x| x[0] * x[1] }.reduce(:+) % 11

    sum = sum == 10 ? 0 : sum
    return nip + sum.to_s
  elsif data_type.eql? 'negative_data_set'
    return '000'
  else
    return ''
  end
end
pesel_generator(data_type, adult = true) click to toggle source

generators below perform typical polish data

# File lib/cuculungwa/generators.rb, line 15
def pesel_generator(data_type, adult = true)
  # returns polish registration number of the population
  if data_type.eql? 'positive_data_set'
    weights = Array[1, 3, 7, 9, 1, 3, 7, 9, 1, 3]
    current_year = Time.new.year
    temp_up = (current_year - 18).to_s[-2..-1].to_i
    temp_down = (current_year - 99).to_s[-2..-1].to_i
    if adult == true
      year = (rand * 100).to_i
      year = (rand * 100).to_i until (temp_down < year) && (year < temp_up)
    else
      year = (rand * 100).to_i
      year = (rand * 100).to_i until (temp_down > year) || (year > temp_up)
    end
    month = rand * 12 + 1
    day = rand * 28 + 1
    evidence_number = rand * 999
    sex = rand * 9
    pesel = '%02d%02d%02d%03d%d' % [year, month, day, evidence_number, sex]
    temp_cs = 0
    pesel_elem = pesel.each_char.map(&:to_i)
    for i in 0..weights.length - 1
      temp_cs += weights[i] * pesel_elem[i]
    end
    control_sum = (10 - (temp_cs % 10)) % 10
    return pesel + control_sum.to_s
  elsif data_type.eql? 'negative_data_set'
    return '00000000000'
  else
    return ''
  end
end
pl_iban_generator(data_type = 'positive_set', bank_prefix = nil, polish_format = false) click to toggle source
# File lib/cuculungwa/generators.rb, line 68
def pl_iban_generator(data_type = 'positive_set', bank_prefix = nil, polish_format = false)
  # return polish fashioned iban bank account number
  # data_type:     if you want correct iban use positive_set, anything else will return 00111122223333444455556666
  # bank_prefix:   if you want specify bank prefix (minimum 4 and maximum 8 digits)
  # polish_format: if you want add spaces for readability
  bank_prefix ||= sprintf('%08d', rand(10**8))
  bank_prefix = bank_prefix.to_s.gsub(/\s+/, '')

  raise 'Bank prefix error!' if bank_prefix.length < 4

  bank_prefix << '0001' if bank_prefix.length == 4

  if data_type.eql? 'positive_set'
    account_no = sprintf('%016d', rand(10**16))
    num = bank_prefix + account_no + '252100'
    control_digit = sprintf('%02d', 98 - (num.to_i % 97))
    number = control_digit + bank_prefix + account_no
  else
    number = '00111122223333444455556666'
  end

  if polish_format
    pl_iban_formatter(number)
  else
    number
  end
end