Sha256: e018e8c33f2e399e501e4f46beaee1f81b78c4b279b8c1f1bd83be73543f9c6f

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

# Copyright 2022 Daniel Herzog
# Use of this source code is governed by an MIT
# license that can be found in the LICENSE.md file.
# 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(*execute_request_with_retries(post_request(payload)))
        end

        def details
          "HTTP Headers: #{headers}\nPayload #{{
            name: name, source_lang: source_lang, target_lang: target_lang, entries: entries_to_tsv,
            entries_format: entries_format
          }}"
        end

        def to_s
          "POST #{uri.request_uri}"
        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-3.1.0 lib/deepl/requests/glossary/create.rb
deepl-rb-3.0.2 lib/deepl/requests/glossary/create.rb
deepl-rb-3.0.1 lib/deepl/requests/glossary/create.rb
deepl-rb-3.0.0 lib/deepl/requests/glossary/create.rb