Class: DeepL::Requests::Translate

Inherits:
Base
  • Object
show all
Defined in:
lib/deepl/requests/translate.rb

Constant Summary collapse

STRING_TO_BOOLEAN_MAP =
{ '1' => true, '0' => false }.freeze
BOOLEAN_TO_STRING_MAP =
{ true => '1', false => '0' }.freeze
STRING_TO_BOOLEAN_CONVERSION =
->(value) { STRING_TO_BOOLEAN_MAP[value] }
BOOLEAN_TO_STRING_CONVERSION =
->(value) { BOOLEAN_TO_STRING_MAP[value] }
STRING_TO_ARRAY_CONVERSION =
lambda { |value|
  if value.nil?
    nil
  else
    (value.is_a?(Array) ? value : value.split(','))
  end
}.freeze
OPTIONS_CONVERSIONS =
{
  split_sentences: BOOLEAN_TO_STRING_CONVERSION,
  preserve_formatting: STRING_TO_BOOLEAN_CONVERSION,
  outline_detection: STRING_TO_BOOLEAN_CONVERSION,
  splitting_tags: STRING_TO_ARRAY_CONVERSION,
  non_splitting_tags: STRING_TO_ARRAY_CONVERSION,
  ignore_tags: STRING_TO_ARRAY_CONVERSION,
  custom_instructions: STRING_TO_ARRAY_CONVERSION
}.freeze

Constants inherited from Base

Base::MAX_GLOSSARY_IDS

Instance Attribute Summary collapse

Attributes inherited from Base

#api, #options, #response

Instance Method Summary collapse

Constructor Details

#initialize(api, text, source_lang, target_lang, options = {}, additional_headers = {}) ⇒ Translate

Returns a new instance of Translate.



33
34
35
36
37
38
39
40
# File 'lib/deepl/requests/translate.rb', line 33

def initialize(api, text, source_lang, target_lang, options = {}, additional_headers = {})
  super(api, options, additional_headers)
  @text = text
  @source_lang = source_lang
  @target_lang = target_lang

  tweak_parameters!
end

Instance Attribute Details

#custom_instructionsObject (readonly)

Returns the value of attribute custom_instructions.



30
31
32
# File 'lib/deepl/requests/translate.rb', line 30

def custom_instructions
  @custom_instructions
end

#ignore_tagsObject (readonly)

Returns the value of attribute ignore_tags.



30
31
32
# File 'lib/deepl/requests/translate.rb', line 30

def ignore_tags
  @ignore_tags
end

#model_typeObject (readonly)

Returns the value of attribute model_type.



30
31
32
# File 'lib/deepl/requests/translate.rb', line 30

def model_type
  @model_type
end

#non_splitting_tagsObject (readonly)

Returns the value of attribute non_splitting_tags.



30
31
32
# File 'lib/deepl/requests/translate.rb', line 30

def non_splitting_tags
  @non_splitting_tags
end

#source_langObject (readonly)

Returns the value of attribute source_lang.



30
31
32
# File 'lib/deepl/requests/translate.rb', line 30

def source_lang
  @source_lang
end

#splitting_tagsObject (readonly)

Returns the value of attribute splitting_tags.



30
31
32
# File 'lib/deepl/requests/translate.rb', line 30

def splitting_tags
  @splitting_tags
end

#tag_handling_versionObject (readonly)

Returns the value of attribute tag_handling_version.



30
31
32
# File 'lib/deepl/requests/translate.rb', line 30

def tag_handling_version
  @tag_handling_version
end

#target_langObject (readonly)

Returns the value of attribute target_lang.



30
31
32
# File 'lib/deepl/requests/translate.rb', line 30

def target_lang
  @target_lang
end

#textObject (readonly)

Returns the value of attribute text.



30
31
32
# File 'lib/deepl/requests/translate.rb', line 30

def text
  @text
end

Instance Method Details

#detailsObject



74
75
76
77
# File 'lib/deepl/requests/translate.rb', line 74

def details
  "HTTP Headers: #{headers}\nPayload #{{ text: text, source_lang: source_lang,
                                         target_lang: target_lang }}"
end

#requestObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/deepl/requests/translate.rb', line 42

def request # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
  text_arrayified = text.is_a?(Array) ? text : [text]
  payload = { text: text_arrayified, source_lang: source_lang, target_lang: target_lang }

  if option?(:style_rule)
    style_rule = delete_option(:style_rule)
    payload[:style_id] = if style_rule.is_a?(DeepL::Resources::StyleRule)
                           style_rule.style_id
                         else
                           style_rule.to_s
                         end
  end

  if option?(:translation_memory)
    tm = delete_option(:translation_memory)
    payload[:translation_memory_id] = if tm.is_a?(DeepL::Resources::TranslationMemory)
                                        tm.translation_memory_id
                                      else
                                        tm.to_s
                                      end
  end

  if option?(:translation_memory_threshold)
    payload[:translation_memory_threshold] = delete_option(:translation_memory_threshold)
  end

  glossary_ids = build_glossary_ids_param(source_lang)
  payload[:glossary_ids] = glossary_ids unless glossary_ids.nil?

  build_texts(*execute_request_with_retries(post_request(payload)))
end

#to_sObject



79
80
81
# File 'lib/deepl/requests/translate.rb', line 79

def to_s
  "POST #{uri.request_uri}"
end