module RaspbergPasswordOptions
RaspbergPasswordOptions — parse !raspberg password flags (VB Password Generator 1.1 GUI).
Constants
- DEFAULT_COUNT
- DEFAULT_LENGTH
- MAX_COUNT
- MAX_CUSTOM_LENGTH
- MAX_LENGTH
Public Instance Methods
Source
# File lib/raspberg_password_options.rb, line 81 def charset_enabled?(lower:, upper:, numbers:, extra:, vowl:, custom:, custom_enabled:) return true if lower || upper || numbers || extra || vowl return custom_enabled && !custom.to_s.empty? if custom_enabled false end
Source
# File lib/raspberg_password_options.rb, line 88 def option_summary(opts) parts = ["length #{opts[:length]}"] parts << "lower" if opts[:lower] parts << "upper" if opts[:upper] parts << "numbers" if opts[:numbers] parts << "extra" if opts[:extra] parts << "vowl" if opts[:vowl] parts << "custom" if opts[:custom_enabled] parts << "count #{opts[:count]}" if opts[:count].to_i > 1 parts end
Source
# File lib/raspberg_password_options.rb, line 13 def parse(text) opts = { length: DEFAULT_LENGTH, lower: true, upper: true, numbers: true, extra: false, vowl: false, custom: nil, custom_enabled: false, count: DEFAULT_COUNT } text.to_s.strip.split(/\s+/).each do |token| case token.downcase when "lower" opts[:lower] = true when "nolower" opts[:lower] = false when "upper" opts[:upper] = true when "noupper" opts[:upper] = false when "numbers" opts[:numbers] = true when "nonumbers" opts[:numbers] = false when "extra" opts[:extra] = true when "noextra" opts[:extra] = false when "vowl" opts[:vowl] = true when "novowl" opts[:vowl] = false else if (match = token.match(/\A(?:length|len|chars?)=(\d+)\z/i)) opts[:length] = match[1].to_i elsif (match = token.match(/\Acount=(\d+)\z/i)) opts[:count] = match[1].to_i elsif (match = token.match(/\Acustom=(.+)\z/i)) opts[:custom] = match[1] opts[:custom_enabled] = !opts[:custom].empty? elsif token.match?(/\A\d+\z/) opts[:length] = token.to_i end end end opts[:custom] = opts[:custom].to_s[0, MAX_CUSTOM_LENGTH] if opts[:custom] opts end
Source
# File lib/raspberg_password_options.rb, line 66 def validate(length:, lower:, upper:, numbers:, extra:, vowl:, custom:, custom_enabled:) return "Please use a positive integer for the number of characters." if length < 1 || length > MAX_LENGTH unless charset_enabled?(lower: lower, upper: upper, numbers: numbers, extra: extra, vowl: vowl, custom: custom, custom_enabled: custom_enabled) return "Select at least one character set (lower, upper, numbers, extra, vowl, or custom=chars)." end if !lower && !upper && !numbers && !extra && !vowl && custom_enabled && custom.to_s.empty? return "custom= requires at least one character." end nil end