Class: DeepL::Requests::Translate
- 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
Instance Attribute Summary collapse
-
#custom_instructions ⇒ Object
readonly
Returns the value of attribute custom_instructions.
-
#ignore_tags ⇒ Object
readonly
Returns the value of attribute ignore_tags.
-
#model_type ⇒ Object
readonly
Returns the value of attribute model_type.
-
#non_splitting_tags ⇒ Object
readonly
Returns the value of attribute non_splitting_tags.
-
#source_lang ⇒ Object
readonly
Returns the value of attribute source_lang.
-
#splitting_tags ⇒ Object
readonly
Returns the value of attribute splitting_tags.
-
#tag_handling_version ⇒ Object
readonly
Returns the value of attribute tag_handling_version.
-
#target_lang ⇒ Object
readonly
Returns the value of attribute target_lang.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Attributes inherited from Base
Instance Method Summary collapse
- #details ⇒ Object
-
#initialize(api, text, source_lang, target_lang, options = {}, additional_headers = {}) ⇒ Translate
constructor
A new instance of Translate.
-
#request ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity.
- #to_s ⇒ Object
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, = {}, additional_headers = {}) super(api, , additional_headers) @text = text @source_lang = source_lang @target_lang = target_lang tweak_parameters! end |
Instance Attribute Details
#custom_instructions ⇒ Object (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_tags ⇒ Object (readonly)
Returns the value of attribute ignore_tags.
30 31 32 |
# File 'lib/deepl/requests/translate.rb', line 30 def @ignore_tags end |
#model_type ⇒ Object (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_tags ⇒ Object (readonly)
Returns the value of attribute non_splitting_tags.
30 31 32 |
# File 'lib/deepl/requests/translate.rb', line 30 def @non_splitting_tags end |
#source_lang ⇒ Object (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_tags ⇒ Object (readonly)
Returns the value of attribute splitting_tags.
30 31 32 |
# File 'lib/deepl/requests/translate.rb', line 30 def @splitting_tags end |
#tag_handling_version ⇒ Object (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_lang ⇒ Object (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 |
#text ⇒ Object (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
#details ⇒ Object
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 |
#request ⇒ Object
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_s ⇒ Object
79 80 81 |
# File 'lib/deepl/requests/translate.rb', line 79 def to_s "POST #{uri.request_uri}" end |