Class: Pass

Inherits:
Object
  • Object
show all
Defined in:
lib/pass.rb,
lib/pass/cli.rb,
lib/pass/version.rb

Defined Under Namespace

Classes: CLI, Error

Constant Summary collapse

MIN_PASSWORD_LENGTH =
5
DEFAULT_NUM_PASSWORDS =
1
DEFAULT_PASSWORD_LENGTH =
20
ALPHABETIC_CHARS =
('a'..'z').to_a + ('A'..'Z').to_a
NUMERIC_CHARS =
('1'..'9').to_a
SYMBOL_CHARS =
('!'..'/').to_a + (':'..'@').to_a + ('['..'`').to_a + ('{'..'~').to_a
AMBIGUOUS_CHARS =
%w[l o I O 1 " ' ` |]
VERSION =
'0.0.6'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Pass

Returns a new instance of Pass.



13
14
15
16
# File 'lib/pass.rb', line 13

def initialize(**options)
  @options = { length: DEFAULT_PASSWORD_LENGTH }
  @options.update(options)
end

Class Method Details

.generate(num = DEFAULT_PASSWORD_LENGTH) ⇒ Object



54
55
56
# File 'lib/pass.rb', line 54

def self.generate(num = DEFAULT_PASSWORD_LENGTH)
  new(length: num).generate
end

Instance Method Details

#char_listObject



18
19
20
21
22
23
24
25
26
# File 'lib/pass.rb', line 18

def char_list
  list = ALPHABETIC_CHARS + NUMERIC_CHARS

  list += SYMBOL_CHARS if @options[:symbols]

  list -= exclude_char_list

  list
end

#exclude_char_listObject



28
29
30
31
32
# File 'lib/pass.rb', line 28

def exclude_char_list
  list = AMBIGUOUS_CHARS
  list += @options[:exclude].split(//).sort.uniq if @options[:exclude]
  list
end

#generateObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pass.rb', line 34

def generate
  length = @options[:length]

  if length < MIN_PASSWORD_LENGTH
    raise Pass::Error,
          "Invalid Argument: password length must be more than #{MIN_PASSWORD_LENGTH}."
  end

  rest_length = length
  result = ""
  # append append password string until 'result' reaches the 'length'
  while rest_length > char_list_size
    result += generate_password_base(char_list_size)
    rest_length -= char_list_size
  end
  result += generate_password_base(rest_length)

  result
end