Sha256: cc1f6e8836a9a9d2f30173c70fba9d348d1ffa50753c7c1132119eed7b3e8374

Contents?: true

Size: 822 Bytes

Versions: 10

Compression:

Stored size: 822 Bytes

Contents

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

  def identify(user)
    password = mkpasswd
    puts_password(user, password)
    password
  end

  private

  def mkpasswd(len = 8)
    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

10 entries across 10 versions & 1 rubygems

Version Path
posgra-0.2.1 lib/posgra/identifier/auto.rb
posgra-0.2.0 lib/posgra/identifier/auto.rb
posgra-0.1.9 lib/posgra/identifier/auto.rb
posgra-0.1.8 lib/posgra/identifier/auto.rb
posgra-0.1.7 lib/posgra/identifier/auto.rb
posgra-0.1.6 lib/posgra/identifier/auto.rb
posgra-0.1.5 lib/posgra/identifier/auto.rb
posgra-0.1.4 lib/posgra/identifier/auto.rb
posgra-0.1.3 lib/posgra/identifier/auto.rb
posgra-0.1.2 lib/posgra/identifier/auto.rb