Sha256: c881612e08a0eb183626d400f81e9b7e589130d3ce4d3930dad3cba4674aacc1

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 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' #there are no synonyms
        " ---"
      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

1 entries across 1 versions & 1 rubygems

Version Path
termit-2.0.8 lib/termit/text_response_handler.rb