Class: PassForge::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/passforge/batch.rb

Overview

Batch password generator Generate multiple passwords at once

Class Method Summary collapse

Class Method Details

.generate(count, type = :random, **options) ⇒ Array<String>

Generate multiple passwords

Examples:

Generate 10 random passwords

PassForge::Batch.generate(10, :random, length: 16)
# => ["aB3dE7gH9jK2mN5p", "xY9zA2bC4dE6fG8h", ...]

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/passforge/batch.rb', line 18

def self.generate(count, type = :random, **options)
  raise ArgumentError, "Count must be at least 1" if count < 1
  raise ArgumentError, "Count must be at most 1000" if count > 1000
  
  passwords = []
  
  count.times do
    password = case type
               when :random
                 length = options.delete(:length) || 12
                 Generator.generate(length, **options)
               when :passphrase
                 Passphrase.generate(**options)
               when :pronounceable
                 Pronounceable.generate(**options)
               when :pattern
                 Pattern.generate(options[:pattern] || "Cvccvc99!")
               else
                 raise ArgumentError, "Unknown type: #{type}"
               end
    
    passwords << password
  end
  
  passwords
end

.to_csv(passwords) ⇒ String

Export passwords to CSV format



48
49
50
# File 'lib/passforge/batch.rb', line 48

def self.to_csv(passwords)
  "Password\n" + passwords.join("\n")
end

.to_json(passwords) ⇒ String

Export passwords to JSON format



55
56
57
58
# File 'lib/passforge/batch.rb', line 55

def self.to_json(passwords)
  require "json"
  { passwords: passwords, count: passwords.length, generated_at: Time.now.iso8601 }.to_json
end