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
|
@@ -7,6 +7,8 @@ module DeepL
|
|
|
7
7
|
module Requests
|
|
8
8
|
module StyleRule
|
|
9
9
|
class List < Base
|
|
10
|
+
accepts_options :page, :page_size, :detailed
|
|
11
|
+
|
|
10
12
|
def initialize(api, options = {})
|
|
11
13
|
super
|
|
12
14
|
end
|
|
@@ -47,7 +49,7 @@ module DeepL
|
|
|
47
49
|
params['page'] = option(:page).to_s if option?(:page)
|
|
48
50
|
params['page_size'] = option(:page_size).to_s if option?(:page_size)
|
|
49
51
|
params['detailed'] = option(:detailed).to_s.downcase if option?(:detailed)
|
|
50
|
-
|
|
52
|
+
encode_query(params)
|
|
51
53
|
end
|
|
52
54
|
|
|
53
55
|
def path
|
|
@@ -7,9 +7,12 @@ module DeepL
|
|
|
7
7
|
module Requests
|
|
8
8
|
module StyleRule
|
|
9
9
|
class Update < Base
|
|
10
|
+
include Helpers
|
|
11
|
+
|
|
10
12
|
def initialize(api, style_id, name, options = {})
|
|
11
13
|
super(api, options)
|
|
12
|
-
@style_id = style_id
|
|
14
|
+
@style_id = style_rule_id(style_id)
|
|
15
|
+
check_non_empty!(name: name)
|
|
13
16
|
@name = name
|
|
14
17
|
end
|
|
15
18
|
|
|
@@ -34,7 +37,7 @@ module DeepL
|
|
|
34
37
|
end
|
|
35
38
|
|
|
36
39
|
def path
|
|
37
|
-
|
|
40
|
+
style_rule_path(@style_id)
|
|
38
41
|
end
|
|
39
42
|
end
|
|
40
43
|
end
|
|
@@ -7,9 +7,11 @@ module DeepL
|
|
|
7
7
|
module Requests
|
|
8
8
|
module StyleRule
|
|
9
9
|
class UpdateConfiguredRules < Base
|
|
10
|
+
include Helpers
|
|
11
|
+
|
|
10
12
|
def initialize(api, style_id, configured_rules, options = {})
|
|
11
13
|
super(api, options)
|
|
12
|
-
@style_id = style_id
|
|
14
|
+
@style_id = style_rule_id(style_id)
|
|
13
15
|
@configured_rules = configured_rules
|
|
14
16
|
end
|
|
15
17
|
|
|
@@ -33,7 +35,7 @@ module DeepL
|
|
|
33
35
|
end
|
|
34
36
|
|
|
35
37
|
def path
|
|
36
|
-
|
|
38
|
+
style_rule_path(@style_id, 'configured_rules')
|
|
37
39
|
end
|
|
38
40
|
end
|
|
39
41
|
end
|
|
@@ -7,13 +7,17 @@ module DeepL
|
|
|
7
7
|
module Requests
|
|
8
8
|
module StyleRule
|
|
9
9
|
class UpdateCustomInstruction < Base
|
|
10
|
+
include Helpers
|
|
11
|
+
|
|
10
12
|
def initialize(api, style_id, instruction_id, label, prompt, # rubocop:disable Metrics/ParameterLists
|
|
11
13
|
source_language = nil, options = {})
|
|
12
14
|
super(api, options)
|
|
13
|
-
@style_id = style_id
|
|
14
|
-
@instruction_id = instruction_id
|
|
15
|
+
@style_id = style_rule_id(style_id)
|
|
16
|
+
@instruction_id = custom_instruction_id(instruction_id)
|
|
17
|
+
check_non_empty!(label: label, prompt: prompt)
|
|
15
18
|
@label = label
|
|
16
19
|
@prompt = prompt
|
|
20
|
+
validate_language!('source_language', source_language, optional: true)
|
|
17
21
|
@source_language = source_language
|
|
18
22
|
end
|
|
19
23
|
|
|
@@ -39,7 +43,7 @@ module DeepL
|
|
|
39
43
|
end
|
|
40
44
|
|
|
41
45
|
def path
|
|
42
|
-
|
|
46
|
+
style_rule_path(@style_id, 'custom_instructions', @instruction_id)
|
|
43
47
|
end
|
|
44
48
|
end
|
|
45
49
|
end
|
|
@@ -17,6 +17,10 @@ module DeepL
|
|
|
17
17
|
(value.is_a?(Array) ? value : value.split(','))
|
|
18
18
|
end
|
|
19
19
|
}.freeze
|
|
20
|
+
# A custom instruction may contain commas, so a single string is one instruction.
|
|
21
|
+
STRING_TO_SINGLE_ELEMENT_ARRAY_CONVERSION = lambda { |value|
|
|
22
|
+
value.is_a?(String) ? [value] : value
|
|
23
|
+
}.freeze
|
|
20
24
|
OPTIONS_CONVERSIONS = {
|
|
21
25
|
split_sentences: BOOLEAN_TO_STRING_CONVERSION,
|
|
22
26
|
preserve_formatting: STRING_TO_BOOLEAN_CONVERSION,
|
|
@@ -24,9 +28,15 @@ module DeepL
|
|
|
24
28
|
splitting_tags: STRING_TO_ARRAY_CONVERSION,
|
|
25
29
|
non_splitting_tags: STRING_TO_ARRAY_CONVERSION,
|
|
26
30
|
ignore_tags: STRING_TO_ARRAY_CONVERSION,
|
|
27
|
-
custom_instructions:
|
|
31
|
+
custom_instructions: STRING_TO_SINGLE_ELEMENT_ARRAY_CONVERSION
|
|
28
32
|
}.freeze
|
|
29
33
|
|
|
34
|
+
accepts_options :glossary, :glossary_ids, :style_rule, :style_profile, :translation_memory,
|
|
35
|
+
:translation_memory_threshold, :formality, :split_sentences,
|
|
36
|
+
:preserve_formatting, :context, :tag_handling, :tag_handling_version,
|
|
37
|
+
:outline_detection, :splitting_tags, :non_splitting_tags, :ignore_tags,
|
|
38
|
+
:model_type, :custom_instructions
|
|
39
|
+
|
|
30
40
|
attr_reader :text, :source_lang, :target_lang, :ignore_tags, :splitting_tags,
|
|
31
41
|
:non_splitting_tags, :model_type, :custom_instructions, :tag_handling_version
|
|
32
42
|
|
|
@@ -35,45 +45,43 @@ module DeepL
|
|
|
35
45
|
@text = text
|
|
36
46
|
@source_lang = source_lang
|
|
37
47
|
@target_lang = target_lang
|
|
48
|
+
validate_text!(text)
|
|
49
|
+
validate_languages!(source_lang, target_lang)
|
|
50
|
+
validate_translation_memory_threshold!
|
|
38
51
|
|
|
39
52
|
tweak_parameters!
|
|
40
53
|
end
|
|
41
54
|
|
|
42
55
|
def request # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
|
|
43
56
|
text_arrayified = text.is_a?(Array) ? text : [text]
|
|
44
|
-
payload = { text: text_arrayified,
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if option?(:translation_memory)
|
|
56
|
-
tm = delete_option(:translation_memory)
|
|
57
|
-
payload[:translation_memory_id] = if tm.is_a?(DeepL::Resources::TranslationMemory)
|
|
58
|
-
tm.translation_memory_id
|
|
59
|
-
else
|
|
60
|
-
tm.to_s
|
|
61
|
-
end
|
|
62
|
-
end
|
|
57
|
+
payload = { text: text_arrayified, target_lang: target_lang, show_billed_characters: true }
|
|
58
|
+
payload[:source_lang] = source_lang unless source_lang.nil?
|
|
59
|
+
|
|
60
|
+
style_id = take_resource_option(:style_rule, [DeepL::Resources::StyleRule], :style_id)
|
|
61
|
+
payload[:style_id] = style_id unless style_id.nil?
|
|
62
|
+
profile_id = take_resource_option(:style_profile, [DeepL::Resources::StyleProfile],
|
|
63
|
+
:style_profile_id)
|
|
64
|
+
payload[:style_profile_id] = profile_id unless profile_id.nil?
|
|
65
|
+
tm_id = take_resource_option(:translation_memory, [DeepL::Resources::TranslationMemory],
|
|
66
|
+
:translation_memory_id)
|
|
67
|
+
payload[:translation_memory_id] = tm_id unless tm_id.nil?
|
|
63
68
|
|
|
64
69
|
if option?(:translation_memory_threshold)
|
|
65
|
-
|
|
70
|
+
threshold = delete_option(:translation_memory_threshold)
|
|
71
|
+
payload[:translation_memory_threshold] = threshold unless threshold.nil?
|
|
66
72
|
end
|
|
67
73
|
|
|
68
|
-
glossary_ids = build_glossary_ids_param
|
|
74
|
+
glossary_ids = build_glossary_ids_param
|
|
69
75
|
payload[:glossary_ids] = glossary_ids unless glossary_ids.nil?
|
|
76
|
+
glossary_id = build_glossary_param
|
|
77
|
+
payload[:glossary_id] = glossary_id unless glossary_id.nil?
|
|
70
78
|
|
|
71
79
|
build_texts(*execute_request_with_retries(post_request(payload)))
|
|
72
80
|
end
|
|
73
81
|
|
|
74
82
|
def details
|
|
75
|
-
"HTTP Headers: #{
|
|
76
|
-
|
|
83
|
+
"HTTP Headers: #{loggable_headers}\nPayload #{{ text: text, source_lang: source_lang,
|
|
84
|
+
target_lang: target_lang }}"
|
|
77
85
|
end
|
|
78
86
|
|
|
79
87
|
def to_s
|
|
@@ -94,11 +102,19 @@ module DeepL
|
|
|
94
102
|
data = JSON.parse(response.body)
|
|
95
103
|
|
|
96
104
|
texts = data['translations'].map do |translation|
|
|
97
|
-
Resources::Text.new(
|
|
98
|
-
|
|
105
|
+
Resources::Text.new(
|
|
106
|
+
translation['text'], translation['detected_source_language'],
|
|
107
|
+
translation['model_type_used'], request, response,
|
|
108
|
+
Utils::NumberParser.optional_count(translation['billed_characters']),
|
|
109
|
+
optional_string(translation['tag_handling_version'])
|
|
110
|
+
)
|
|
99
111
|
end
|
|
100
112
|
|
|
101
|
-
|
|
113
|
+
text.is_a?(Array) ? texts : texts.first
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def optional_string(value)
|
|
117
|
+
value if value.is_a?(String)
|
|
102
118
|
end
|
|
103
119
|
|
|
104
120
|
def path
|
|
@@ -24,10 +24,7 @@ module DeepL
|
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def build_query_string
|
|
27
|
-
|
|
28
|
-
# a URI query string. A literal `+` is already escaped as %2B, so every remaining `+`
|
|
29
|
-
# is a space.
|
|
30
|
-
URI.encode_www_form(query_params).gsub('+', '%20')
|
|
27
|
+
encode_query(query_params)
|
|
31
28
|
end
|
|
32
29
|
|
|
33
30
|
def query_params
|
|
@@ -15,7 +15,7 @@ module DeepL
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def request
|
|
18
|
-
build_translation_memory_export(*execute_request_with_retries(
|
|
18
|
+
build_translation_memory_export(*execute_request_with_retries(bodiless_post_request))
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def to_s
|
|
@@ -24,6 +24,11 @@ module DeepL
|
|
|
24
24
|
|
|
25
25
|
private
|
|
26
26
|
|
|
27
|
+
# A repeated creation request could create a second resource.
|
|
28
|
+
def retry_only_unprocessed?
|
|
29
|
+
true
|
|
30
|
+
end
|
|
31
|
+
|
|
27
32
|
def build_translation_memory_export(request, response)
|
|
28
33
|
data = JSON.parse(response.body)
|
|
29
34
|
# The API answers 200 when it reused a previously completed export, and 202 when it
|
|
@@ -33,7 +38,7 @@ module DeepL
|
|
|
33
38
|
end
|
|
34
39
|
|
|
35
40
|
def path
|
|
36
|
-
"translation_memories/#{translation_memory_id}/export"
|
|
41
|
+
"translation_memories/#{escape_path_segment(translation_memory_id)}/export"
|
|
37
42
|
end
|
|
38
43
|
end
|
|
39
44
|
end
|
|
@@ -7,10 +7,15 @@ module DeepL
|
|
|
7
7
|
module Requests
|
|
8
8
|
module TranslationMemory
|
|
9
9
|
class CreateImport < Base
|
|
10
|
+
accepts_options :content_type, :display_name
|
|
11
|
+
|
|
10
12
|
attr_reader :file_name, :content_length
|
|
11
13
|
|
|
12
14
|
def initialize(api, file_name, content_length, options = {}, additional_headers = {})
|
|
13
15
|
super(api, options, additional_headers)
|
|
16
|
+
raise ArgumentError, '`file_name` must not be empty' if file_name.to_s.empty?
|
|
17
|
+
raise ArgumentError, '`content_length` must be positive' unless content_length.positive?
|
|
18
|
+
|
|
14
19
|
@file_name = file_name
|
|
15
20
|
@content_length = content_length
|
|
16
21
|
@content_type = delete_option(:content_type)
|
|
@@ -22,7 +27,7 @@ module DeepL
|
|
|
22
27
|
end
|
|
23
28
|
|
|
24
29
|
def details
|
|
25
|
-
"HTTP Headers: #{
|
|
30
|
+
"HTTP Headers: #{loggable_headers}\nPayload #{payload}"
|
|
26
31
|
end
|
|
27
32
|
|
|
28
33
|
def to_s
|
|
@@ -31,6 +36,11 @@ module DeepL
|
|
|
31
36
|
|
|
32
37
|
private
|
|
33
38
|
|
|
39
|
+
# A repeated creation request could create a second resource.
|
|
40
|
+
def retry_only_unprocessed?
|
|
41
|
+
true
|
|
42
|
+
end
|
|
43
|
+
|
|
34
44
|
def payload
|
|
35
45
|
source_file = { file_name: file_name, content_length: content_length }
|
|
36
46
|
source_file[:content_type] = @content_type if @content_type
|
|
@@ -7,7 +7,7 @@ module DeepL
|
|
|
7
7
|
module Requests
|
|
8
8
|
module TranslationMemory
|
|
9
9
|
class List < Base
|
|
10
|
-
|
|
10
|
+
accepts_options :page, :page_size
|
|
11
11
|
|
|
12
12
|
def initialize(api, options = {})
|
|
13
13
|
super
|
|
@@ -31,8 +31,9 @@ module DeepL
|
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
def query_params
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
self.class.accepted_options.each_with_object({}) do |option_name, params|
|
|
35
|
+
value = option(option_name)
|
|
36
|
+
params[option_name] = value.to_s unless value.nil?
|
|
36
37
|
end
|
|
37
38
|
end
|
|
38
39
|
|
|
@@ -7,7 +7,7 @@ module DeepL
|
|
|
7
7
|
module Requests
|
|
8
8
|
module TranslationMemory
|
|
9
9
|
class Segments < Base
|
|
10
|
-
|
|
10
|
+
accepts_options :page_size, :page_cursor, :filter_text, :filter_case_sensitive
|
|
11
11
|
|
|
12
12
|
attr_reader :translation_memory_id
|
|
13
13
|
|
|
@@ -32,13 +32,14 @@ module DeepL
|
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
def query_params
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
self.class.accepted_options.each_with_object({}) do |option_name, params|
|
|
36
|
+
value = option(option_name)
|
|
37
|
+
params[option_name] = value.to_s unless value.nil?
|
|
37
38
|
end
|
|
38
39
|
end
|
|
39
40
|
|
|
40
41
|
def path
|
|
41
|
-
"translation_memories/#{translation_memory_id}/segments"
|
|
42
|
+
"translation_memories/#{escape_path_segment(translation_memory_id)}/segments"
|
|
42
43
|
end
|
|
43
44
|
end
|
|
44
45
|
end
|
|
@@ -9,8 +9,10 @@ module DeepL
|
|
|
9
9
|
##
|
|
10
10
|
# Common behaviour of the requests to a pre-signed storage URL handed out by the API, such
|
|
11
11
|
# as a translation memory upload or download URL. Those URLs point outside of the DeepL
|
|
12
|
-
# API, so they are requested over their own connection and the
|
|
13
|
-
#
|
|
12
|
+
# API, so they are requested over their own connection and send only the User-Agent: neither
|
|
13
|
+
# the DeepL `Authorization` header nor other caller headers. That connection uses the same
|
|
14
|
+
# proxy, SSL and timeout options as the API connection. A non-2xx response raises a plain
|
|
15
|
+
# DeepL::Exceptions::Error.
|
|
14
16
|
|
|
15
17
|
class StorageBase < DeepL::Requests::Base
|
|
16
18
|
attr_reader :storage_url
|
|
@@ -18,7 +20,7 @@ module DeepL
|
|
|
18
20
|
def initialize(api, storage_url, options = {}, additional_headers = {})
|
|
19
21
|
super(api, options, additional_headers)
|
|
20
22
|
if storage_url.nil? || storage_url.empty?
|
|
21
|
-
raise
|
|
23
|
+
raise ArgumentError, 'Storage URL must not be empty'
|
|
22
24
|
end
|
|
23
25
|
|
|
24
26
|
@storage_url = storage_url
|
|
@@ -27,19 +29,45 @@ module DeepL
|
|
|
27
29
|
private
|
|
28
30
|
|
|
29
31
|
def http_client
|
|
30
|
-
@http_client ||=
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
@http_client ||= if api.http_client_options
|
|
33
|
+
Utils::HTTPClientBuilder.build(storage_url, api.http_client_options)
|
|
34
|
+
else
|
|
35
|
+
default_http_client
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def http_client_options_of(_client)
|
|
40
|
+
api.http_client_options
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def default_http_client
|
|
44
|
+
client = Net::HTTP.new(uri.host, uri.port)
|
|
45
|
+
client.use_ssl = uri.scheme == 'https'
|
|
46
|
+
Utils::HTTPClientBuilder.disable_lenient_defaults(client)
|
|
35
47
|
end
|
|
36
48
|
|
|
37
49
|
def uri
|
|
38
50
|
@uri ||= URI(storage_url)
|
|
39
51
|
end
|
|
40
52
|
|
|
53
|
+
# Storage responses are not DeepL API responses, so their statuses carry none of the API's
|
|
54
|
+
# meanings: an expired URL replies 403, which is no authorization failure.
|
|
55
|
+
def validate_response!(response, _request = nil)
|
|
56
|
+
return if response.is_a?(Net::HTTPSuccess)
|
|
57
|
+
|
|
58
|
+
status = response.code.to_i
|
|
59
|
+
raise Exceptions::Error.new(
|
|
60
|
+
"Request to the storage URL failed with HTTP status #{status}",
|
|
61
|
+
http_status_code: status, should_retry: status == 429 || (500..599).cover?(status)
|
|
62
|
+
)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Only the User-Agent: the caller's from the configured +headers+, else the library's.
|
|
41
66
|
def headers
|
|
42
|
-
|
|
67
|
+
caller_agent = (api.configuration.headers || {}).find do |name, _|
|
|
68
|
+
name.to_s.casecmp?('user-agent')
|
|
69
|
+
end
|
|
70
|
+
{ 'User-Agent' => caller_agent ? caller_agent.last : api.configuration.user_agent }
|
|
43
71
|
end
|
|
44
72
|
end
|
|
45
73
|
end
|
data/lib/deepl/requests/usage.rb
CHANGED
|
@@ -22,7 +22,34 @@ module DeepL
|
|
|
22
22
|
|
|
23
23
|
def build_usage(request, response)
|
|
24
24
|
data = JSON.parse(response.body)
|
|
25
|
-
Resources::Usage.new(data['character_count'], data['character_limit'], request, response
|
|
25
|
+
Resources::Usage.new(data['character_count'], data['character_limit'], request, response,
|
|
26
|
+
**usage_fields(data))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def usage_fields(data)
|
|
30
|
+
details = Resources::Usage::DETAIL_TYPES.to_h { |type| [type, detail(data, type)] }
|
|
31
|
+
details.merge(products: products(data['products']),
|
|
32
|
+
start_time: optional_time(data['start_time']),
|
|
33
|
+
end_time: optional_time(data['end_time']))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def detail(data, type)
|
|
37
|
+
count = data["#{type}_count"]
|
|
38
|
+
limit = data["#{type}_limit"]
|
|
39
|
+
Resources::Usage::Detail.new(count, limit) unless count.nil? || limit.nil?
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def products(entries)
|
|
43
|
+
return nil unless entries.is_a?(Array)
|
|
44
|
+
|
|
45
|
+
entries.grep(Hash).map { |product| Resources::UsageProduct.new(product) }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# An unexpected billing-period format must not fail the whole usage response.
|
|
49
|
+
def optional_time(value)
|
|
50
|
+
Utils::TimeParser.parse_optional_time(value) if value.is_a?(String)
|
|
51
|
+
rescue ArgumentError
|
|
52
|
+
nil
|
|
26
53
|
end
|
|
27
54
|
|
|
28
55
|
def path
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
# @api private
|
|
9
|
+
# Builds the results of the write endpoints (rephrase, correct) from their response.
|
|
10
|
+
module WriteImprovements
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
# +nil+ keeps the language of the text; an empty target language is rejected.
|
|
14
|
+
def validate_optional_target_lang!(target_lang)
|
|
15
|
+
return if target_lang.nil? || !target_lang.to_s.empty?
|
|
16
|
+
|
|
17
|
+
raise ArgumentError, '`target_lang` must not be empty; pass nil to keep the language'
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Returns one DeepL::Resources::Text per improvement. Raises DeepL::Exceptions::Error if
|
|
21
|
+
# the response has fewer improvements than +input_count+ input texts.
|
|
22
|
+
def build_improvements(input_count, request, response)
|
|
23
|
+
improvements = JSON.parse(response.body)['improvements']
|
|
24
|
+
validate_improvement_count!(improvements, input_count)
|
|
25
|
+
improvements.map do |improvement|
|
|
26
|
+
target_language = improvement['target_language']
|
|
27
|
+
Resources::Text.new(improvement['text'], improvement['detected_source_language'], nil,
|
|
28
|
+
request, response, nil, nil,
|
|
29
|
+
target_language.is_a?(String) ? target_language : nil)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def validate_improvement_count!(improvements, input_count)
|
|
34
|
+
return if improvements.is_a?(Array) && improvements.size >= input_count
|
|
35
|
+
|
|
36
|
+
count = improvements.is_a?(Array) ? improvements.size : 'no'
|
|
37
|
+
raise Exceptions::Error, 'Unexpected response from the DeepL API: expected ' \
|
|
38
|
+
"#{input_count} improvements, got #{count}"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
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 Resources
|
|
8
|
+
##
|
|
9
|
+
# Usage breakdown of an admin usage report. +speech_to_speech_minutes+ is +nil+ when the
|
|
10
|
+
# response does not contain it.
|
|
11
|
+
class AdminUsageBreakdown
|
|
12
|
+
attr_reader :total_characters, :text_translation_characters,
|
|
13
|
+
:document_translation_characters, :text_improvement_characters,
|
|
14
|
+
:speech_to_text_minutes, :speech_to_speech_minutes
|
|
15
|
+
|
|
16
|
+
def initialize(usage)
|
|
17
|
+
usage = {} unless usage.is_a?(Hash)
|
|
18
|
+
@total_characters = Utils::NumberParser.count(usage['total_characters'])
|
|
19
|
+
@text_translation_characters = Utils::NumberParser.count(usage['text_translation_characters'])
|
|
20
|
+
@document_translation_characters =
|
|
21
|
+
Utils::NumberParser.count(usage['document_translation_characters'])
|
|
22
|
+
@text_improvement_characters = Utils::NumberParser.count(usage['text_improvement_characters'])
|
|
23
|
+
@speech_to_text_minutes = Utils::NumberParser.minutes(usage['speech_to_text_minutes'])
|
|
24
|
+
@speech_to_speech_minutes =
|
|
25
|
+
Utils::NumberParser.optional_minutes(usage['speech_to_speech_minutes'])
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
##
|
|
30
|
+
# Usage of a single API key. +usage_date+ is only set when grouped by key and day;
|
|
31
|
+
# +api_key_label+ is +nil+ when the response does not contain it.
|
|
32
|
+
class KeyUsage
|
|
33
|
+
attr_reader :api_key, :api_key_label, :usage, :usage_date
|
|
34
|
+
|
|
35
|
+
def initialize(key_usage)
|
|
36
|
+
@api_key = key_usage['api_key'].is_a?(String) ? key_usage['api_key'] : ''
|
|
37
|
+
@api_key_label = key_usage['api_key_label'].is_a?(String) ? key_usage['api_key_label'] : nil
|
|
38
|
+
@usage = AdminUsageBreakdown.new(key_usage['usage'])
|
|
39
|
+
@usage_date = Utils::TimeParser.parse_optional_utc_time(key_usage['usage_date'])
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
##
|
|
44
|
+
# Admin usage analytics report. +key_usages+ is only filled when grouped by key and
|
|
45
|
+
# +key_and_day_usages+ only when grouped by key and day; otherwise they are empty.
|
|
46
|
+
# +total_usage+, +start_date+ and +end_date+ are +nil+ when the response does not contain them.
|
|
47
|
+
class AdminUsageReport < Base
|
|
48
|
+
attr_reader :total_usage, :start_date, :end_date, :group_by, :key_usages,
|
|
49
|
+
:key_and_day_usages
|
|
50
|
+
|
|
51
|
+
def initialize(data, *args) # rubocop:disable Metrics/AbcSize
|
|
52
|
+
super(*args)
|
|
53
|
+
report = data.is_a?(Hash) ? data['usage_report'] : nil
|
|
54
|
+
report = {} unless report.is_a?(Hash)
|
|
55
|
+
total_usage = report['total_usage']
|
|
56
|
+
@total_usage = total_usage.is_a?(Hash) ? AdminUsageBreakdown.new(total_usage) : nil
|
|
57
|
+
@start_date = Utils::TimeParser.parse_optional_utc_time(report['start_date'])
|
|
58
|
+
@end_date = Utils::TimeParser.parse_optional_utc_time(report['end_date'])
|
|
59
|
+
@group_by = report['group_by'].is_a?(String) ? report['group_by'] : nil
|
|
60
|
+
@key_usages = parse_key_usages(report['key_usages'])
|
|
61
|
+
@key_and_day_usages = parse_key_usages(report['key_and_day_usages'])
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def parse_key_usages(key_usages)
|
|
67
|
+
return [] unless key_usages.is_a?(Array)
|
|
68
|
+
|
|
69
|
+
key_usages.grep(Hash).map { |usage| KeyUsage.new(usage) }
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
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 Resources
|
|
8
|
+
##
|
|
9
|
+
# Usage breakdown of a custom tag. +speech_to_speech_minutes+ is +nil+ when the response
|
|
10
|
+
# does not contain it.
|
|
11
|
+
class CustomTagUsageBreakdown
|
|
12
|
+
attr_reader :total_characters, :text_translation_characters,
|
|
13
|
+
:text_improvement_characters, :speech_to_text_minutes,
|
|
14
|
+
:speech_to_speech_minutes
|
|
15
|
+
|
|
16
|
+
def initialize(breakdown)
|
|
17
|
+
breakdown = {} unless breakdown.is_a?(Hash)
|
|
18
|
+
@total_characters = Utils::NumberParser.count(breakdown['total_characters'])
|
|
19
|
+
@text_translation_characters =
|
|
20
|
+
Utils::NumberParser.count(breakdown['text_translation_characters'])
|
|
21
|
+
@text_improvement_characters =
|
|
22
|
+
Utils::NumberParser.count(breakdown['text_improvement_characters'])
|
|
23
|
+
@speech_to_text_minutes = Utils::NumberParser.minutes(breakdown['speech_to_text_minutes'])
|
|
24
|
+
@speech_to_speech_minutes =
|
|
25
|
+
Utils::NumberParser.optional_minutes(breakdown['speech_to_speech_minutes'])
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
##
|
|
30
|
+
# Usage of a single custom tag. +usage_date+ is only set when aggregated by day.
|
|
31
|
+
class CustomTagUsage
|
|
32
|
+
attr_reader :custom_tag, :breakdown, :usage_date
|
|
33
|
+
|
|
34
|
+
def initialize(usage)
|
|
35
|
+
@custom_tag = usage['custom_tag'].is_a?(String) ? usage['custom_tag'] : ''
|
|
36
|
+
@breakdown = CustomTagUsageBreakdown.new(usage['breakdown'])
|
|
37
|
+
@usage_date = Utils::TimeParser.parse_optional_utc_time(usage['usage_date'])
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
##
|
|
42
|
+
# Custom tag usage analytics report. +next_page+ is +nil+ on the last page.
|
|
43
|
+
class CustomTagUsageReport < Base
|
|
44
|
+
attr_reader :aggregate_by, :start_date, :end_date, :next_page, :usage
|
|
45
|
+
|
|
46
|
+
def initialize(data, *args) # rubocop:disable Metrics/AbcSize
|
|
47
|
+
super(*args)
|
|
48
|
+
report = data.is_a?(Hash) ? data['custom_tag_usage_report'] : nil
|
|
49
|
+
report = {} unless report.is_a?(Hash)
|
|
50
|
+
@aggregate_by = report['aggregate_by'].is_a?(String) ? report['aggregate_by'] : nil
|
|
51
|
+
@start_date = Utils::TimeParser.parse_optional_utc_time(report['start_date'])
|
|
52
|
+
@end_date = Utils::TimeParser.parse_optional_utc_time(report['end_date'])
|
|
53
|
+
@next_page = report['next_page'].is_a?(Integer) ? report['next_page'] : nil
|
|
54
|
+
usages = report['usage'].is_a?(Array) ? report['usage'] : []
|
|
55
|
+
@usage = usages.grep(Hash).map { |item| CustomTagUsage.new(item) }
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|