Class: Lita::Handlers::Random

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/random.rb

Overview

Generator of random numbers and strings for the Lita chat bot.

Constant Summary collapse

HELP =

rubocop:disable Metrics/ClassLength

{
  'random' =>
    'random float number, greater or equal to 0 and lesser than 1',

  'random <to>' =>
    'random integer or float number, ' \
    'greater or equal to 0 and lesser than `to`',

  'random <from> <to>' =>
    'random integer or float number, ' \
    'greater or equal to `from` and lesser than `to`',

  'random base64 <n=16>' =>
    'random base64 string, length of source string is n, ' \
    'length of result is about `n * 4 / 3` ' \
    '(24 with default value of `n`)',

  'random hex <n=16>' =>
    'random hexadecimal string with length `n * 2`',

  'random uuid' =>
    'v4 random UUID (Universally Unique IDentifier). ' \
    'The version 4 UUID is purely random (except the version). ' \
    'It doesn’t contain meaningful information ' \
    'such as MAC address, time, etc.',

  'random password <n=16>' =>
    'random password with length `n` containing characters ' \
    'in upper and lower case, and digits',

  'random smart password <n=8>' =>
    'random pronounceable password with a minimum length of `n`',
}
SMART_PASS_SEQS =
{
  false => %w(
    b c d f g h j k l m n p qu r s t v w x z
    ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr lt
  ),
  true => %w(a e i o u y),
}
PASS_CHARS =
[*'a'..'z', *'A'..'Z', *'0'..'9']

Instance Method Summary collapse

Instance Method Details

#password(length = 16) ⇒ Object (protected)



160
161
162
# File 'lib/lita/handlers/random.rb', line 160

def password(length = 16)
  (0...length).map { |_| PASS_CHARS.sample }.join
end

#route_random(response) ⇒ Object



48
49
50
# File 'lib/lita/handlers/random.rb', line 48

def route_random(response)
  response.reply(::Random.rand.to_s)
end

#route_random_base64(response) ⇒ Object



84
85
86
# File 'lib/lita/handlers/random.rb', line 84

def route_random_base64(response)
  response.reply(SecureRandom.base64)
end

#route_random_base64_n(response) ⇒ Object



90
91
92
93
# File 'lib/lita/handlers/random.rb', line 90

def route_random_base64_n(response)
  n = response.matches[0][0].to_i
  response.reply(SecureRandom.base64(n))
end

#route_random_float_from_to(response) ⇒ Object



76
77
78
79
80
81
# File 'lib/lita/handlers/random.rb', line 76

def route_random_float_from_to(response)
  matches = response.matches[0]
  from = matches[0].to_f
  to = matches[1].to_f
  response.reply(::Random.rand(from...to).to_s)
end

#route_random_float_to(response) ⇒ Object



59
60
61
62
# File 'lib/lita/handlers/random.rb', line 59

def route_random_float_to(response)
  to = response.matches[0][1].to_f
  response.reply(::Random.rand(to).to_s)
end

#route_random_from_to(response) ⇒ Object



66
67
68
69
70
# File 'lib/lita/handlers/random.rb', line 66

def route_random_from_to(response)
  from = response.matches[0][1].to_i
  to = response.matches[0][2].to_i
  response.reply(::Random.rand(from...to).to_s)
end

#route_random_hex(response) ⇒ Object



96
97
98
# File 'lib/lita/handlers/random.rb', line 96

def route_random_hex(response)
  response.reply(SecureRandom.hex)
end

#route_random_hex_n(response) ⇒ Object



102
103
104
105
# File 'lib/lita/handlers/random.rb', line 102

def route_random_hex_n(response)
  n = response.matches[0][0].to_i
  response.reply(SecureRandom.hex(n))
end

#route_random_pass(response) ⇒ Object



126
127
128
# File 'lib/lita/handlers/random.rb', line 126

def route_random_pass(response)
  response.reply(password)
end

#route_random_pass_n(response) ⇒ Object



132
133
134
135
# File 'lib/lita/handlers/random.rb', line 132

def route_random_pass_n(response)
  length = response.matches[0][0].to_i
  response.reply(password(length))
end

#route_random_smart_pass(response) ⇒ Object



114
115
116
# File 'lib/lita/handlers/random.rb', line 114

def route_random_smart_pass(response)
  response.reply(smart_password)
end

#route_random_smart_pass_n(response) ⇒ Object



120
121
122
123
# File 'lib/lita/handlers/random.rb', line 120

def route_random_smart_pass_n(response)
  min_length = response.matches[0][0].to_i
  response.reply(smart_password(min_length))
end

#route_random_to(response) ⇒ Object



53
54
55
56
# File 'lib/lita/handlers/random.rb', line 53

def route_random_to(response)
  to = response.matches[0][1].to_i
  response.reply(::Random.rand(to).to_s)
end

#route_random_uuid(response) ⇒ Object



108
109
110
# File 'lib/lita/handlers/random.rb', line 108

def route_random_uuid(response)
  response.reply(SecureRandom.uuid)
end

#smart_password(min_length = 8) ⇒ Object (protected)



147
148
149
150
151
152
153
154
155
156
# File 'lib/lita/handlers/random.rb', line 147

def smart_password(min_length = 8)
  password = ''
  sequence_id = false
  while password.length < min_length
    sequence = SMART_PASS_SEQS[sequence_id]
    password << sequence.sample
    sequence_id = !sequence_id
  end
  password
end