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
|
@@ -12,7 +12,6 @@ module DeepL
|
|
|
12
12
|
STATUS_AWAITING_INPUT = 'awaiting_input'
|
|
13
13
|
STATUS_PROCESSING = 'processing'
|
|
14
14
|
STATUS_COMPLETED = 'completed'
|
|
15
|
-
STATUS_DOWNLOADED = 'downloaded'
|
|
16
15
|
STATUS_FAILED = 'failed'
|
|
17
16
|
STATUS_EXPIRED = 'expired'
|
|
18
17
|
|
|
@@ -26,20 +25,20 @@ module DeepL
|
|
|
26
25
|
@status = result['status']
|
|
27
26
|
@required_action = status_metadata['required_action']
|
|
28
27
|
@download_url = result['download_url']
|
|
29
|
-
@expires_at = Utils::TimeParser.
|
|
28
|
+
@expires_at = Utils::TimeParser.parse_optional_utc_time(result['expires_at'])
|
|
30
29
|
@error_message = error['message']
|
|
31
30
|
@translation_memory_id = result['translation_memory_id']
|
|
32
31
|
@skipped_segment_count = result['skipped_segment_count']
|
|
33
32
|
end
|
|
34
33
|
|
|
35
34
|
##
|
|
36
|
-
# Checks if the job terminated
|
|
37
|
-
# no further waiting is necessary.
|
|
35
|
+
# Checks if the job terminated: its status is +completed+, +failed+ or +expired+. Note that
|
|
36
|
+
# this could be due to an error as well, but means no further waiting is necessary.
|
|
38
37
|
#
|
|
39
38
|
# @return [true] if so
|
|
40
39
|
|
|
41
40
|
def finished?
|
|
42
|
-
[STATUS_COMPLETED,
|
|
41
|
+
[STATUS_COMPLETED, STATUS_FAILED, STATUS_EXPIRED].include?(status)
|
|
43
42
|
end
|
|
44
43
|
|
|
45
44
|
##
|
|
@@ -110,6 +109,26 @@ module DeepL
|
|
|
110
109
|
result ? result.error? : false
|
|
111
110
|
end
|
|
112
111
|
|
|
112
|
+
##
|
|
113
|
+
# Checks if the job succeeded: its status is +completed+. Unlike +finished?+, this is
|
|
114
|
+
# false for a failed or expired job.
|
|
115
|
+
#
|
|
116
|
+
# @return [Boolean]
|
|
117
|
+
|
|
118
|
+
def done?
|
|
119
|
+
status == TranslationMemoryJobResult::STATUS_COMPLETED
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
##
|
|
123
|
+
# Checks if the job has not failed: its status is not +failed+ or +expired+. Also true
|
|
124
|
+
# while the job is still running.
|
|
125
|
+
#
|
|
126
|
+
# @return [Boolean]
|
|
127
|
+
|
|
128
|
+
def ok?
|
|
129
|
+
!error?
|
|
130
|
+
end
|
|
131
|
+
|
|
113
132
|
def error_message
|
|
114
133
|
result&.error_message
|
|
115
134
|
end
|
|
@@ -124,8 +143,8 @@ module DeepL
|
|
|
124
143
|
@job_id = job['job_id']
|
|
125
144
|
@product = job['product']
|
|
126
145
|
@operation = job['operation']
|
|
127
|
-
@creation_time = Utils::TimeParser.
|
|
128
|
-
@updated_time = Utils::TimeParser.
|
|
146
|
+
@creation_time = Utils::TimeParser.parse_optional_utc_time(job['creation_time'])
|
|
147
|
+
@updated_time = Utils::TimeParser.parse_optional_utc_time(job['updated_time'])
|
|
129
148
|
end
|
|
130
149
|
|
|
131
150
|
def extract_parameters(job)
|
|
@@ -13,9 +13,11 @@ module DeepL
|
|
|
13
13
|
@target_segment_id = target_segment['target_segment_id']
|
|
14
14
|
@target_language = target_segment['target_language']
|
|
15
15
|
@target_text = target_segment['target_text']
|
|
16
|
-
@creation_time = Utils::TimeParser.
|
|
17
|
-
@updated_time = Utils::TimeParser.
|
|
18
|
-
@last_used_time = Utils::TimeParser.
|
|
16
|
+
@creation_time = Utils::TimeParser.parse_optional_utc_time(target_segment['creation_time'])
|
|
17
|
+
@updated_time = Utils::TimeParser.parse_optional_utc_time(target_segment['updated_time'])
|
|
18
|
+
@last_used_time = Utils::TimeParser.parse_optional_utc_time(
|
|
19
|
+
target_segment['last_used_time']
|
|
20
|
+
)
|
|
19
21
|
end
|
|
20
22
|
|
|
21
23
|
def to_s
|
|
@@ -33,9 +35,9 @@ module DeepL
|
|
|
33
35
|
@targets = (segment['targets'] || []).map do |target|
|
|
34
36
|
TranslationMemoryTargetSegment.new(target)
|
|
35
37
|
end
|
|
36
|
-
@creation_time = Utils::TimeParser.
|
|
37
|
-
@updated_time = Utils::TimeParser.
|
|
38
|
-
@last_used_time = Utils::TimeParser.
|
|
38
|
+
@creation_time = Utils::TimeParser.parse_optional_utc_time(segment['creation_time'])
|
|
39
|
+
@updated_time = Utils::TimeParser.parse_optional_utc_time(segment['updated_time'])
|
|
40
|
+
@last_used_time = Utils::TimeParser.parse_optional_utc_time(segment['last_used_time'])
|
|
39
41
|
end
|
|
40
42
|
|
|
41
43
|
def to_s
|
|
@@ -6,21 +6,77 @@
|
|
|
6
6
|
module DeepL
|
|
7
7
|
module Resources
|
|
8
8
|
class Usage < Base
|
|
9
|
-
|
|
9
|
+
##
|
|
10
|
+
# The amount used and the maximum amount allowed for one usage type.
|
|
11
|
+
class Detail
|
|
12
|
+
attr_reader :count, :limit
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
def initialize(count, limit)
|
|
15
|
+
@count = count
|
|
16
|
+
@limit = limit
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def limit_reached?
|
|
20
|
+
count >= limit
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def to_s
|
|
24
|
+
"#{count} of #{limit}"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
DETAIL_TYPES = %i[api_key_character speech_to_text_minutes speech_to_speech_minutes].freeze
|
|
29
|
+
KEYWORD_FIELDS = (DETAIL_TYPES + %i[products start_time end_time]).freeze
|
|
30
|
+
|
|
31
|
+
attr_reader :character, *KEYWORD_FIELDS
|
|
32
|
+
|
|
33
|
+
##
|
|
34
|
+
# Each keyword argument is nil when the response does not contain the field.
|
|
35
|
+
#
|
|
36
|
+
# @param [Detail, nil] api_key_character Character usage of this API key (API Pro).
|
|
37
|
+
# @param [Detail, nil] speech_to_text_minutes Speech-to-text usage in minutes (API Pro).
|
|
38
|
+
# @param [Detail, nil] speech_to_speech_minutes Speech-to-speech usage in minutes (API Pro).
|
|
39
|
+
# @param [Array<UsageProduct>, nil] products Per-product usage (API Pro).
|
|
40
|
+
# @param [Time, nil] start_time Start of the current billing period (API Pro).
|
|
41
|
+
# @param [Time, nil] end_time End of the current billing period (API Pro).
|
|
42
|
+
|
|
43
|
+
def initialize(character_count, character_limit, *args, **fields)
|
|
12
44
|
super(*args)
|
|
13
45
|
|
|
46
|
+
unknown = fields.keys - KEYWORD_FIELDS
|
|
47
|
+
raise ArgumentError, "unknown keywords: #{unknown.join(', ')}" unless unknown.empty?
|
|
48
|
+
|
|
14
49
|
@character_count = character_count
|
|
15
50
|
@character_limit = character_limit
|
|
51
|
+
@character = Detail.new(character_count, character_limit) unless
|
|
52
|
+
character_count.nil? || character_limit.nil?
|
|
53
|
+
KEYWORD_FIELDS.each { |field| instance_variable_set("@#{field}", fields[field]) }
|
|
16
54
|
end
|
|
17
55
|
|
|
18
56
|
def to_s
|
|
19
|
-
"#{character_count} / #{character_limit}"
|
|
57
|
+
"#{@character_count} / #{@character_limit}"
|
|
20
58
|
end
|
|
21
59
|
|
|
22
|
-
|
|
23
|
-
|
|
60
|
+
##
|
|
61
|
+
# True if the account's character limit or this API key's character limit is reached; the
|
|
62
|
+
# speech minutes have their own details. A missing detail counts as not reached.
|
|
63
|
+
def any_character_limit_reached?
|
|
64
|
+
[character, api_key_character].compact.any?(&:limit_reached?)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
class UsageProduct
|
|
69
|
+
attr_reader :product_type, :billing_unit, :api_key_unit_count, :account_unit_count
|
|
70
|
+
|
|
71
|
+
def initialize(product)
|
|
72
|
+
@product_type = product['product_type']
|
|
73
|
+
@billing_unit = product['billing_unit']
|
|
74
|
+
@api_key_unit_count = product['api_key_unit_count']
|
|
75
|
+
@account_unit_count = product['account_unit_count']
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def to_s
|
|
79
|
+
"#{product_type}: #{api_key_unit_count} / #{account_unit_count} #{billing_unit}"
|
|
24
80
|
end
|
|
25
81
|
end
|
|
26
82
|
end
|
|
@@ -0,0 +1,89 @@
|
|
|
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 Utils
|
|
8
|
+
# Builds the API connection of a DeepL::Client or DeepL::AdminClient from its constructor
|
|
9
|
+
# options. Nothing is read from the environment or from module state.
|
|
10
|
+
# @api private
|
|
11
|
+
module ClientBuilder
|
|
12
|
+
extend self
|
|
13
|
+
|
|
14
|
+
def build_api(key, key_name:, server_url:, default_server_url:, headers:, max_retries:,
|
|
15
|
+
timeout:, proxy:, send_platform_info:, app_info:, http_client_options:, logger:) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/ParameterLists
|
|
16
|
+
key = key.to_s.strip
|
|
17
|
+
raise ArgumentError, "#{key_name} must not be empty" if key.empty?
|
|
18
|
+
|
|
19
|
+
host = host(server_url, default_server_url)
|
|
20
|
+
headers = (headers || {}).to_h
|
|
21
|
+
configuration = Configuration.new(
|
|
22
|
+
auth_key: key, host: host, max_network_retries: or_default(max_retries, 5),
|
|
23
|
+
timeout: or_default(timeout, 10), logger: logger,
|
|
24
|
+
headers: headers.reject { |name, _| user_agent_header?(name) },
|
|
25
|
+
user_agent: headers.find { |name, _| user_agent_header?(name) }&.last,
|
|
26
|
+
send_platform_info: or_default(send_platform_info, true), app_info: AppInfo.from(app_info)
|
|
27
|
+
)
|
|
28
|
+
API.new(configuration, merge_proxy(http_client_options, proxy))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
# nil means the option was not given, so it takes the constructor default.
|
|
34
|
+
def or_default(value, default)
|
|
35
|
+
value.nil? ? default : value
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Validates the server URL and removes its trailing slashes: requests append /v2/... or
|
|
39
|
+
# /v3/... to the host, so a trailing slash would give //v2/...
|
|
40
|
+
def host(server_url, default_server_url)
|
|
41
|
+
return default_server_url if server_url.nil?
|
|
42
|
+
|
|
43
|
+
validate_server_url!(server_url)
|
|
44
|
+
server_url.to_s.sub(%r{/+\z}, '')
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The server URL must be an absolute http or https URL with a host; a port and a path
|
|
48
|
+
# prefix are allowed. +nil+ selects the default server.
|
|
49
|
+
def validate_server_url!(server_url)
|
|
50
|
+
return if server_url.nil?
|
|
51
|
+
|
|
52
|
+
uri = begin
|
|
53
|
+
URI.parse(server_url.to_s)
|
|
54
|
+
rescue URI::InvalidURIError
|
|
55
|
+
nil
|
|
56
|
+
end
|
|
57
|
+
return if uri.is_a?(URI::HTTP) && !uri.host.to_s.empty?
|
|
58
|
+
|
|
59
|
+
# The URL is left out of the message: it can contain credentials.
|
|
60
|
+
raise ArgumentError, 'server_url must be an absolute http or https URL with a host',
|
|
61
|
+
cause: nil
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def user_agent_header?(name)
|
|
65
|
+
name.to_s.casecmp?('user-agent')
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def no_proxy?(proxy)
|
|
69
|
+
proxy.nil? || (proxy.respond_to?(:empty?) && proxy.empty?)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def merge_proxy(options, proxy) # rubocop:disable Metrics/MethodLength
|
|
73
|
+
return options if proxy.nil?
|
|
74
|
+
if options && !no_proxy?(options.proxy)
|
|
75
|
+
raise ArgumentError,
|
|
76
|
+
'Set the proxy either with proxy: or in http_client_options:, not both'
|
|
77
|
+
end
|
|
78
|
+
return HTTPClientOptions.new(proxy) if options.nil?
|
|
79
|
+
|
|
80
|
+
HTTPClientOptions.new(proxy, options.cert_path,
|
|
81
|
+
enable_ssl_verification: options.enable_ssl_verification,
|
|
82
|
+
open_timeout: options.open_timeout,
|
|
83
|
+
read_timeout: options.read_timeout,
|
|
84
|
+
write_timeout: options.write_timeout,
|
|
85
|
+
ssl_timeout: options.ssl_timeout)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
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 Utils
|
|
8
|
+
# @api private
|
|
9
|
+
# Polls the status of a document translation every 5 seconds.
|
|
10
|
+
module DocumentPoller
|
|
11
|
+
INTERVAL_S = 5
|
|
12
|
+
|
|
13
|
+
extend self
|
|
14
|
+
|
|
15
|
+
# Polls until the translation is done and returns that status. Raises
|
|
16
|
+
# DeepL::Exceptions::Error with the API's message when the translation fails, and when
|
|
17
|
+
# +timeout_s+ is set and expires. An unknown status keeps polling.
|
|
18
|
+
def wait_until_done(api, handle, timeout_s: nil)
|
|
19
|
+
deadline = timeout_s.nil? ? nil : monotonic_now + timeout_s
|
|
20
|
+
loop do
|
|
21
|
+
status = get_status(api, handle)
|
|
22
|
+
return status if status.done?
|
|
23
|
+
|
|
24
|
+
raise_if_failed(status)
|
|
25
|
+
|
|
26
|
+
sleep(interval_until(deadline, timeout_s))
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Polls until the translation is done or failed and returns that status, or the last status
|
|
31
|
+
# after +max_queries+ + 1 queries if +max_queries+ is set.
|
|
32
|
+
def wait_until_finished(api, handle, max_queries: nil)
|
|
33
|
+
num_queries = 0
|
|
34
|
+
loop do
|
|
35
|
+
sleep(INTERVAL_S) if num_queries.positive?
|
|
36
|
+
num_queries += 1
|
|
37
|
+
status = get_status(api, handle)
|
|
38
|
+
return status if status.finished? || (!max_queries.nil? && num_queries > max_queries)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def raise_if_failed(status)
|
|
45
|
+
return unless status.error?
|
|
46
|
+
|
|
47
|
+
raise Exceptions::Error, "Document translation failed: #{status.error_message}"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def get_status(api, handle)
|
|
51
|
+
Requests::Document::GetStatus.new(api, handle.document_id, handle.document_key).request
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def interval_until(deadline, timeout_s)
|
|
55
|
+
return INTERVAL_S if deadline.nil?
|
|
56
|
+
|
|
57
|
+
remaining = deadline - monotonic_now
|
|
58
|
+
if remaining <= 0
|
|
59
|
+
raise Exceptions::Error, "Document translation did not finish within #{timeout_s} seconds"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
[INTERVAL_S, remaining].min
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def monotonic_now
|
|
66
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -21,13 +21,14 @@ module DeepL
|
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
def initialize(response)
|
|
24
|
+
def initialize(response, request = nil)
|
|
25
25
|
@response = response
|
|
26
|
+
@request = request
|
|
26
27
|
end
|
|
27
28
|
|
|
28
29
|
def build
|
|
29
30
|
error_class = self.class.error_class_from_response_code(response.code.to_i)
|
|
30
|
-
error_class.new(response)
|
|
31
|
+
error_class.new(response, request)
|
|
31
32
|
end
|
|
32
33
|
end
|
|
33
34
|
end
|
|
@@ -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 Utils
|
|
8
|
+
# TSV encoding of glossary entries, shared by the v2 and v3 glossary requests.
|
|
9
|
+
# @api private
|
|
10
|
+
module GlossaryEntries
|
|
11
|
+
extend self
|
|
12
|
+
|
|
13
|
+
# A hash, source/target pairs or a TSV string of them, checked and joined as TSV.
|
|
14
|
+
def to_tsv(entries)
|
|
15
|
+
entries_class = Resources::MultilingualGlossaryDictionaryEntries
|
|
16
|
+
entries = entries.is_a?(String) ? entries_class.tsv_to_pairs(entries) : entries.to_a
|
|
17
|
+
entries_class.check_pairs!(entries).map do |entry|
|
|
18
|
+
source = TermValidator.validate!(entry.first)
|
|
19
|
+
target = TermValidator.validate!(entry.last)
|
|
20
|
+
"#{source}\t#{target}"
|
|
21
|
+
end.join("\n")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Source term to target term, one pair per non-empty TSV line.
|
|
25
|
+
def from_tsv(tsv)
|
|
26
|
+
tsv.to_s.split(/\r?\n/).reject(&:empty?).to_h { |line| line.split("\t", 2) }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
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 Utils
|
|
8
|
+
module HTTPClientBuilder
|
|
9
|
+
extend self
|
|
10
|
+
|
|
11
|
+
PROXY_SCHEMES = %w[http https].freeze
|
|
12
|
+
|
|
13
|
+
##
|
|
14
|
+
# Builds an unstarted Net::HTTP client for +host+ configured with +options+. Net::HTTP does
|
|
15
|
+
# not retry on its own (+max_retries+ 0): only the library's retries apply.
|
|
16
|
+
#
|
|
17
|
+
# @param [String] host Base URL of the API.
|
|
18
|
+
# @param [DeepL::HTTPClientOptions] options Proxy, SSL and timeout options.
|
|
19
|
+
# @return [Net::HTTP]
|
|
20
|
+
def build(host, options) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
21
|
+
uri = URI(host)
|
|
22
|
+
http = Net::HTTP.new(uri.host, uri.port, *proxy_arguments(options.proxy, uri))
|
|
23
|
+
http.use_ssl = uri.scheme == 'https'
|
|
24
|
+
disable_lenient_defaults(http)
|
|
25
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless options.enable_ssl_verification
|
|
26
|
+
http.ca_file = options.cert_path if options.cert_path
|
|
27
|
+
http.open_timeout = options.open_timeout unless options.open_timeout.nil?
|
|
28
|
+
http.read_timeout = options.read_timeout unless options.read_timeout.nil?
|
|
29
|
+
http.write_timeout = options.write_timeout unless options.write_timeout.nil?
|
|
30
|
+
http.ssl_timeout = options.ssl_timeout unless options.ssl_timeout.nil?
|
|
31
|
+
http
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
##
|
|
35
|
+
# Net::HTTP would re-send idempotent requests once on its own, outside max_retries, and would
|
|
36
|
+
# return a body shorter than its Content-Length as complete (+ignore_eof+, net-http 0.3+).
|
|
37
|
+
def disable_lenient_defaults(http)
|
|
38
|
+
http.max_retries = 0
|
|
39
|
+
http.ignore_eof = false if http.respond_to?(:ignore_eof=)
|
|
40
|
+
http
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
# The proxy is a URL string or +URI+ with the +http+ or +https+ scheme, e.g.
|
|
46
|
+
# +http://user:pass@proxy.example:8080+, or a hash with +proxy_addr+ and +proxy_port+ (string
|
|
47
|
+
# or symbol keys), which is a plain HTTP proxy. +nil+, +''+ and an empty hash mean no proxy;
|
|
48
|
+
# any other value raises ArgumentError. Returns the proxy arguments of +Net::HTTP.new+ for the
|
|
49
|
+
# server +uri+.
|
|
50
|
+
def proxy_arguments(proxy, uri)
|
|
51
|
+
return [nil, nil] if proxy.nil? || proxy == '' || proxy == {}
|
|
52
|
+
|
|
53
|
+
proxy = proxy.to_s if proxy.is_a?(URI::Generic)
|
|
54
|
+
return proxy_url_arguments(proxy, uri) if proxy.is_a?(String)
|
|
55
|
+
|
|
56
|
+
hash_proxy_arguments(proxy)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def proxy_url_arguments(proxy, uri)
|
|
60
|
+
proxy_uri = parse_proxy_url(proxy)
|
|
61
|
+
# The messages name the problem only: the proxy URL may hold credentials.
|
|
62
|
+
unless PROXY_SCHEMES.include?(proxy_uri.scheme)
|
|
63
|
+
raise ArgumentError, 'Invalid proxy URL scheme: expected http or https', cause: nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
arguments = [proxy_uri.host, proxy_uri.port, decode(proxy_uri.user),
|
|
67
|
+
decode(proxy_uri.password)]
|
|
68
|
+
proxy_uri.scheme == 'https' ? https_proxy_arguments(arguments, uri) : arguments
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Net::HTTP connects to the proxy over TLS (+p_use_ssl+) from net-http 0.5.0, and only for an
|
|
72
|
+
# https server; it does not verify the proxy's certificate.
|
|
73
|
+
def https_proxy_arguments(arguments, uri)
|
|
74
|
+
unless Net::HTTP.method_defined?(:proxy_use_ssl=)
|
|
75
|
+
raise ArgumentError, 'An https proxy needs net-http 0.5.0 or later (installed: ' \
|
|
76
|
+
"#{Net::HTTP::VERSION})", cause: nil
|
|
77
|
+
end
|
|
78
|
+
unless uri.scheme == 'https'
|
|
79
|
+
raise ArgumentError, 'An https proxy can only be used for an https server URL', cause: nil
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
arguments + [nil, true]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def hash_proxy_arguments(proxy)
|
|
86
|
+
if proxy.is_a?(Hash)
|
|
87
|
+
address = proxy.fetch('proxy_addr') { proxy[:proxy_addr] }
|
|
88
|
+
port = proxy.fetch('proxy_port') { proxy[:proxy_port] }
|
|
89
|
+
return [address, port] unless address.nil? || port.nil? || address.to_s.strip.empty?
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
raise ArgumentError,
|
|
93
|
+
'Invalid proxy: expected a URL string, a URI or a hash with proxy_addr and ' \
|
|
94
|
+
"proxy_port, got #{HTTPClientOptions.redact_proxy(proxy).inspect}"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Error messages show the URL without its user info; the URI error, which repeats the URL,
|
|
98
|
+
# is not kept as cause.
|
|
99
|
+
def parse_proxy_url(proxy)
|
|
100
|
+
proxy_uri = begin
|
|
101
|
+
URI(proxy)
|
|
102
|
+
rescue URI::InvalidURIError
|
|
103
|
+
nil
|
|
104
|
+
end
|
|
105
|
+
return proxy_uri unless proxy_uri.nil? || proxy_uri.host.to_s.empty?
|
|
106
|
+
|
|
107
|
+
raise ArgumentError, "Invalid proxy URL: #{HTTPClientOptions.redact_proxy(proxy)}",
|
|
108
|
+
cause: nil
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def decode(value)
|
|
112
|
+
value && URI::DEFAULT_PARSER.unescape(value)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
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 Utils
|
|
8
|
+
##
|
|
9
|
+
# Reads numbers from API responses without failing on unexpected values.
|
|
10
|
+
module NumberParser
|
|
11
|
+
extend self
|
|
12
|
+
|
|
13
|
+
# @return [Integer] The value if it is an Integer, otherwise 0.
|
|
14
|
+
def count(value)
|
|
15
|
+
value.is_a?(Integer) ? value : 0
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# @return [Float] The value as a Float if it is numeric, otherwise 0.0.
|
|
19
|
+
def minutes(value)
|
|
20
|
+
value.is_a?(Numeric) ? value.to_f : 0.0
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @return [Float, nil] The value as a Float if it is numeric, otherwise nil.
|
|
24
|
+
def optional_minutes(value)
|
|
25
|
+
value.is_a?(Numeric) ? value.to_f : nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @return [Integer, nil] The value if it is an Integer, otherwise nil.
|
|
29
|
+
def optional_count(value)
|
|
30
|
+
value.is_a?(Integer) ? value : nil
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
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 Utils
|
|
8
|
+
# Checks of an output file path before any request is sent.
|
|
9
|
+
# @api private
|
|
10
|
+
module OutputPath
|
|
11
|
+
extend self
|
|
12
|
+
|
|
13
|
+
# @raise [Errno::EEXIST] If +path+ exists.
|
|
14
|
+
# @raise [Errno::ENOENT] If the directory of +path+ does not exist.
|
|
15
|
+
# @raise [Errno::EACCES] If the directory of +path+ is not writable.
|
|
16
|
+
def check!(path)
|
|
17
|
+
raise Errno::EEXIST, path.to_s if File.exist?(path)
|
|
18
|
+
|
|
19
|
+
directory = File.dirname(path)
|
|
20
|
+
raise Errno::ENOENT, directory unless File.directory?(directory)
|
|
21
|
+
raise Errno::EACCES, directory unless File.writable?(directory)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
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 Utils
|
|
8
|
+
module TermValidator
|
|
9
|
+
extend self
|
|
10
|
+
|
|
11
|
+
# C0 and C1 control characters plus the Unicode line and paragraph separators.
|
|
12
|
+
INVALID_CHARACTERS = /[\u0000-\u001F\u0080-\u009F
]/
|
|
13
|
+
|
|
14
|
+
##
|
|
15
|
+
# Strips surrounding whitespace from a single glossary or spoken term, then checks it
|
|
16
|
+
# before it is serialized into a request.
|
|
17
|
+
#
|
|
18
|
+
# @param [String] term The term to check.
|
|
19
|
+
# @return [String] The stripped term, which is what the request sends.
|
|
20
|
+
# @raise [DeepL::Exceptions::ValidationError] (an ArgumentError) If the stripped term is empty
|
|
21
|
+
# or contains a control character.
|
|
22
|
+
def validate!(term)
|
|
23
|
+
term = term.to_s.strip
|
|
24
|
+
raise Exceptions::ValidationError, 'Term must not be empty' if term.empty?
|
|
25
|
+
if INVALID_CHARACTERS.match?(term)
|
|
26
|
+
raise Exceptions::ValidationError, "Term #{term.inspect} contains an invalid character"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
term
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -21,6 +21,20 @@ module DeepL
|
|
|
21
21
|
|
|
22
22
|
Time.parse(time_string)
|
|
23
23
|
end
|
|
24
|
+
|
|
25
|
+
##
|
|
26
|
+
# Like +parse_optional_time+, but treats timestamps without a zone offset as UTC.
|
|
27
|
+
#
|
|
28
|
+
# @param [Object] time_string Timestamp in ISO 8601 format, or nil.
|
|
29
|
+
# @return [Time, nil] The parsed time, or nil if no valid timestamp was given.
|
|
30
|
+
def parse_optional_utc_time(time_string)
|
|
31
|
+
return nil unless time_string.is_a?(String) && !time_string.empty?
|
|
32
|
+
return Time.parse(time_string) if Date._parse(time_string).key?(:offset)
|
|
33
|
+
|
|
34
|
+
Time.parse("#{time_string}Z")
|
|
35
|
+
rescue ArgumentError
|
|
36
|
+
nil
|
|
37
|
+
end
|
|
24
38
|
end
|
|
25
39
|
end
|
|
26
40
|
end
|