Module: GovukSchemas::Random

Defined in:
lib/govuk_schemas/random.rb

Constant Summary collapse

WORDS =
%w[Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut suscipit at mauris non bibendum. Ut ac massa est. Aenean tempor imperdiet leo vel interdum. Nam sagittis cursus sem ultricies scelerisque. Quisque porttitor risus vel risus finibus, eu sollicitudin nisl aliquet. Sed sed lectus ac dolor molestie interdum. Nam molestie pellentesque purus ac vestibulum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse non tempor eros. Mauris eu orci hendrerit, volutpat lorem in, tristique libero. Duis a nibh nibh.].freeze

Class Method Summary collapse

Class Method Details

.anchorObject



50
51
52
# File 'lib/govuk_schemas/random.rb', line 50

def anchor
  "##{SecureRandom.hex}"
end

.base_pathObject



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

def base_path
  "/" + rand(1..5).times.map { SecureRandom.uuid }.join('/')
end

.boolObject



46
47
48
# File 'lib/govuk_schemas/random.rb', line 46

def bool
  rand(2) == 1
end

.govuk_subdomain_urlObject



33
34
35
36
37
38
# File 'lib/govuk_schemas/random.rb', line 33

def govuk_subdomain_url
  subdomain = rand(2..4).times.map {
    ('a'..'z').to_a.sample(rand(3..8)).join
  }.join('.')
  "https://#{subdomain}.gov.uk#{base_path}"
end

.random_identifier(separator:) ⇒ Object



54
55
56
# File 'lib/govuk_schemas/random.rb', line 54

def random_identifier(separator:)
  Utils.parameterize(WORDS.sample(rand(1..10)).join('-')).gsub('-', separator)
end

.string(minimum_chars = nil, maximum_chars = nil) ⇒ Object



40
41
42
43
44
# File 'lib/govuk_schemas/random.rb', line 40

def string(minimum_chars = nil, maximum_chars = nil)
  minimum_chars = minimum_chars || 0
  maximum_chars = maximum_chars || 100
  WORDS.sample(rand(minimum_chars..maximum_chars)).join(' ')
end

.string_for_regex(pattern) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/govuk_schemas/random.rb', line 58

def string_for_regex(pattern)
  case pattern.to_s
  when '^(placeholder|placeholder_.+)$'
    ['placeholder', "placeholder_#{WORDS.sample}"].sample
  when '^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$'
    SecureRandom.uuid
  when "^/(([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})+(/([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)*)?$"
    base_path
  when "^[1-9][0-9]{3}[-/](0[1-9]|1[0-2])[-/](0[1-9]|[12][0-9]|3[0-1])$"
    Date.today.iso8601
  when "^[1-9][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[0-1])$"
    Date.today.iso8601
  when "^#.+$"
    anchor
  when "[a-z-]"
    random_identifier(separator: '-')
  when "^[a-z_]+$"
    random_identifier(separator: '_')
  when "^/(([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})+(/([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)*)?(\\?([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)?(#([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)?$"
    base_path
  when "^https://([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[A-Za-z0-9])?\\.)+campaign\\.gov\\.uk(/(([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})+(/([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)*)?(\\?([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)?(#([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)?)?$"
    govuk_subdomain_url
  when "^https://([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[A-Za-z0-9])?\\.)*gov\\.uk(/(([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})+(/([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)*)?(\\?([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)?(#([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)?)?$"
    govuk_subdomain_url
  when '[a-z0-9\-_]'
    "#{SecureRandom.hex}-#{SecureRandom.hex}"
  else
    raise "      Don't know how to generate random string for pattern \#{pattern.inspect}\n\n      This propably means you've introduced a new regex in  govuk-content-schemas.\n      Because it's very hard to generate a valid string from a regex alone,\n      we have to specify a method to generate random data for each regex in\n      the schemas.\n\n      To fix this:\n\n      - Add your regex to `lib/govuk_schemas/random.rb`\n    doc\n  end\nend\n"

.string_for_type(type) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/govuk_schemas/random.rb', line 9

def string_for_type(type)
  if type == 'date-time'
    time
  elsif type == 'uri'
    uri
  else
    raise "Unknown attribute type `#{type}`"
  end
end

.timeObject



19
20
21
22
# File 'lib/govuk_schemas/random.rb', line 19

def time
  seconds_ago = rand(10_000_000) - 5_000_000
  (Time.now + seconds_ago).iso8601
end

.uriObject

TODO: make this more random with query string, optional anchor.



25
26
27
# File 'lib/govuk_schemas/random.rb', line 25

def uri
  "http://example.com#{base_path}#{anchor}"
end