Sha256: 235de6d9863039fa9217395490f7eafcbe82d2879b1b4655a663c7890ab76c97

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

#encoding: UTF-8
module Termit
  class TextResponseHandler
    include CanOutput
    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 && synonyms_data.length
      if length && 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

6 entries across 6 versions & 1 rubygems

Version Path
termit-2.13.0 lib/termit/text_response_handler.rb
termit-2.12.1 lib/termit/text_response_handler.rb
termit-2.12.0 lib/termit/text_response_handler.rb
termit-2.11.2 lib/termit/text_response_handler.rb
termit-2.11.1 lib/termit/text_response_handler.rb
termit-2.11.0 lib/termit/text_response_handler.rb