Class: Staticd::DomainGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/staticd/domain_generator.rb

Overview

Domain name generator.

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

  • length: the length of the random word

  • suffix: a suffix to append to the random world (default is none)

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

Class Method Summary collapse

Class Method Details

.generate(options = {}) ⇒ Object



35
36
37
38
39
40
# 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 = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# 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