Module: Degenerate::Generators

Defined in:
lib/degenerate/generators.rb

Constant Summary collapse

INTEGER_MAX =
(2**(0.size * 8 -2) -1) / 2
INTEGER_MIN =
-(INTEGER_MAX)
GENERATORS =
[:integer, :string, :array]
HTTP_STATUSES =
[100, 101, 102, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226,                                                                                                                                     300, 301, 302, 303, 304, 305, 307, 308, 400, 401, 402, 403, 404,                                                                                                                                     405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417,                                                                                                                                     422, 423, 424, 426, 428, 429, 431, 500, 501, 502, 503, 504, 505,                                                                                                                                     506, 507, 508, 510, 511]

Class Method Summary collapse

Class Method Details

.anyObject



31
32
33
# File 'lib/degenerate/generators.rb', line 31

def self.any
  send(GENERATORS.sample)
end

.arrayObject



22
23
24
25
26
27
28
29
# File 'lib/degenerate/generators.rb', line 22

def self.array
  ->(opts={}) {
    limit = opts[:limit] || random_int
    of = Array(opts [:of] ||
               send([:integer, :string].sample).call(limit: 1000))
    array_of(limit, *of)
  }
end

.http_statusObject



35
36
37
# File 'lib/degenerate/generators.rb', line 35

def self.http_status
  ->() { HTTP_STATUSES.sample }
end

.integerObject



10
11
12
# File 'lib/degenerate/generators.rb', line 10

def self.integer
  ->(opts={}) { rand((opts[:min] || INTEGER_MIN)..(opts[:max] || INTEGER_MAX)) }
end

.stringObject



14
15
16
17
18
19
20
# File 'lib/degenerate/generators.rb', line 14

def self.string
  ->(opts={}) {
    chars = opts[:of] || default_chars
    limit = opts[:limit] || random_int
    random_string_of(chars, limit)
  }
end