Class: Wovnrb::ApiTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/wovnrb/api_translator.rb

Instance Method Summary collapse

Constructor Details

#initialize(store, headers, uuid) ⇒ ApiTranslator

Returns a new instance of ApiTranslator.



8
9
10
11
12
# File 'lib/wovnrb/api_translator.rb', line 8

def initialize(store, headers, uuid)
  @store = store
  @headers = headers
  @uuid = uuid
end

Instance Method Details

#translate(body) ⇒ Object



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
# File 'lib/wovnrb/api_translator.rb', line 14

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
    begin
      raw_response_body = @store.dev_mode? ? response.body : Zlib::GzipReader.new(StringIO.new(response.body)).read
    rescue Zlib::GzipFile::Error
      raw_response_body = response.body
    end

    begin
      JSON.parse(raw_response_body)['body'] || body
    rescue JSON::JSONError
      body
    end
  else
    WovnLogger.error("HTML-swapper call failed. Received \"#{response.message}\" from WOVNio translation API.")
    body
  end
end