Class: Studio::UsernameGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/studio/username_generator.rb

Class Method Summary collapse

Class Method Details

.build_nameObject



11
12
13
14
15
# File 'lib/studio/username_generator.rb', line 11

def self.build_name
  plant = Faker::Food.send(%i[vegetables fruits].sample)
  animal = Faker::Creature::Animal.name
  "#{sanitize(plant)}-#{sanitize(animal)}"
end

.generateObject



3
4
5
6
7
8
9
# File 'lib/studio/username_generator.rb', line 3

def self.generate
  2.times do
    candidate = build_name
    return candidate unless User.exists?(username: candidate)
  end
  "#{build_name}-#{rand(1000..9999)}"
end

.sanitize(str) ⇒ Object



17
18
19
# File 'lib/studio/username_generator.rb', line 17

def self.sanitize(str)
  str.downcase.gsub(/[^a-z0-9]/, "-").gsub(/-+/, "-").gsub(/^-|-$/, "")
end