Module: SafeRandom

Defined in:
lib/safe_random/base.rb,
lib/safe_random/text.rb,
lib/safe_random/version.rb

Constant Summary collapse

VERSION =
'0.0.6'.freeze

Class Method Summary collapse

Class Method Details

.alphanumeric(_length = 32, _case = nil) ⇒ Object



10
11
12
# File 'lib/safe_random/base.rb', line 10

def self.alphanumeric(_length = 32, _case = nil)
  random(_length, Constants::SET_ALPHANUMBERIC)
end

.generate_token(_length) ⇒ Object



40
41
42
# File 'lib/safe_random/base.rb', line 40

def self.generate_token(_length)
  Digest::SHA1.hexdigest([Time.now, rand].join)[0.._length]
end

.number(_length = 32) ⇒ Object



2
3
4
# File 'lib/safe_random/base.rb', line 2

def self.number(_length = 32)
  random(_length, Constants::SET_NUMBER)
end

.paragraphs(num = 2) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/safe_random/text.rb', line 17

def self.paragraphs(num = 2)
  text = ''

  num.times do
    (rand(5) + 1).times do
      text += Constants::SET_PARAGRAPH.sample + '. '
    end
    text += "\n\n"
  end
  text
end

.random(len = 32, character_set) ⇒ Object

> Methods - For Main



24
25
26
27
# File 'lib/safe_random/base.rb', line 24

def self.random(len = 32, character_set)
  chars = character_set.map { |x| x.is_a?(Range) ? x.to_a : x }.flatten
  Array.new(len) { chars.sample }.join
end

.sentences(num = 1) ⇒ Object

> Returns a given number of paragraphs delimited by two newlines (defaults to two paragraphs),

> using a small pool of generic sentences.

> SafeRandom.sentences

> SafeRandom.paragraphs

“I might jump an open drawbridge or Tarzan from a vine, beause I’m the unknown stuntman that makes Eastwood look so fine.\n\n Always fighting all the evil forces bringing peace and justice to all. \n\n”



9
10
11
12
13
14
15
# File 'lib/safe_random/text.rb', line 9

def self.sentences(num = 1)
  sentences = ''
  num.times do
    sentences += Constants::SET_PARAGRAPH.sample + '. '
  end
  sentences.strip
end

.single(_type) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/safe_random/base.rb', line 44

def self.single(_type)
  case _type
  when 'number'
    random(1, Constants::SET_NUMBER)
  when 'upcase'
    random(1, Constants::SET_UPCASE)
  when 'downcase'
    random(1, Constants::SET_DOWNCASE)
  when 'symbol'
    random(1, Constants::SET_SYMBOL)
  else
    random(1, Constants::SET_ALPHANUMBERIC)
  end
end

.small_tokenObject



14
15
16
# File 'lib/safe_random/base.rb', line 14

def self.small_token
  generate_token(6)
end

.string(_length = 32, _case = nil) ⇒ Object



6
7
8
# File 'lib/safe_random/base.rb', line 6

def self.string(_length = 32, _case = nil)
  random(_length, Constants::SET_STRING)
end

.strong_string(_len = 32) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/safe_random/base.rb', line 29

def self.strong_string(_len = 32)
  _len = 8 if _len < 5
  [single('number'),
   single('symbol'),
   single('upcase'),
   single('downcase'),
   alphanumeric(_len - 4)]
    .shuffle
    .join
end

.tokenObject



18
19
20
# File 'lib/safe_random/base.rb', line 18

def self.token
  generate_token(10)
end