Sha256: 91cb947b5b011798c9a1d8b3d941820df1660f720f3112a500ceae4be32143a5

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 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_available synonyms_data
        synonyms_data[0..(length-3)].delete("\"").gsub(/(,)/, ", ")
      else
        " ---"
      end
    end

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

    def synonyms_available synonyms_data
      !synonyms_data.include?('true,false')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
termit-2.10.0 lib/termit/text_response_handler.rb
termit-2.0.9 lib/termit/text_response_handler.rb