Module: DataFactory::Random

Included in:
BaseAPI
Defined in:
lib/data_factory/random.rb

Constant Summary collapse

CHARS =
['A'..'Z', 'a'..'z', '0'..'9'].map{|r|r.to_a}.flatten
HEX_CHARS =
['A'..'F', '0'..'9'].map{|r|r.to_a}.flatten

Instance Method Summary collapse

Instance Method Details

#random_hex_string_of_length(length) ⇒ Object

Generates a random string of hex characters



17
18
19
20
21
22
23
24
# File 'lib/data_factory/random.rb', line 17

def random_hex_string_of_length(length)
  str = ''
  # Binary length is double in ASCII
  1.upto(length*2) do
    str << HEX_CHARS[rand(HEX_CHARS.size)]
  end
  str
end

#random_hex_string_upto_length(max_length) ⇒ Object

Generates a hex string of random length, which has a maximum length of max_length



35
36
37
38
# File 'lib/data_factory/random.rb', line 35

def random_hex_string_upto_length(max_length)
  length = random_integer(max_length)
  random_hex_string_of_length(length)
end

#random_integer(max_size) ⇒ Object

Generates a random integer that is at most max_size



41
42
43
# File 'lib/data_factory/random.rb', line 41

def random_integer(max_size)
  1 + rand(max_size)
end

#random_string_of_length(length) ⇒ Object

Generates a random string of letters and numbers of length



8
9
10
11
12
13
14
# File 'lib/data_factory/random.rb', line 8

def random_string_of_length(length)
  str = ''
  1.upto(length) do
    str << CHARS[rand(CHARS.size)]
  end
  str
end

#random_string_upto_length(max_length) ⇒ Object

Generates a random string of random length, which has a maximum length of max_length



28
29
30
31
# File 'lib/data_factory/random.rb', line 28

def random_string_upto_length(max_length)
  length = random_integer(max_length)
  random_string_of_length(length)
end