Class: Lita::Handlers::Random
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::Random
- 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 case <s>' => 'randomize case of each character of string `s`', '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`', 'shuffle <array, ...>' => 'new array with elements of `array` shuffled', 'sample <n=1> <array, ...>' => 'choose `n` random elements from `array`', }
Instance Method Summary collapse
- #route_random(response) ⇒ Object
- #route_random_base64(response) ⇒ Object
- #route_random_base64_n(response) ⇒ Object
- #route_random_case(response) ⇒ Object
- #route_random_float_from_to(response) ⇒ Object
- #route_random_float_to(response) ⇒ Object
- #route_random_from_to(response) ⇒ Object
- #route_random_hex(response) ⇒ Object
- #route_random_hex_n(response) ⇒ Object
- #route_random_pass(response) ⇒ Object
- #route_random_pass_n(response) ⇒ Object
- #route_random_smart_pass(response) ⇒ Object
- #route_random_smart_pass_n(response) ⇒ Object
- #route_random_to(response) ⇒ Object
- #route_random_uuid(response) ⇒ Object
- #route_sample(response) ⇒ Object
- #route_shuffle(response) ⇒ Object
Instance Method Details
#route_random(response) ⇒ Object
57 58 59 |
# File 'lib/lita/handlers/random.rb', line 57 def route_random(response) response.reply(::Random.rand.to_s) end |
#route_random_base64(response) ⇒ Object
99 100 101 |
# File 'lib/lita/handlers/random.rb', line 99 def route_random_base64(response) response.reply(SecureRandom.base64) end |
#route_random_base64_n(response) ⇒ Object
105 106 107 108 |
# File 'lib/lita/handlers/random.rb', line 105 def route_random_base64_n(response) n = response.matches[0][0].to_i response.reply(SecureRandom.base64(n)) end |
#route_random_case(response) ⇒ Object
94 95 96 |
# File 'lib/lita/handlers/random.rb', line 94 def route_random_case(response) response.reply(random_case(response.matches[0][0] || '')) end |
#route_random_float_from_to(response) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/lita/handlers/random.rb', line 85 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
68 69 70 71 |
# File 'lib/lita/handlers/random.rb', line 68 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
75 76 77 78 79 |
# File 'lib/lita/handlers/random.rb', line 75 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
111 112 113 |
# File 'lib/lita/handlers/random.rb', line 111 def route_random_hex(response) response.reply(SecureRandom.hex) end |
#route_random_hex_n(response) ⇒ Object
117 118 119 120 |
# File 'lib/lita/handlers/random.rb', line 117 def route_random_hex_n(response) n = response.matches[0][0].to_i response.reply(SecureRandom.hex(n)) end |
#route_random_pass(response) ⇒ Object
141 142 143 |
# File 'lib/lita/handlers/random.rb', line 141 def route_random_pass(response) response.reply(password) end |
#route_random_pass_n(response) ⇒ Object
147 148 149 150 |
# File 'lib/lita/handlers/random.rb', line 147 def route_random_pass_n(response) length = response.matches[0][0].to_i response.reply(password(length)) end |
#route_random_smart_pass(response) ⇒ Object
129 130 131 |
# File 'lib/lita/handlers/random.rb', line 129 def route_random_smart_pass(response) response.reply(smart_password) end |
#route_random_smart_pass_n(response) ⇒ Object
135 136 137 138 |
# File 'lib/lita/handlers/random.rb', line 135 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
62 63 64 65 |
# File 'lib/lita/handlers/random.rb', line 62 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
123 124 125 |
# File 'lib/lita/handlers/random.rb', line 123 def route_random_uuid(response) response.reply(SecureRandom.uuid) end |
#route_sample(response) ⇒ Object
161 162 163 164 165 166 167 168 |
# File 'lib/lita/handlers/random.rb', line 161 def route_sample(response) matches = response.matches[0] n = matches[0] s = matches[1] a = s ? s.split(',').map(&:strip) : [] result = n ? a.sample(n.to_i).join(', ') : a.sample.to_s response.reply(result) end |
#route_shuffle(response) ⇒ Object
153 154 155 156 157 |
# File 'lib/lita/handlers/random.rb', line 153 def route_shuffle(response) s = response.matches[0][0] a = s ? s.split(',').map(&:strip) : [] response.reply(a.shuffle.join(', ')) end |