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
|
# File 'lib/wovnrb/api_translator.rb', line 13
def translate(body)
connection = prepare_connection
request = prepare_request(body)
begin
response = connection.request(request)
rescue => e
WovnLogger.error("\"#{e.message}\" error occurred when contacting WOVNio translation API")
return body
end
case response
when Net::HTTPSuccess
if response.['Content-Encoding'] == 'gzip'
response_body = Zlib::GzipReader.new(StringIO.new(response.body)).read
JSON.parse(response_body)['body'] || body
elsif @store.dev_mode?
JSON.parse(response.body)['body'] || body
else
WovnLogger.error("Received invalid content (\"#{response.header['Content-Encoding']}\") from WOVNio translation API.")
body
end
else
WovnLogger.error("Received \"#{response.message}\" from WOVNio translation API.")
body
end
end
|