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
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 = {}) ⇒ Translate
constructor
A new instance of Translate.
-
#request ⇒ Object
rubocop:disable Metrics/MethodLength.
- #to_s ⇒ Object
Constructor Details
#initialize(api, text, source_lang, target_lang, options = {}) ⇒ 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, = {}) super(api, ) @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 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 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 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
58 59 60 61 |
# File 'lib/deepl/requests/translate.rb', line 58 def details "HTTP Headers: #{headers}\nPayload #{{ text: text, source_lang: source_lang, target_lang: target_lang }}" end |
#request ⇒ Object
rubocop:disable Metrics/MethodLength
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/deepl/requests/translate.rb', line 42 def request # rubocop:disable Metrics/MethodLength 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 = 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 build_texts(*execute_request_with_retries(post_request(payload))) end |
#to_s ⇒ Object
63 64 65 |
# File 'lib/deepl/requests/translate.rb', line 63 def to_s "POST #{uri.request_uri}" end |