deepl-rb 3.9.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +27 -1
- data/CHANGELOG.md +741 -18
- data/Gemfile +4 -0
- data/README.md +1001 -327
- data/Rakefile +29 -7
- data/VERSION +1 -1
- data/deepl-rb.gemspec +75 -88
- data/lib/deepl/admin_client.rb +208 -0
- data/lib/deepl/api.rb +18 -8
- data/lib/deepl/app_info.rb +19 -0
- data/lib/deepl/client.rb +1284 -0
- data/lib/deepl/configuration.rb +27 -32
- data/lib/deepl/constants/admin_usage_group_by.rb +13 -0
- data/lib/deepl/constants/custom_tag_usage_aggregate_by.rb +13 -0
- data/lib/deepl/constants/document_status_code.rb +17 -0
- data/lib/deepl/constants/language_include.rb +17 -0
- data/lib/deepl/constants/language_resource_name.rb +21 -0
- data/lib/deepl/constants/tag_handling_version.rb +14 -0
- data/lib/deepl/exceptions/authorization_failed.rb +1 -1
- data/lib/deepl/exceptions/bad_request.rb +1 -3
- data/lib/deepl/exceptions/connection_error.rb +40 -0
- data/lib/deepl/exceptions/document_not_ready_error.rb +17 -0
- data/lib/deepl/exceptions/document_translation_error.rb +17 -1
- data/lib/deepl/exceptions/error.rb +13 -1
- data/lib/deepl/exceptions/limit_exceeded.rb +1 -1
- data/lib/deepl/exceptions/not_found.rb +8 -3
- data/lib/deepl/exceptions/quota_exceeded.rb +1 -1
- data/lib/deepl/exceptions/request_entity_too_large.rb +1 -2
- data/lib/deepl/exceptions/request_error.rb +26 -2
- data/lib/deepl/exceptions/server_error.rb +9 -2
- data/lib/deepl/exceptions/translation_memory_job_error.rb +32 -0
- data/lib/deepl/exceptions/validation_error.rb +15 -0
- data/lib/deepl/requests/admin/base.rb +94 -0
- data/lib/deepl/requests/admin/create_developer_key.rb +38 -0
- data/lib/deepl/requests/admin/custom_tag_usage_analytics.rb +38 -0
- data/lib/deepl/requests/admin/list_developer_keys.rb +28 -0
- data/lib/deepl/requests/admin/update_developer_key.rb +35 -0
- data/lib/deepl/requests/admin/usage_analytics.rb +36 -0
- data/lib/deepl/requests/base.rb +313 -65
- data/lib/deepl/requests/correct.rb +51 -0
- data/lib/deepl/requests/document/download.rb +11 -3
- data/lib/deepl/requests/document/get_status.rb +2 -2
- data/lib/deepl/requests/document/upload.rb +56 -20
- data/lib/deepl/requests/glossary/base.rb +30 -0
- data/lib/deepl/requests/glossary/create.rb +18 -18
- data/lib/deepl/requests/glossary/destroy.rb +4 -16
- data/lib/deepl/requests/glossary/entries.rb +6 -14
- data/lib/deepl/requests/glossary/find.rb +3 -13
- data/lib/deepl/requests/glossary/language_pairs.rb +22 -6
- data/lib/deepl/requests/glossary/list.rb +2 -2
- data/lib/deepl/requests/language_resources.rb +41 -0
- data/lib/deepl/requests/languages.rb +25 -6
- data/lib/deepl/requests/languages_for_resource.rb +56 -0
- data/lib/deepl/requests/multilingual_glossary/create.rb +45 -0
- data/lib/deepl/requests/multilingual_glossary/destroy.rb +36 -0
- data/lib/deepl/requests/multilingual_glossary/destroy_dictionary.rb +55 -0
- data/lib/deepl/requests/multilingual_glossary/entries.rb +68 -0
- data/lib/deepl/requests/multilingual_glossary/find.rb +35 -0
- data/lib/deepl/requests/multilingual_glossary/helpers.rb +71 -0
- data/lib/deepl/requests/multilingual_glossary/list.rb +39 -0
- data/lib/deepl/requests/multilingual_glossary/replace_dictionary.rb +41 -0
- data/lib/deepl/requests/multilingual_glossary/update.rb +40 -0
- data/lib/deepl/requests/rephrase.rb +8 -10
- data/lib/deepl/requests/spoken_terms/create.rb +42 -0
- data/lib/deepl/requests/spoken_terms/destroy.rb +36 -0
- data/lib/deepl/requests/spoken_terms/destroy_term_list.rb +42 -0
- data/lib/deepl/requests/spoken_terms/entries.rb +47 -0
- data/lib/deepl/requests/spoken_terms/find.rb +35 -0
- data/lib/deepl/requests/spoken_terms/helpers.rb +74 -0
- data/lib/deepl/requests/spoken_terms/list.rb +35 -0
- data/lib/deepl/requests/spoken_terms/replace_term_list.rb +41 -0
- data/lib/deepl/requests/spoken_terms/update.rb +40 -0
- data/lib/deepl/requests/style_profile/base.rb +83 -0
- data/lib/deepl/requests/style_profile/create.rb +40 -0
- data/lib/deepl/requests/style_profile/destroy.rb +34 -0
- data/lib/deepl/requests/style_profile/find.rb +33 -0
- data/lib/deepl/requests/style_profile/list.rb +43 -0
- data/lib/deepl/requests/style_profile/update.rb +35 -0
- data/lib/deepl/requests/style_rule/create.rb +24 -0
- data/lib/deepl/requests/style_rule/create_custom_instruction.rb +11 -2
- data/lib/deepl/requests/style_rule/destroy.rb +4 -2
- data/lib/deepl/requests/style_rule/destroy_custom_instruction.rb +5 -3
- data/lib/deepl/requests/style_rule/find.rb +4 -2
- data/lib/deepl/requests/style_rule/find_custom_instruction.rb +5 -3
- data/lib/deepl/requests/style_rule/helpers.rb +44 -0
- data/lib/deepl/requests/style_rule/list.rb +3 -1
- data/lib/deepl/requests/style_rule/update.rb +5 -2
- data/lib/deepl/requests/style_rule/update_configured_rules.rb +4 -2
- data/lib/deepl/requests/style_rule/update_custom_instruction.rb +7 -3
- data/lib/deepl/requests/translate.rb +43 -27
- data/lib/deepl/requests/translation_memory/base.rb +1 -4
- data/lib/deepl/requests/translation_memory/create_export.rb +7 -2
- data/lib/deepl/requests/translation_memory/create_import.rb +11 -1
- data/lib/deepl/requests/translation_memory/destroy.rb +1 -1
- data/lib/deepl/requests/translation_memory/find.rb +1 -1
- data/lib/deepl/requests/translation_memory/find_job.rb +1 -1
- data/lib/deepl/requests/translation_memory/list.rb +4 -3
- data/lib/deepl/requests/translation_memory/segments.rb +5 -4
- data/lib/deepl/requests/translation_memory/storage_base.rb +37 -9
- data/lib/deepl/requests/translation_memory/upload_file.rb +1 -1
- data/lib/deepl/requests/usage.rb +28 -1
- data/lib/deepl/requests/write_improvements.rb +42 -0
- data/lib/deepl/resources/admin_usage_report.rb +73 -0
- data/lib/deepl/resources/custom_tag_usage_report.rb +59 -0
- data/lib/deepl/resources/developer_key.rb +57 -0
- data/lib/deepl/resources/document_handle.rb +8 -34
- data/lib/deepl/resources/document_translation_status.rb +14 -7
- data/lib/deepl/resources/language.rb +5 -3
- data/lib/deepl/resources/language_resource.rb +57 -0
- data/lib/deepl/resources/language_support.rb +56 -0
- data/lib/deepl/resources/multilingual_glossary.rb +112 -0
- data/lib/deepl/resources/spoken_terms.rb +61 -0
- data/lib/deepl/resources/style_profile.rb +44 -0
- data/lib/deepl/resources/style_rule.rb +4 -23
- data/lib/deepl/resources/text.rb +18 -2
- data/lib/deepl/resources/translation_memory.rb +8 -2
- data/lib/deepl/resources/translation_memory_import.rb +1 -1
- data/lib/deepl/resources/translation_memory_job.rb +26 -7
- data/lib/deepl/resources/translation_memory_segments.rb +8 -6
- data/lib/deepl/resources/usage.rb +61 -5
- data/lib/deepl/utils/client_builder.rb +89 -0
- data/lib/deepl/utils/document_poller.rb +70 -0
- data/lib/deepl/utils/exception_builder.rb +3 -2
- data/lib/deepl/utils/glossary_entries.rb +30 -0
- data/lib/deepl/utils/http_client_builder.rb +116 -0
- data/lib/deepl/utils/number_parser.rb +34 -0
- data/lib/deepl/utils/output_path.rb +25 -0
- data/lib/deepl/utils/term_validator.rb +33 -0
- data/lib/deepl/utils/time_parser.rb +14 -0
- data/lib/deepl/utils/translation_memory_operations.rb +230 -0
- data/lib/deepl.rb +68 -108
- data/lib/http_client_options.rb +21 -1
- data/lib/version.rb +1 -1
- data/upgrading_to_v4.md +374 -0
- metadata +84 -85
- data/.circleci/config.yml +0 -27
- data/.github/workflows/add_issues_to_kanban.yml +0 -16
- data/.gitlab-ci.yml +0 -179
- data/lib/deepl/document_api.rb +0 -131
- data/lib/deepl/exceptions/not_supported.rb +0 -14
- data/lib/deepl/glossary_api.rb +0 -38
- data/lib/deepl/style_rule_api.rb +0 -61
- data/lib/deepl/translation_memory_api.rb +0 -309
- data/license_checker.sh +0 -8
- data/spec/api/api_spec.rb +0 -20
- data/spec/api/configuration_spec.rb +0 -122
- data/spec/api/deepl_spec.rb +0 -262
- data/spec/constants/constants_spec.rb +0 -158
- data/spec/integration_tests/document_api_spec.rb +0 -211
- data/spec/integration_tests/document_error_paths_spec.rb +0 -33
- data/spec/integration_tests/glossary_api_spec.rb +0 -114
- data/spec/integration_tests/glossary_error_paths_spec.rb +0 -107
- data/spec/integration_tests/integration_test_utils.rb +0 -178
- data/spec/integration_tests/languages_api_spec.rb +0 -54
- data/spec/integration_tests/languages_error_paths_spec.rb +0 -25
- data/spec/integration_tests/rephrase_api_spec.rb +0 -90
- data/spec/integration_tests/rephrase_error_paths_spec.rb +0 -53
- data/spec/integration_tests/smoke_test_spec.rb +0 -24
- data/spec/integration_tests/style_rule_api_spec.rb +0 -117
- data/spec/integration_tests/style_rule_error_paths_spec.rb +0 -45
- data/spec/integration_tests/translate_api_spec.rb +0 -117
- data/spec/integration_tests/translate_error_paths_spec.rb +0 -48
- data/spec/integration_tests/translation_memory_api_spec.rb +0 -244
- data/spec/integration_tests/translation_memory_error_paths_spec.rb +0 -87
- data/spec/integration_tests/usage_api_spec.rb +0 -29
- data/spec/integration_tests/usage_error_paths_spec.rb +0 -18
- data/spec/requests/document/upload_spec.rb +0 -130
- data/spec/requests/extra_body_parameters_types_spec.rb +0 -82
- data/spec/requests/glossary/create_spec.rb +0 -44
- data/spec/requests/glossary/destroy_spec.rb +0 -27
- data/spec/requests/glossary/entries_spec.rb +0 -27
- data/spec/requests/glossary/find_spec.rb +0 -28
- data/spec/requests/glossary/language_pairs_spec.rb +0 -27
- data/spec/requests/glossary/list_spec.rb +0 -27
- data/spec/requests/languages_spec.rb +0 -27
- data/spec/requests/rephrase_spec.rb +0 -46
- data/spec/requests/style_rule/create_custom_instruction_spec.rb +0 -30
- data/spec/requests/style_rule/create_spec.rb +0 -29
- data/spec/requests/style_rule/destroy_custom_instruction_spec.rb +0 -28
- data/spec/requests/style_rule/destroy_spec.rb +0 -27
- data/spec/requests/style_rule/find_custom_instruction_spec.rb +0 -29
- data/spec/requests/style_rule/find_spec.rb +0 -28
- data/spec/requests/style_rule/list_spec.rb +0 -27
- data/spec/requests/style_rule/update_configured_rules_spec.rb +0 -31
- data/spec/requests/style_rule/update_custom_instruction_spec.rb +0 -32
- data/spec/requests/style_rule/update_spec.rb +0 -29
- data/spec/requests/translate_spec.rb +0 -338
- data/spec/requests/translation_memory/create_export_spec.rb +0 -37
- data/spec/requests/translation_memory/create_import_spec.rb +0 -61
- data/spec/requests/translation_memory/destroy_spec.rb +0 -36
- data/spec/requests/translation_memory/download_export_spec.rb +0 -54
- data/spec/requests/translation_memory/find_job_spec.rb +0 -37
- data/spec/requests/translation_memory/find_spec.rb +0 -36
- data/spec/requests/translation_memory/list_spec.rb +0 -27
- data/spec/requests/translation_memory/segments_spec.rb +0 -58
- data/spec/requests/translation_memory/upload_file_spec.rb +0 -55
- data/spec/requests/usage_spec.rb +0 -27
- data/spec/resources/custom_instruction_spec.rb +0 -32
- data/spec/resources/glossary_spec.rb +0 -38
- data/spec/resources/language_pair_spec.rb +0 -23
- data/spec/resources/language_spec.rb +0 -45
- data/spec/resources/style_rule_spec.rb +0 -68
- data/spec/resources/text_spec.rb +0 -23
- data/spec/resources/translation_memory_export_spec.rb +0 -38
- data/spec/resources/translation_memory_import_spec.rb +0 -30
- data/spec/resources/translation_memory_job_spec.rb +0 -109
- data/spec/resources/translation_memory_segments_spec.rb +0 -78
- data/spec/resources/translation_memory_spec.rb +0 -52
- data/spec/resources/usage_spec.rb +0 -35
- data/spec/spec_helper.rb +0 -62
- data/spec/support/live_mock_server.rb +0 -12
- data/spec/support/managed_glossary.rb +0 -65
- data/spec/support/managed_style_rule.rb +0 -17
- data/spec/support/managed_translation_memory.rb +0 -55
|
@@ -22,7 +22,7 @@ module DeepL
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
def details
|
|
25
|
-
"HTTP Headers: #{
|
|
25
|
+
"HTTP Headers: #{loggable_headers}\nPayload #{{ document_key: document_key }}"
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
def to_s
|
|
@@ -31,12 +31,20 @@ module DeepL
|
|
|
31
31
|
|
|
32
32
|
private
|
|
33
33
|
|
|
34
|
+
# Binary mode: text mode would convert newlines on Windows and transcode the bytes when
|
|
35
|
+
# Encoding.default_internal is set.
|
|
34
36
|
def extract_file(_request, response)
|
|
35
|
-
File.
|
|
37
|
+
File.binwrite(@output_file, response.body)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def validate_response!(response, request = nil)
|
|
41
|
+
raise Exceptions::DocumentNotReadyError.new(response, request) if response.code == '503'
|
|
42
|
+
|
|
43
|
+
super
|
|
36
44
|
end
|
|
37
45
|
|
|
38
46
|
def path
|
|
39
|
-
"document/#{document_id}/result"
|
|
47
|
+
"document/#{escape_path_segment(document_id)}/result"
|
|
40
48
|
end
|
|
41
49
|
end
|
|
42
50
|
end
|
|
@@ -21,7 +21,7 @@ module DeepL
|
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def details
|
|
24
|
-
"HTTP Headers: #{
|
|
24
|
+
"HTTP Headers: #{loggable_headers}\nPayload #{{ document_key: document_key }}"
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
def to_s
|
|
@@ -36,7 +36,7 @@ module DeepL
|
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
def path
|
|
39
|
-
"document/#{document_id}"
|
|
39
|
+
"document/#{escape_path_segment(document_id)}"
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
end
|
|
@@ -6,10 +6,12 @@
|
|
|
6
6
|
module DeepL
|
|
7
7
|
module Requests
|
|
8
8
|
module Document
|
|
9
|
-
class Upload < Base
|
|
9
|
+
class Upload < Base # rubocop:disable Metrics/ClassLength
|
|
10
10
|
attr_reader :input_file_path, :source_lang, :target_lang, :filename
|
|
11
11
|
|
|
12
|
-
SUPPORTED_OPTIONS = %w[formality
|
|
12
|
+
SUPPORTED_OPTIONS = %w[formality output_format enable_watermark].freeze
|
|
13
|
+
accepts_options(*SUPPORTED_OPTIONS, :glossary, :glossary_ids, :style_rule, :style_profile,
|
|
14
|
+
:translation_memory, :translation_memory_threshold)
|
|
13
15
|
|
|
14
16
|
def initialize(api, input_file_path, source_lang, target_lang, filename = nil, # rubocop:disable Metrics/ParameterLists
|
|
15
17
|
options = {}, additional_headers = {})
|
|
@@ -18,6 +20,9 @@ module DeepL
|
|
|
18
20
|
@source_lang = source_lang
|
|
19
21
|
@target_lang = target_lang
|
|
20
22
|
@filename = filename
|
|
23
|
+
validate_languages!(source_lang, target_lang)
|
|
24
|
+
validate_translation_memory_threshold!
|
|
25
|
+
validate_glossary_source_lang!
|
|
21
26
|
end
|
|
22
27
|
|
|
23
28
|
def request
|
|
@@ -30,7 +35,7 @@ module DeepL
|
|
|
30
35
|
end
|
|
31
36
|
|
|
32
37
|
def details
|
|
33
|
-
"HTTP Headers: #{
|
|
38
|
+
"HTTP Headers: #{loggable_headers}\nPayload #{[
|
|
34
39
|
['file', "File at #{input_file_path} opened in binary mode"],
|
|
35
40
|
['source_lang', source_lang], ['target_lang', target_lang], ['filename', filename]
|
|
36
41
|
]}"
|
|
@@ -42,11 +47,22 @@ module DeepL
|
|
|
42
47
|
|
|
43
48
|
private
|
|
44
49
|
|
|
50
|
+
# Text translation detects the source language for glossaries; documents do not.
|
|
51
|
+
def validate_glossary_source_lang!
|
|
52
|
+
return unless option?(:glossary) && source_lang.nil?
|
|
53
|
+
|
|
54
|
+
raise ArgumentError, 'The `glossary` option requires `source_lang` to be set'
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# A repeated creation request could create a second resource.
|
|
58
|
+
def retry_only_unprocessed?
|
|
59
|
+
true
|
|
60
|
+
end
|
|
61
|
+
|
|
45
62
|
def build_base_form_data(input_file)
|
|
46
|
-
form_data = [
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
]
|
|
63
|
+
form_data = [['file', input_file]]
|
|
64
|
+
form_data.push(['source_lang', source_lang]) unless source_lang.nil?
|
|
65
|
+
form_data.push(['target_lang', target_lang])
|
|
50
66
|
filename_param = filename || File.basename(input_file_path)
|
|
51
67
|
form_data.push(['filename', filename_param]) unless filename_param.nil?
|
|
52
68
|
add_extra_options_to_form(form_data)
|
|
@@ -56,7 +72,9 @@ module DeepL
|
|
|
56
72
|
def add_extra_options_to_form(form_data)
|
|
57
73
|
add_supported_options_to_form(form_data)
|
|
58
74
|
add_glossary_ids_to_form(form_data)
|
|
75
|
+
add_glossary_to_form(form_data)
|
|
59
76
|
add_style_rule_to_form(form_data)
|
|
77
|
+
add_style_profile_to_form(form_data)
|
|
60
78
|
add_translation_memory_to_form(form_data)
|
|
61
79
|
add_translation_memory_threshold_to_form(form_data)
|
|
62
80
|
end
|
|
@@ -64,21 +82,38 @@ module DeepL
|
|
|
64
82
|
# Validates and serializes the `glossary_ids` option (see
|
|
65
83
|
# `DeepL::Requests::Base#build_glossary_ids_param`) and appends it to the form data as a
|
|
66
84
|
# comma-separated `glossary_ids` field. Requires `source_lang`, cannot be combined with
|
|
67
|
-
# the singular `
|
|
85
|
+
# the singular `glossary` option, and allows at most 5 IDs.
|
|
68
86
|
def add_glossary_ids_to_form(form_data)
|
|
69
|
-
glossary_ids = build_glossary_ids_param
|
|
87
|
+
glossary_ids = build_glossary_ids_param
|
|
88
|
+
# Text translation detects the source language for glossaries; documents do not.
|
|
89
|
+
if !glossary_ids.nil? && source_lang.nil?
|
|
90
|
+
raise ArgumentError, 'The `glossary_ids` option requires `source_lang` to be set'
|
|
91
|
+
end
|
|
92
|
+
|
|
70
93
|
form_data.push(['glossary_ids', glossary_ids.join(',')]) unless glossary_ids.nil?
|
|
71
94
|
end
|
|
72
95
|
|
|
96
|
+
# Resolves the `glossary` option (an ID string, a `DeepL::Resources::Glossary` or a
|
|
97
|
+
# `DeepL::Resources::MultilingualGlossary`) and appends it as a `glossary_id` field.
|
|
98
|
+
def add_glossary_to_form(form_data)
|
|
99
|
+
glossary_id = build_glossary_param
|
|
100
|
+
form_data.push(['glossary_id', glossary_id]) unless glossary_id.nil?
|
|
101
|
+
end
|
|
102
|
+
|
|
73
103
|
# Serializes the `style_rule` option and appends it to the form data as a `style_id`
|
|
74
104
|
# field. Mirrors text translation: accepts either a style rule ID string or a
|
|
75
105
|
# `DeepL::Resources::StyleRule` object.
|
|
76
106
|
def add_style_rule_to_form(form_data)
|
|
77
|
-
|
|
107
|
+
style_id = take_resource_option(:style_rule, [DeepL::Resources::StyleRule], :style_id)
|
|
108
|
+
form_data.push(['style_id', style_id]) unless style_id.nil?
|
|
109
|
+
end
|
|
78
110
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
111
|
+
# Serializes the `style_profile` option (a style profile ID string or a
|
|
112
|
+
# `DeepL::Resources::StyleProfile` object) as a `style_profile_id` field.
|
|
113
|
+
def add_style_profile_to_form(form_data)
|
|
114
|
+
profile_id = take_resource_option(:style_profile, [DeepL::Resources::StyleProfile],
|
|
115
|
+
:style_profile_id)
|
|
116
|
+
form_data.push(['style_profile_id', profile_id]) unless profile_id.nil?
|
|
82
117
|
end
|
|
83
118
|
|
|
84
119
|
# Serializes the `translation_memory` option and appends it to the form data as a
|
|
@@ -86,11 +121,9 @@ module DeepL
|
|
|
86
121
|
# option accepts either a translation memory ID string or a
|
|
87
122
|
# `DeepL::Resources::TranslationMemory` object.
|
|
88
123
|
def add_translation_memory_to_form(form_data)
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
tm_id = tm.is_a?(DeepL::Resources::TranslationMemory) ? tm.translation_memory_id : tm
|
|
93
|
-
form_data.push(['translation_memory_id', tm_id.to_s])
|
|
124
|
+
tm_id = take_resource_option(:translation_memory, [DeepL::Resources::TranslationMemory],
|
|
125
|
+
:translation_memory_id)
|
|
126
|
+
form_data.push(['translation_memory_id', tm_id]) unless tm_id.nil?
|
|
94
127
|
end
|
|
95
128
|
|
|
96
129
|
# Serializes the `translation_memory_threshold` option (integer 0-100) and appends it to
|
|
@@ -99,20 +132,23 @@ module DeepL
|
|
|
99
132
|
return unless option?(:translation_memory_threshold)
|
|
100
133
|
|
|
101
134
|
threshold = delete_option(:translation_memory_threshold)
|
|
135
|
+
return if threshold.nil?
|
|
136
|
+
|
|
102
137
|
form_data.push(['translation_memory_threshold', threshold.to_s])
|
|
103
138
|
end
|
|
104
139
|
|
|
105
140
|
def add_supported_options_to_form(form_data)
|
|
106
141
|
SUPPORTED_OPTIONS.each do |option_name|
|
|
107
142
|
option_value = option(option_name)
|
|
108
|
-
form_data.push([option_name, option_value]) unless option_value.nil?
|
|
143
|
+
form_data.push([option_name, option_value.to_s]) unless option_value.nil?
|
|
109
144
|
end
|
|
110
145
|
end
|
|
111
146
|
|
|
112
147
|
def build_doc_handle(request, response)
|
|
113
148
|
parsed_response = JSON.parse(response.body)
|
|
114
149
|
Resources::DocumentHandle.new(parsed_response['document_id'],
|
|
115
|
-
parsed_response['document_key'], request, response
|
|
150
|
+
parsed_response['document_key'], request, response,
|
|
151
|
+
api: api)
|
|
116
152
|
end
|
|
117
153
|
|
|
118
154
|
def path
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Copyright 2026 DeepL SE (https://www.deepl.com)
|
|
2
|
+
# Use of this source code is governed by an MIT
|
|
3
|
+
# license that can be found in the LICENSE.md file.
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
module DeepL
|
|
7
|
+
module Requests
|
|
8
|
+
module Glossary
|
|
9
|
+
# A request for one v2 glossary, given as its ID or a Resources::Glossary.
|
|
10
|
+
class Base < DeepL::Requests::Base
|
|
11
|
+
attr_reader :id
|
|
12
|
+
|
|
13
|
+
def initialize(api, glossary)
|
|
14
|
+
super(api, {})
|
|
15
|
+
@id = glossary.is_a?(DeepL::Resources::Glossary) ? glossary.id : glossary.to_s
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def to_s
|
|
19
|
+
"#{self.class::METHOD} #{uri.request_uri}"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def glossary_path(suffix = nil)
|
|
25
|
+
["glossaries/#{escape_path_segment(id)}", suffix].compact.join('/')
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -6,31 +6,28 @@
|
|
|
6
6
|
module DeepL
|
|
7
7
|
module Requests
|
|
8
8
|
module Glossary
|
|
9
|
-
|
|
9
|
+
# Creates a v2 glossary from entries that are already encoded as TSV or CSV.
|
|
10
|
+
class Create < DeepL::Requests::Base
|
|
10
11
|
attr_reader :name, :source_lang, :target_lang, :entries, :entries_format
|
|
11
12
|
|
|
12
|
-
def initialize(api, name, source_lang, target_lang, entries, options = {})
|
|
13
|
+
def initialize(api, name, source_lang, target_lang, entries, entries_format, options = {}) # rubocop:disable Metrics/ParameterLists
|
|
13
14
|
super(api, options)
|
|
15
|
+
raise ArgumentError, 'Glossary name must not be empty' if name.to_s.empty?
|
|
16
|
+
|
|
14
17
|
@name = name
|
|
15
|
-
|
|
16
|
-
@
|
|
18
|
+
# v2 glossaries are not regional: EN-GB is sent as EN.
|
|
19
|
+
@source_lang = source_lang.to_s.split('-').first.upcase
|
|
20
|
+
@target_lang = target_lang.to_s.split('-').first.upcase
|
|
17
21
|
@entries = entries
|
|
18
|
-
@entries_format =
|
|
22
|
+
@entries_format = entries_format
|
|
19
23
|
end
|
|
20
24
|
|
|
21
25
|
def request
|
|
22
|
-
payload = {
|
|
23
|
-
name: name, source_lang: source_lang, target_lang: target_lang, entries: entries_to_tsv,
|
|
24
|
-
entries_format: entries_format
|
|
25
|
-
}
|
|
26
26
|
build_glossary(*execute_request_with_retries(post_request(payload)))
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def details
|
|
30
|
-
"HTTP Headers: #{
|
|
31
|
-
name: name, source_lang: source_lang, target_lang: target_lang, entries: entries_to_tsv,
|
|
32
|
-
entries_format: entries_format
|
|
33
|
-
}}"
|
|
30
|
+
"HTTP Headers: #{loggable_headers}\nPayload #{payload}"
|
|
34
31
|
end
|
|
35
32
|
|
|
36
33
|
def to_s
|
|
@@ -39,15 +36,18 @@ module DeepL
|
|
|
39
36
|
|
|
40
37
|
private
|
|
41
38
|
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
# A repeated creation request could create a second resource.
|
|
40
|
+
def retry_only_unprocessed?
|
|
41
|
+
true
|
|
42
|
+
end
|
|
44
43
|
|
|
45
|
-
|
|
44
|
+
def payload
|
|
45
|
+
{ name: name, source_lang: source_lang, target_lang: target_lang, entries: entries,
|
|
46
|
+
entries_format: entries_format }
|
|
46
47
|
end
|
|
47
48
|
|
|
48
49
|
def build_glossary(request, response)
|
|
49
|
-
|
|
50
|
-
Resources::Glossary.new(glossary, request, response)
|
|
50
|
+
Resources::Glossary.new(JSON.parse(response.body), request, response)
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
def path
|
|
@@ -7,29 +7,17 @@ module DeepL
|
|
|
7
7
|
module Requests
|
|
8
8
|
module Glossary
|
|
9
9
|
class Destroy < Base
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def initialize(api, id, options = {})
|
|
13
|
-
super(api, options)
|
|
14
|
-
@id = id
|
|
15
|
-
end
|
|
10
|
+
METHOD = 'DELETE'
|
|
16
11
|
|
|
17
12
|
def request
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def to_s
|
|
22
|
-
"DELETE #{uri.request_uri}"
|
|
13
|
+
execute_request_with_retries(delete_request)
|
|
14
|
+
nil
|
|
23
15
|
end
|
|
24
16
|
|
|
25
17
|
private
|
|
26
18
|
|
|
27
|
-
def build_response(_, _)
|
|
28
|
-
id
|
|
29
|
-
end
|
|
30
|
-
|
|
31
19
|
def path
|
|
32
|
-
|
|
20
|
+
glossary_path
|
|
33
21
|
end
|
|
34
22
|
end
|
|
35
23
|
end
|
|
@@ -7,29 +7,21 @@ module DeepL
|
|
|
7
7
|
module Requests
|
|
8
8
|
module Glossary
|
|
9
9
|
class Entries < Base
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def initialize(api, id, options = {})
|
|
13
|
-
super(api, options)
|
|
14
|
-
@id = id
|
|
15
|
-
end
|
|
10
|
+
METHOD = 'GET'
|
|
16
11
|
|
|
17
12
|
def request
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def to_s
|
|
22
|
-
"GET #{uri.request_uri}"
|
|
13
|
+
_, response = execute_request_with_retries(get_request)
|
|
14
|
+
Utils::GlossaryEntries.from_tsv(response.body)
|
|
23
15
|
end
|
|
24
16
|
|
|
25
17
|
private
|
|
26
18
|
|
|
27
|
-
def
|
|
28
|
-
|
|
19
|
+
def get_request # rubocop:disable Naming/AccessorMethodName
|
|
20
|
+
Net::HTTP::Get.new(uri.path, headers.merge('Accept' => 'text/tab-separated-values'))
|
|
29
21
|
end
|
|
30
22
|
|
|
31
23
|
def path
|
|
32
|
-
|
|
24
|
+
glossary_path('entries')
|
|
33
25
|
end
|
|
34
26
|
end
|
|
35
27
|
end
|
|
@@ -7,30 +7,20 @@ module DeepL
|
|
|
7
7
|
module Requests
|
|
8
8
|
module Glossary
|
|
9
9
|
class Find < Base
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def initialize(api, id, options = {})
|
|
13
|
-
super(api, options)
|
|
14
|
-
@id = id
|
|
15
|
-
end
|
|
10
|
+
METHOD = 'GET'
|
|
16
11
|
|
|
17
12
|
def request
|
|
18
13
|
build_glossary(*execute_request_with_retries(get_request))
|
|
19
14
|
end
|
|
20
15
|
|
|
21
|
-
def to_s
|
|
22
|
-
"GET #{uri.request_uri}"
|
|
23
|
-
end
|
|
24
|
-
|
|
25
16
|
private
|
|
26
17
|
|
|
27
18
|
def build_glossary(request, response)
|
|
28
|
-
|
|
29
|
-
Resources::Glossary.new(glossary, request, response)
|
|
19
|
+
Resources::Glossary.new(JSON.parse(response.body), request, response)
|
|
30
20
|
end
|
|
31
21
|
|
|
32
22
|
def path
|
|
33
|
-
|
|
23
|
+
glossary_path
|
|
34
24
|
end
|
|
35
25
|
end
|
|
36
26
|
end
|
|
@@ -7,8 +7,8 @@ module DeepL
|
|
|
7
7
|
module Requests
|
|
8
8
|
module Glossary
|
|
9
9
|
class LanguagePairs < Base
|
|
10
|
-
def initialize(api
|
|
11
|
-
super
|
|
10
|
+
def initialize(api)
|
|
11
|
+
super(api, {})
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
def request
|
|
@@ -23,14 +23,30 @@ module DeepL
|
|
|
23
23
|
|
|
24
24
|
def build_language_pair_list(request, response)
|
|
25
25
|
data = JSON.parse(response.body)
|
|
26
|
-
data
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
sources = langs_where(data, 'usable_as_source')
|
|
27
|
+
targets = langs_where(data, 'usable_as_target')
|
|
28
|
+
|
|
29
|
+
sources.product(targets).each_with_object([]) do |(source, target), pairs|
|
|
30
|
+
next if source == target
|
|
31
|
+
|
|
32
|
+
pairs << Resources::LanguagePair.new(source, target, request, response)
|
|
29
33
|
end
|
|
30
34
|
end
|
|
31
35
|
|
|
36
|
+
def langs_where(data, usable_key)
|
|
37
|
+
data.select { |language| language[usable_key] }.map { |language| language['lang'] }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def uri
|
|
41
|
+
@uri ||= URI("#{host}/v3/#{path}?resource=glossary")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def get_request # rubocop:disable Naming/AccessorMethodName
|
|
45
|
+
Net::HTTP::Get.new(uri.request_uri, add_json_content_type(headers))
|
|
46
|
+
end
|
|
47
|
+
|
|
32
48
|
def path
|
|
33
|
-
'
|
|
49
|
+
'languages'
|
|
34
50
|
end
|
|
35
51
|
end
|
|
36
52
|
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Copyright 2026 DeepL SE (https://www.deepl.com)
|
|
2
|
+
# Use of this source code is governed by an MIT
|
|
3
|
+
# license that can be found in the LICENSE file.
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
module DeepL
|
|
7
|
+
module Requests
|
|
8
|
+
class LanguageResources < Base
|
|
9
|
+
def initialize(api, options = {})
|
|
10
|
+
super
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def request
|
|
14
|
+
build_language_resource_list(*execute_request_with_retries(get_request))
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def to_s
|
|
18
|
+
"GET #{uri.request_uri}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def build_language_resource_list(request, response)
|
|
24
|
+
data = JSON.parse(response.body)
|
|
25
|
+
data.map { |resource| Resources::LanguageResource.new(resource, request, response) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def uri
|
|
29
|
+
@uri ||= URI("#{host}/v3/#{path}")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def get_request # rubocop:disable Naming/AccessorMethodName
|
|
33
|
+
Net::HTTP::Get.new(uri.request_uri, add_json_content_type(headers))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def path
|
|
37
|
+
'languages/resources'
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -6,8 +6,10 @@
|
|
|
6
6
|
module DeepL
|
|
7
7
|
module Requests
|
|
8
8
|
class Languages < Base
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
# +type+ is +:source+ or +:target+.
|
|
10
|
+
def initialize(api, type)
|
|
11
|
+
super(api, {})
|
|
12
|
+
@type = type
|
|
11
13
|
end
|
|
12
14
|
|
|
13
15
|
def request
|
|
@@ -20,15 +22,32 @@ module DeepL
|
|
|
20
22
|
|
|
21
23
|
private
|
|
22
24
|
|
|
25
|
+
def target?
|
|
26
|
+
@type == :target
|
|
27
|
+
end
|
|
28
|
+
|
|
23
29
|
def build_languages(request, response)
|
|
24
30
|
data = JSON.parse(response.body)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
request, response)
|
|
31
|
+
usable_key = target? ? 'usable_as_target' : 'usable_as_source'
|
|
32
|
+
data.select { |language| language[usable_key] }.map do |language|
|
|
33
|
+
build_language(language, request, response)
|
|
29
34
|
end
|
|
30
35
|
end
|
|
31
36
|
|
|
37
|
+
def build_language(language, request, response)
|
|
38
|
+
supports_formality = target? ? language.fetch('features', {}).to_h.key?('formality') : nil
|
|
39
|
+
Resources::Language.new(language['lang'].upcase, language['name'], supports_formality,
|
|
40
|
+
request, response)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def uri
|
|
44
|
+
@uri ||= URI("#{host}/v3/#{path}?resource=translate_text")
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def get_request # rubocop:disable Naming/AccessorMethodName
|
|
48
|
+
Net::HTTP::Get.new(uri.request_uri, add_json_content_type(headers))
|
|
49
|
+
end
|
|
50
|
+
|
|
32
51
|
def path
|
|
33
52
|
'languages'
|
|
34
53
|
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Copyright 2026 DeepL SE (https://www.deepl.com)
|
|
2
|
+
# Use of this source code is governed by an MIT
|
|
3
|
+
# license that can be found in the LICENSE file.
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
module DeepL
|
|
7
|
+
module Requests
|
|
8
|
+
class LanguagesForResource < Base
|
|
9
|
+
accepts_options :include
|
|
10
|
+
|
|
11
|
+
def initialize(api, resource, options = {})
|
|
12
|
+
super(api, options)
|
|
13
|
+
@resource = resource.to_s
|
|
14
|
+
raise ArgumentError, 'resource must not be empty' if @resource.empty?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def request
|
|
18
|
+
build_language_support_list(*execute_request_with_retries(get_request))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def to_s
|
|
22
|
+
"GET #{uri.request_uri}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def build_language_support_list(request, response)
|
|
28
|
+
data = JSON.parse(response.body)
|
|
29
|
+
data.map { |language| Resources::LanguageSupport.new(language, request, response) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def uri
|
|
33
|
+
@uri ||= begin
|
|
34
|
+
base_uri = URI("#{host}/v3/#{path}")
|
|
35
|
+
base_uri.query = build_query_string
|
|
36
|
+
base_uri
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# include is sent as repeated include=<value> parameters
|
|
41
|
+
def build_query_string
|
|
42
|
+
params = [['resource', @resource]]
|
|
43
|
+
Array(option(:include)).each { |value| params << ['include', value.to_s] }
|
|
44
|
+
encode_query(params)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def get_request # rubocop:disable Naming/AccessorMethodName
|
|
48
|
+
Net::HTTP::Get.new(uri.request_uri, add_json_content_type(headers))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def path
|
|
52
|
+
'languages'
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Copyright 2026 DeepL SE (https://www.deepl.com)
|
|
2
|
+
# Use of this source code is governed by an MIT
|
|
3
|
+
# license that can be found in the LICENSE.md file.
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
module DeepL
|
|
7
|
+
module Requests
|
|
8
|
+
module MultilingualGlossary
|
|
9
|
+
class Create < Base
|
|
10
|
+
include Helpers
|
|
11
|
+
|
|
12
|
+
attr_reader :name, :dictionaries
|
|
13
|
+
|
|
14
|
+
def initialize(api, name, dictionaries, options = {})
|
|
15
|
+
super(api, options)
|
|
16
|
+
raise ArgumentError, 'Glossary name must not be empty' if name.to_s.empty?
|
|
17
|
+
raise ArgumentError, 'Glossary needs at least one dictionary' if dictionaries.to_a.empty?
|
|
18
|
+
|
|
19
|
+
@name = name
|
|
20
|
+
@dictionaries = dictionaries.map { |dictionary| dictionary_payload(dictionary) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def request
|
|
24
|
+
payload = { name: name, dictionaries: dictionaries }
|
|
25
|
+
build_glossary(*execute_request_with_retries(post_request(payload)))
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def to_s
|
|
29
|
+
"POST #{uri.request_uri}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
# A repeated creation request could create a second resource.
|
|
35
|
+
def retry_only_unprocessed?
|
|
36
|
+
true
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def path
|
|
40
|
+
'glossaries'
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|