class Staticd::DomainGenerator

Domain name generator.

This class can be used to generate random words of various length. Options:

Example:

DomainGenerator.new(length: 2, suffix: ".domain.tld")
# => rb.domain.tld

A block can be used to validate the generated domain. It must return true to validate the domain, otherwise a new one is proposed. This feature can be used to validate the domain against certain rules.

Example:

DomainGenerator.new(suffix: ".domain.tld") do |generated_domain|
  ["admin", "www"].include?(generated_domain)
end

Public Class Methods

generate(options={}) click to toggle source
# File lib/staticd/domain_generator.rb, line 35
def self.generate(options={})
  length = options[:length] || 6
  suffix = options[:suffix] || ""
  random = ("a".."z").to_a.shuffle[0, length].join
  random + suffix
end
new(options={}) { |domain| ... } click to toggle source
# File lib/staticd/domain_generator.rb, line 24
def self.new(options={})
  if block_given?
    until domain = generate(options)
      yield domain
    end
    domain
  else
    generate(options)
  end
end