forked from open-runtimes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.rb
35 lines (31 loc) · 775 Bytes
/
index.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
require "free_google_translate"
require "json"
def main(req, res)
# Input validation
text = nil
source = nil
target = nil
begin
payload = JSON.parse(req.payload)
text = payload["text"]
source = payload["source"]
target = payload["target"]
rescue Exception => err
puts err
raise 'Payload is invalid.'
end
if text.nil? or text.empty? or source.nil? or source.empty? or target.nil? or target.empty?
raise 'Payload is invalid.'
end
# Translate your text
translation = GoogleTranslate.translate(
from: source,
to: target,
text: text
)
# Return the translation
res.json({
"text": text,
"translation": translation
})
end