Sha256: 950c32db8f86b08df184e7d3986f621be05f5cc6ed426f179e24978634c1070e

Contents?: true

Size: 858 Bytes

Versions: 2

Compression:

Stored size: 858 Bytes

Contents

class Posgra::Identifier::Auto
  def initialize(output, options = {})
    @output = output
    @options = options
  end

  def identify(user)
    password = mkpasswd((@options[:password_length] || 8).to_i)
    puts_password(user, password)
    password
  end

  private

  def mkpasswd(len)
    sources = [
      (1..9).to_a,
      ('A'..'Z').to_a,
      ('a'..'z').to_a,
    ].shuffle

    passwd = []

    len.times do |i|
      src = sources[i % sources.length]
      passwd << src.shuffle.shift
    end

    passwd.join
  end

  def puts_password(user, password)
    open_output do |f|
      f.puts("#{user},#{password}")
    end
  end

  def open_output
    return if @options[:dry_run]

    if @output == '-'
      yield($stdout)
      $stdout.flush
    else
      open(@output, 'a') do |f|
        yield(f)
        f.flush
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
posgra-0.2.3.beta lib/posgra/identifier/auto.rb
posgra-0.2.2 lib/posgra/identifier/auto.rb