Sha256: b8519d90b8dd1f64249daf47cbf581a5539687037b998b8fdfc834195f93b653

Contents?: true

Size: 874 Bytes

Versions: 2

Compression:

Stored size: 874 Bytes

Contents

module DeepL
  module Requests
    class TranslateText < Base
      attr_reader :text, :source_lang, :target_lang

      def initialize(api, text, source_lang, target_lang)
        super(api)
        @text = text
        @source_lang = source_lang
        @target_lang = target_lang
      end

      def request
        payload = { text: text, source_lang: source_lang, target_lang: target_lang }
        build_texts(*post(payload))
      end

      private

      def build_texts(request, response)
        data = JSON.parse(response.body)

        texts = data['translations'].map do |translation|
          Resources::Text.new(translation['text'], translation['detected_source_language'],
                              request, response)
        end

        texts.size == 1 ? texts.first : texts
      end

      def path
        'translate'
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
deepl-rb-0.0.1 lib/deepl/requests/translate_text.rb
deepl-rb-0.0.0 lib/deepl/requests/translate_text.rb