forked from devoffarg/DevOff-Desafio-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
45 lines (36 loc) · 1.02 KB
/
main.rb
1
2
3
4
5
6
7
8
9
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
37
38
39
40
41
42
43
44
45
require 'sinatra'
def encriptar(largo, texto)
mensaje = ''
n = (texto.length / largo.to_f).ceil
(0...largo).each do |i|
(0...n).each do |j|
mensaje += texto[j * largo + i] || ' '
end
end
mensaje.strip
end
def desencriptar(largo, mensaje)
texto = ''
n = (mensaje.length / largo.to_f).ceil
(0...n).each do |i|
(0...largo).each do |j|
texto += mensaje[j * n + i] || ' '
end
end
texto.strip
end
puts <<~FIN
Ejemplo de invocación:
curl -d '{ "vueltas": 4, "mensaje": "Devoff se puso ATR" }' -X POST http://localhost:4567/encriptar
# => {"mensaje":"DfesTef oRv p osuA"}
curl -d '{ "vueltas": 4, "mensaje": "DfesTef oRv p osuA" }' -X POST http://localhost:4567/desencriptar
# => {"mensaje":"Devoff se puso ATR"}
FIN
post '/encriptar' do
data = JSON.parse(request.body.read)
{ mensaje: encriptar(data['vueltas'], data['mensaje']) }.to_json
end
post '/desencriptar' do
data = JSON.parse(request.body.read)
{ mensaje: desencriptar(data['vueltas'], data['mensaje']) }.to_json
end