module RFauxFactory

The python FauxFactory port

Constants

ALPHANUMERIC
ASCII_LETTERS
ASCII_LOWERCASE
ASCII_UPPERCASE
CJK_LETTERS_RANGE
CYRILLIC_LETTERS_RANGE
DIGITS
HTML_TAGS
IP_BLOCKS
IP_SEPARATOR
LATIN_LETTERS
LATIN_LETTERS_RANGES
MAX_INT
MIN_INT
PUNCTUATION
STRING_TYPES
UNICODE_LETTERS
UNICODE_LETTERS_RANGES
VALID_NETMASKS
VERSION

Public Class Methods

gen_alpha(length = 10) click to toggle source

Returns a random string made up of alpha characters.

# File lib/rfauxfactory.rb, line 56
def gen_alpha(length = 10)
  positive_int! length
  gen_string_from_letters length, ASCII_LETTERS
end
gen_alphanumeric(length = 10) click to toggle source

Returns a random string made up of alpha and numeric characters.

# File lib/rfauxfactory.rb, line 62
def gen_alphanumeric(length = 10)
  positive_int! length
  gen_string_from_letters length, ALPHANUMERIC
end
gen_boolean() click to toggle source

Return a random Boolean value.

# File lib/rfauxfactory.rb, line 127
def gen_boolean
  [true, false].sample
end
gen_cjk(length = 10) click to toggle source

Returns a random string made up of CJK characters.

# File lib/rfauxfactory.rb, line 68
def gen_cjk(length = 10)
  positive_int! length
  gen_string_from_cp_range length, CJK_LETTERS_RANGE
end
gen_cyrillic(length = 10) click to toggle source

Returns a random string made up of Cyrillic characters.

# File lib/rfauxfactory.rb, line 86
def gen_cyrillic(length = 10)
  positive_int! length
  gen_string_from_cp_range length, CYRILLIC_LETTERS_RANGE
end
gen_html(length = 10) click to toggle source

Returns a random string made up of html characters.

# File lib/rfauxfactory.rb, line 98
def gen_html(length = 10)
  positive_int! length
  html_tag = HTML_TAGS.sample
  "<#{html_tag}>#{gen_alpha(length)}</#{html_tag}>"
end
gen_ipaddr(protocol: :ip4, prefix: []) click to toggle source

Generates a random IP address.

# File lib/rfauxfactory.rb, line 132
def gen_ipaddr(protocol: :ip4, prefix: [])
  raise ArgumentError, "#{protocol} is not valid protocol" unless IP_BLOCKS.key?(protocol)
  sections = IP_BLOCKS[protocol]
  prefix.map(&:to_s).compact
  sections -= prefix.length
  raise ArgumentError, "Prefix #{prefix} is too long for this configuration" if sections <= 0
  random_fields = if protocol == :ipv6
                    Array.new(sections) { rand(0..2**16 - 1).to_s(16) }
                  else
                    Array.new(sections) { rand(0..255) }
                  end
  ipaddr = (prefix + random_fields).join(IP_SEPARATOR[protocol])
  ipaddr += '.0' if protocol == :ip3
  ipaddr
end
gen_latin1(length = 10) click to toggle source

Returns a random string made up of UTF-8 characters. (Font: Wikipedia - Latin-1 Supplement Unicode Block)

# File lib/rfauxfactory.rb, line 80
def gen_latin1(length = 10)
  positive_int! length
  gen_string_from_letters length, LATIN_LETTERS
end
gen_mac(delimiter: ':', multicast: nil, locally: nil) click to toggle source

Generates a random MAC address.

# File lib/rfauxfactory.rb, line 149
def gen_mac(delimiter: ':', multicast: nil, locally: nil)
  raise ArgumentError, "#{delimiter} is not valid delimiter" unless %w[: -].include?(delimiter)
  multicast = gen_boolean if multicast.nil?
  locally = gen_boolean if locally.nil?
  first_octet = rand(0..255)
  multicast ? first_octet |= 0b00000001 : first_octet &= 0b11111110
  locally ? first_octet |= 0b00000010 : first_octet &= 0b11111101
  octets = [first_octet]
  octets += (Array.new(5) { rand(0..255) })
  octets.map { |octet| format('%02x', octet) }.join(delimiter)
