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
STRING_TO_SINGLE_ELEMENT_ARRAY_CONVERSION =

A custom instruction may contain commas, so a single string is one instruction.

lambda { |value|
  value.is_a?(String) ? [value] : value
}.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_SINGLE_ELEMENT_ARRAY_CONVERSION
}.freeze

Constants inherited from Base

Base::MAX_GLOSSARY_IDS, Base::NETWORK_ERRORS, Base::RESPONSE_FORMAT_ERRORS, Base::RETRYABLE_NETWORK_ERRORS, Base::TECHNICAL_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Base

#api, #options, #response

Instance Method Summary collapse

Methods inherited from Base

accepted_options, accepts_options, inherited, validate_options!

Constructor Details

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

Returns a new instance of Translate.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/deepl/requests/translate.rb', line 43

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
  validate_text!(text)
  validate_languages!(source_lang, target_lang)
  validate_translation_memory_threshold!

  tweak_parameters!
end

Instance Attribute Details

#custom_instructions ⇒ Object (readonly)

Returns the value of attribute custom_instructions.



40
41
42
# File 'lib/deepl/requests/translate.rb', line 40

def custom_instructions
  @custom_instructions
end

#ignore_tags ⇒ Object (readonly)

Returns the value of attribute ignore_tags.



40
41
42
# File 'lib/deepl/requests/translate.rb', line 40

def ignore_tags
  @ignore_tags
end

#model_type ⇒ Object (readonly)

Returns the value of attribute model_type.



40
41
42
# File 'lib/deepl/requests/translate.rb', line 40

def model_type
  @model_type
end

#non_splitting_tags ⇒ Object (readonly)

Returns the value of attribute non_splitting_tags.



40
41
42
# File 'lib/deepl/requests/translate.rb', line 40

def non_splitting_tags
  @non_splitting_tags
end

#source_lang ⇒ Object (readonly)

Returns the value of attribute source_lang.



40
41
42
# File 'lib/deepl/requests/translate.rb', line 40

def source_lang
  @source_lang
end

#splitting_tags ⇒ Object (readonly)

Returns the value of attribute splitting_tags.



40
41
42
# File 'lib/deepl/requests/translate.rb', line 40

def splitting_tags
  @splitting_tags
end

#tag_handling_version ⇒ Object (readonly)

Returns the value of attribute tag_handling_version.



40
41
42
# File 'lib/deepl/requests/translate.rb', line 40

def tag_handling_version
  @tag_handling_version
end

#target_lang ⇒ Object (readonly)

Returns the value of attribute target_lang.



40
41
42
# File 'lib/deepl/requests/translate.rb', line 40

def target_lang
  @target_lang
end

#text ⇒ Object (readonly)

Returns the value of attribute text.



40
41
42
# File 'lib/deepl/requests/translate.rb', line 40

def text
  @text
end

Instance Method Details

#details ⇒ Object



82
83
84
85
# File 'lib/deepl/requests/translate.rb', line 82

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

#request ⇒ Object

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/deepl/requests/translate.rb', line 55

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

  style_id = take_resource_option(:style_rule, [DeepL::Resources::StyleRule], :style_id)
  payload[:style_id] = style_id unless style_id.nil?
  profile_id = take_resource_option(:style_profile, [DeepL::Resources::StyleProfile],
                                    :style_profile_id)
  payload[:style_profile_id] = profile_id unless profile_id.nil?
  tm_id = take_resource_option(:translation_memory, [DeepL::Resources::TranslationMemory],
                               :translation_memory_id)
  payload[:translation_memory_id] = tm_id unless tm_id.nil?

  if option?(:translation_memory_threshold)
    threshold = delete_option(:translation_memory_threshold)
    payload[:translation_memory_threshold] = threshold unless threshold.nil?
  end

  glossary_ids = build_glossary_ids_param
  payload[:glossary_ids] = glossary_ids unless glossary_ids.nil?
  glossary_id = build_glossary_param
  payload[:glossary_id] = glossary_id unless glossary_id.nil?

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

#to_s ⇒ Object



87
88
89
# File 'lib/deepl/requests/translate.rb', line 87

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