Sha256: db4e7906a3a69278f7e9fe8d2dec753751c17a423a85bbccda5906337901dc45

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

#encoding: UTF-8
module Termit
  class TextResponseHandler
    extend ::Delegation
    @output_manager = Termit::OutputManager.new
    delegate :display_synonyms, :display_translation, to: @output_manager

    def initialize text, synonyms_wanted
      @text = decode text
      @synonyms_wanted = synonyms_wanted
    end

    def call
      translation = extract_translation
      display_translation translation
      display_synonyms extract_synonyms if @synonyms_wanted
      translation
    end

    private

    def extract_translation
      @text.split("[[")[1].split("\"")[1]
    end

    def extract_synonyms
      synonyms_data = @text.split("[[")[2].split("[")[1]
      length = synonyms_data.length
      if synonyms_data[0] == '4' # my code works i have no idea why ..
        " ---"
      else
        synonyms_data[0..(length-3)].delete("\"").gsub(/(,)/, ", ")
      end
    end

    def decode text
      encoding = 'UTF-8'
      text.gsub!(/(\\x26#39;)/, "'")
      text.force_encoding(encoding).encode(encoding)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
termit-2.0.7 lib/termit/text_response_handler.rb
termit-2.0.6 lib/termit/text_response_handler.rb
termit-2.0.5 lib/termit/text_response_handler.rb
termit-2.0.4 lib/termit/text_response_handler.rb
termit-2.0.3 lib/termit/text_response_handler.rb
termit-2.0.2 lib/termit/text_response_handler.rb
termit-2.0.1 lib/termit/text_response_handler.rb
termit-2.0.0 lib/termit/text_response_handler.rb