Sha256: 7ea0f179b5b0dbaa4d5f724cf11e0ffa6dfbb61e6f224bdbb11d6ceb415977df

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module DeepL
  module Requests
    module Glossary
      class Create < Base
        attr_reader :name, :source_lang, :target_lang, :entries, :entries_format

        def initialize(api, name, source_lang, target_lang, entries, options = {})
          super(api, options)
          @name = name
          @source_lang = source_lang
          @target_lang = target_lang
          @entries = entries
          @entries_format = delete_option(:entries_format) || 'tsv'
        end

        def request
          payload = {
            name: name, source_lang: source_lang, target_lang: target_lang, entries: entries_to_tsv,
            entries_format: entries_format
          }
          build_glossary(*post(payload))
        end

        private

        def entries_to_tsv
          return entries if entries.is_a?(String)

          entries.reduce('') { |tsv, entry| "#{tsv}#{entry.first}\t#{entry.last}\n" }
        end

        def build_glossary(request, response)
          glossary = JSON.parse(response.body)
          Resources::Glossary.new(glossary, request, response)
        end

        def path
          'glossaries'
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
deepl-rb-2.5.3 lib/deepl/requests/glossary/create.rb
deepl-rb-2.5.2 lib/deepl/requests/glossary/create.rb
deepl-rb-2.5.1 lib/deepl/requests/glossary/create.rb
deepl-rb-2.5.0 lib/deepl/requests/glossary/create.rb