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
}.freeze

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 = {}) ⇒ Translate

Returns a new instance of Translate.



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

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

  tweak_parameters!
end

Instance Attribute Details

#ignore_tagsObject (readonly)

Returns the value of attribute ignore_tags.



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

def ignore_tags
  @ignore_tags
end

#non_splitting_tagsObject (readonly)

Returns the value of attribute non_splitting_tags.



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

def non_splitting_tags
  @non_splitting_tags
end

#source_langObject (readonly)

Returns the value of attribute source_lang.



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

def source_lang
  @source_lang
end

#splitting_tagsObject (readonly)

Returns the value of attribute splitting_tags.



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

def splitting_tags
  @splitting_tags
end

#target_langObject (readonly)

Returns the value of attribute target_lang.



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

def target_lang
  @target_lang
end

#textObject (readonly)

Returns the value of attribute text.



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

def text
  @text
end

Instance Method Details

#detailsObject



47
48
49
50
# File 'lib/deepl/requests/translate.rb', line 47

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

#requestObject



41
42
43
44
45
# File 'lib/deepl/requests/translate.rb', line 41

def request
  text_arrayified = text.is_a?(Array) ? text : [text]
  payload = { text: text_arrayified, source_lang: source_lang, target_lang: target_lang }
  build_texts(*execute_request_with_retries(post_request(payload)))
end

#to_sObject



52
53
54
# File 'lib/deepl/requests/translate.rb', line 52

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