Module: Koodmeeter

Defined in:
lib/koodmeeter/core.rb,
lib/koodmeeter/axiom.rb,
lib/koodmeeter/version.rb

Defined Under Namespace

Classes: Axiom

Constant Summary collapse

LEVELS =
(0..5).to_a
DEFAULT_MIN_CHARS =
6
VERSION =
"0.0.2"
@@scores =
[10, 15, 25, 45]

Class Method Summary collapse

Class Method Details

.blacklistObject



38
39
40
41
42
# File 'lib/koodmeeter/core.rb', line 38

def blacklist
  path = File.join root, 'data/blacklist.json'
  file = File.read path
  JSON.parse(file)['blacklist']
end

.check(password, minimum_chars = DEFAULT_MIN_CHARS) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/koodmeeter/core.rb', line 10

def check(password, minimum_chars = DEFAULT_MIN_CHARS)
  password = password.to_s

  raise ArgumentError.new 'Password argument required!' if password.nil?
  return 0 unless blacklist.index(password).nil?

  length = password.length
  diff = length - minimum_chars
  scores_dupe = @@scores.dup
  score = calculate_diff_increment(diff)

  Axiom.list.each do |axiom|
    if axiom[:regex].match(password)
      score += axiom[:score]
    end
  end

  score += length
  if score < 0 && score > -199
    index = 0
  else
    scores_dupe.push(score).sort!
    index = scores_dupe.index(score) + 1
  end

  return LEVELS[index].nil? ? LEVELS.last : LEVELS[index]
end