module MiniAutobot::Utils::DataGeneratorHelper
Useful helpers to generate fake data.
Constants
- NPA
All valid area codes in the US
- NXX
Easier to assume for now a list of valid exchanges
Public Instance Methods
Generate a random date.
@param start_date [Integer] minimum date range @param end_date [Integer] maximum date range @return [String] the generated date
# File lib/mini_autobot/utils/data_generator_helper.rb, line 119 def generate_date(start_date, end_date) random_date = rand start_date..end_date return random_date.to_formatted_s(:month_day_year) end
Generate a string of random digits.
@param digits [Fixnum] the number of digits in the string @return [String] the string of digits
# File lib/mini_autobot/utils/data_generator_helper.rb, line 18 def generate_digits(digits = 1) Faker::Number.number(digits) end
Generate a random email address.
The specifier portion may be:
-
`nil`, in which case nothing special happens;
-
a `String`, in which case the words in the string is shuffled, and random separators (`.` or `_`) are inserted between them;
-
an `Integer`, in which case a random alpha-string will be created with length of at least that many characters;
-
a `Range`, in which case a random alpha-string of length within the range will be produced.
@param specifier [nil, String, Integer, Range] a specifier to help
generate the username part of the email address
@return [String]
# File lib/mini_autobot/utils/data_generator_helper.rb, line 37 def generate_email(specifier = nil) Faker::Internet.email(name) end
Generate a handsome first name.
@param length [#to_i, nil] @return [String]
# File lib/mini_autobot/utils/data_generator_helper.rb, line 45 def generate_first_name(length = nil) first_name = '' if length.nil? first_name = Faker::Name.first_name else # Ensure a name with requested length is generated name_length = Faker::Name.first_name.length if length > name_length first_name = Faker::Lorem.characters(length) else first_name = Faker::Name.first_name[0..length.to_i] end end # remove all special characters since name fields on our site have this requirement first_name.gsub!(/[^0-9A-Za-z]/, '') first_name end
Generate a gosh-darn awesome last name.
@param length [#to_i, nil] @return [String]
# File lib/mini_autobot/utils/data_generator_helper.rb, line 67 def generate_last_name(length = nil) last_name = '' if length.nil? last_name = Faker::Name.last_name else # Ensure a name with requested length is generated name_length = Faker::Name.last_name.length if length > name_length last_name = Faker::Lorem.characters(length) else last_name = Faker::Name.last_name[0..length.to_i] end end # remove all special characters since name fields on our site have this requirement last_name.gsub!(/[^0-9A-Za-z]/, '') last_name end
Generate a random number between 0 and `max - 1` if `max` is >= 1, or between 0 and 1 otherwise.
# File lib/mini_autobot/utils/data_generator_helper.rb, line 92 def generate_number(max = nil) rand(max) end
Generate a random password of a certain length, or default length 12
@param length [#to_i, nil] @return [String]
# File lib/mini_autobot/utils/data_generator_helper.rb, line 133 def generate_password(length = nil) if length.nil? SecureRandom.hex(6) # result length = 12 else chars = (('a'..'z').to_a + ('0'..'9').to_a) - %w(i o 0 1 l 0) (1..length).collect{|a| chars[rand(chars.length)] }.join end end
Generates a U.S. phone number (NANPA-aware).
@param format [Symbol, nil] the format of the phone, one of: nil,
`:paren`, `:dotted`, or `:dashed`
@return [String] the phone number
# File lib/mini_autobot/utils/data_generator_helper.rb, line 101 def generate_phone_number(format = nil) case format when :paren, :parenthesis, :parentheses '(' + NPA.sample + ') ' + NXX.sample + '-' + generate_digits(4) when :dot, :dotted, :dots, :period, :periods [ NPA.sample, NXX.sample, generate_digits(4) ].join('.') when :dash, :dashed, :dashes [ NPA.sample, NXX.sample, generate_digits(4) ].join('-') else NPA.sample + NXX.sample + generate_digits(4) end end
Generate a unique random email ends with @test.com
# File lib/mini_autobot/utils/data_generator_helper.rb, line 86 def generate_test_email [ "#{generate_last_name}.#{generate_unique_id}", 'test.com' ].join('@') end
Generate a unique id with a random hex string and time stamp string
# File lib/mini_autobot/utils/data_generator_helper.rb, line 125 def generate_unique_id SecureRandom.hex(3) + Time.current.to_i.to_s end