Module: PolypassHelpers

Included in:
Polypass
Defined in:
lib/polypass/helpers.rb

Overview

Collection of general Polypass helper methods

Instance Method Summary collapse

Instance Method Details

#alphanumeric_charsObject



5
6
7
8
# File 'lib/polypass/helpers.rb', line 5

def alphanumeric_chars
  characters = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
  return characters
end

#alphasymbol_charsObject



10
11
12
13
14
# File 'lib/polypass/helpers.rb', line 10

def alphasymbol_chars
  characters = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
  characters += %w(! @ # $ % ^ & * \( \) { } [ ] - _ ~ < > ? ; : ` ~ / + = , . |)
  return characters
end

#output_json(hash) ⇒ Object



16
17
18
# File 'lib/polypass/helpers.rb', line 16

def output_json(hash)
  JSON.pretty_generate(hash)
end

#output_yaml(hash) ⇒ Object



20
21
22
# File 'lib/polypass/helpers.rb', line 20

def output_yaml(hash)
  YAML.dump(hash)
end

#random_element(array) ⇒ Object



24
25
26
# File 'lib/polypass/helpers.rb', line 24

def random_element(array)
  array[SecureRandom.random_number(array.size)].to_s
end

#random_number(integer = 1000) ⇒ Object



28
29
30
# File 'lib/polypass/helpers.rb', line 28

def random_number(integer = 1000)
  (SecureRandom.random_number(integer) + (SecureRandom.random_number(100) + 1)).to_i
end

#random_sample_chars(chars, length = 32) ⇒ Object



32
33
34
# File 'lib/polypass/helpers.rb', line 32

def random_sample_chars(chars, length = 32)
  (1..length).map { chars[SecureRandom.random_number(chars.size)] }.join
end

#random_sample_set(chartype, length = 32, rounds = 1000) ⇒ Object



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

def random_sample_set(chartype, length = 32, rounds = 1000)
  random_chars = Array.new
  (1..random_number(rounds)).each do
    case chartype
    when 'alphanumeric'
      random_chars << random_sample_chars(alphanumeric_chars, length)
    when 'alphasymbol'
      random_chars << random_sample_chars(alphasymbol_chars, length)
    else
      raise ArgumentError,
            chartype + 'is an invalid chartype argument. alphanumeric and alphasymbol are the ' +
            'only valid chartype arguments for the random_sample_set method'
    end
  end
  return random_chars
end