end
gen_netmask(min_cidr: 1, max_cidr: 31) click to toggle source

Generates a random netmask.

# File lib/rfauxfactory.rb, line 162
def gen_netmask(min_cidr: 1, max_cidr: 31)
  raise ArgumentError, "min_cidr must be 0 or greater, but is #{min_cidr}" if min_cidr < 0
  raise ArgumentError, "max_cidr must be 0 or greater, but is #{max_cidr}" if max_cidr >= VALID_NETMASKS.length
  VALID_NETMASKS[rand(min_cidr..max_cidr)]
end
gen_numeric_string(length = 10) click to toggle source

Returns a random string made up of numbers.

# File lib/rfauxfactory.rb, line 92
def gen_numeric_string(length = 10)
  positive_int! length
  gen_string_from_letters length, DIGITS
end
gen_special(length = 10) click to toggle source
# File lib/rfauxfactory.rb, line 104
def gen_special(length = 10)
  positive_int! length
  gen_string_from_letters length, PUNCTUATION
end
gen_string(str_type, length = 10) click to toggle source

A simple wrapper that calls other string generation methods.

# File lib/rfauxfactory.rb, line 110
def gen_string(str_type, length = 10)
  raise ArgumentError, "str_type: #{str_type} not supported" unless RFauxFactory::STRING_TYPES.key?(str_type)
  send(RFauxFactory::STRING_TYPES[str_type], length)
end
gen_strings(length = 10, exclude: []) click to toggle source

Generates a list of different input strings.

# File lib/rfauxfactory.rb, line 116
def gen_strings(length = 10, exclude: [])
  positive_int_or_range! length
  raise TypeError, 'exclude must be an Array' unless exclude.is_a?(Array)
  str_types = RFauxFactory::STRING_TYPES.keys.reject { |str_type| exclude.include?(str_type) }
  str_types.map do |str_type|
    str_length = length.is_a?(Range) ? rand(length) : length
    [str_type, gen_string(str_type, str_length)]
  end.to_h
end
gen_utf8(length = 10) click to toggle source
# File lib/rfauxfactory.rb, line 73
def gen_utf8(length = 10)
  positive_int! length
  gen_string_from_letters length, UNICODE_LETTERS
end

Private Class Methods

gen_string_from_cp_range(length, max_or_range) click to toggle source
# File lib/rfauxfactory.rb, line 30
def gen_string_from_cp_range(length, max_or_range)
  (1..length).map do
    char_ascii = rand(max_or_range)
    char_ascii.chr(Encoding::UTF_8)
  end.join
end
gen_string_from_letters(length, letters) click to toggle source
# File lib/rfauxfactory.rb, line 23
def gen_string_from_letters(length, letters)
  max_index = letters.length - 1
  (1..length).map do
    letters[rand(max_index)].chr
  end.join
end
positive_int!(length, name: 'length') click to toggle source
# File lib/rfauxfactory.rb, line 37
def positive_int!(length, name: 'length')
  raise TypeError, "#{name} is not of type Integer" unless length.is_a?(Integer)
  raise ArgumentError, "#{name} must be a positive integer" unless length > 0
end
positive_int_or_range!(length) click to toggle source
# File lib/rfauxfactory.rb, line 42
def positive_int_or_range!(length)
  raise TypeError, 'length must be Integer or Range' unless length.is_a?(Integer) || length.is_a?(Range)
  if length.is_a?(Integer)
    positive_int! length
  else
    raise ArgumentError, 'Bad length range' if length.size.nil? || length.size.zero?
    positive_int! length.first, name: 'length.first'
    positive_int! length.last, name: 'length.last'
  end
end