class GovukSchemas::RandomContentGenerator
@private
Constants
- WORDS
Public Class Methods
new(random: Random.new)
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 6 def initialize(random: Random.new) @random = random end
Public Instance Methods
anchor()
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 51 def anchor "##{hex}" end
base_path()
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 30 def base_path "/" + @random.rand(1..5).times.map { uuid }.join("/") end
bool()
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 47 def bool @random.rand(2) == 1 end
govuk_subdomain_url()
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 34 def govuk_subdomain_url subdomain = @random.rand(2..4).times.map { ("a".."z").to_a.sample(@random.rand(3..8), random: @random).join }.join(".") "https://#{subdomain}.gov.uk#{base_path}" end
hex(length = 10)
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 68 def hex(length = 10) length.times.map { bool ? random_letter : random_number }.join("") end
random_identifier(separator:)
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 55 def random_identifier(separator:) WORDS.sample(@random.rand(1..10), random: @random) .join("-") .gsub(/[^a-z0-9\-_]+/i, "-") .gsub("-", separator) end
string(minimum_chars = nil, maximum_chars = nil)
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 41 def string(minimum_chars = nil, maximum_chars = nil) minimum_chars ||= 0 maximum_chars ||= 100 WORDS.sample(@random.rand(minimum_chars..maximum_chars), random: @random).join(" ") end
string_for_regex(pattern)
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 72 def string_for_regex(pattern) case pattern.to_s when "^(placeholder|placeholder_.+)$" ["placeholder", "placeholder_#{WORDS.sample(random: @random)}"].sample(random: @random) when "^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$" 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 "^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$" Time.now.strftime("%H:%m") 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\-_]' "#{hex}-#{hex}" else raise <<-DOC Don't know how to generate random string for pattern #{pattern.inspect} This propably means you've introduced a new regex in govuk-content-schemas. Because it's very hard to generate a valid string from a regex alone, we have to specify a method to generate random data for each regex in the schemas. To fix this: - Add your regex to `lib/govuk_schemas/random.rb` DOC end end
string_for_type(type)
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 10 def string_for_type(type) if type == "date-time" time elsif type == "uri" uri else raise "Unknown attribute type `#{type}`" end end
time()
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 20 def time arbitrary_time = Time.new(2012, 2, 1) (arbitrary_time + @random.rand(0..500_000_000)).iso8601 end
uri()
click to toggle source
TODO: make this more random with query string, optional anchor.
# File lib/govuk_schemas/random_content_generator.rb, line 26 def uri "http://example.com#{base_path}#{anchor}" end
uuid()
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 62 def uuid # matches uuid regex e.g. e058aad7-ce86-5181-8801-4ddcb3c8f27c # /^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/ "#{hex(8)}-#{hex(4)}-1#{hex(3)}-a#{hex(3)}-#{hex(12)}" end
Private Instance Methods
random_letter()
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 118 def random_letter letters = ("a".."f").to_a letters[@random.rand(0..letters.count - 1)] end
random_number()
click to toggle source
# File lib/govuk_schemas/random_content_generator.rb, line 123 def random_number numbers = ("0".."9").to_a numbers[@random.rand(0..numbers.count - 1)] end