deepl-rb 3.8.0 → 3.10.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.
Files changed (225) hide show
  1. checksums.yaml +4 -4
  2. data/.gitlab-ci.yml +71 -53
  3. data/.rubocop.yml +26 -0
  4. data/CHANGELOG.md +450 -17
  5. data/Gemfile +4 -0
  6. data/README.md +1119 -152
  7. data/Rakefile +18 -5
  8. data/VERSION +1 -1
  9. data/deepl-rb.gemspec +31 -3
  10. data/lib/deepl/admin_client.rb +183 -0
  11. data/lib/deepl/api.rb +30 -6
  12. data/lib/deepl/app_info.rb +20 -0
  13. data/lib/deepl/client.rb +969 -0
  14. data/lib/deepl/configuration.rb +56 -10
  15. data/lib/deepl/constants/admin_usage_group_by.rb +13 -0
  16. data/lib/deepl/constants/custom_tag_usage_aggregate_by.rb +13 -0
  17. data/lib/deepl/constants/document_status_code.rb +17 -0
  18. data/lib/deepl/constants/language_include.rb +17 -0
  19. data/lib/deepl/constants/language_resource_name.rb +21 -0
  20. data/lib/deepl/constants/tag_handling_version.rb +14 -0
  21. data/lib/deepl/deprecation.rb +42 -0
  22. data/lib/deepl/document_api.rb +45 -16
  23. data/lib/deepl/exceptions/bad_request.rb +1 -3
  24. data/lib/deepl/exceptions/connection_error.rb +37 -0
  25. data/lib/deepl/exceptions/document_not_ready_error.rb +17 -0
  26. data/lib/deepl/exceptions/document_translation_error.rb +17 -1
  27. data/lib/deepl/exceptions/error.rb +13 -1
  28. data/lib/deepl/exceptions/not_found.rb +8 -3
  29. data/lib/deepl/exceptions/not_supported.rb +6 -0
  30. data/lib/deepl/exceptions/request_error.rb +19 -2
  31. data/lib/deepl/exceptions/server_error.rb +7 -1
  32. data/lib/deepl/exceptions/validation_error.rb +14 -0
  33. data/lib/deepl/requests/admin/base.rb +94 -0
  34. data/lib/deepl/requests/admin/create_developer_key.rb +38 -0
  35. data/lib/deepl/requests/admin/custom_tag_usage_analytics.rb +38 -0
  36. data/lib/deepl/requests/admin/list_developer_keys.rb +28 -0
  37. data/lib/deepl/requests/admin/update_developer_key.rb +35 -0
  38. data/lib/deepl/requests/admin/usage_analytics.rb +36 -0
  39. data/lib/deepl/requests/base.rb +265 -43
  40. data/lib/deepl/requests/correct.rb +57 -0
  41. data/lib/deepl/requests/document/download.rb +11 -3
  42. data/lib/deepl/requests/document/get_status.rb +2 -2
  43. data/lib/deepl/requests/document/upload.rb +92 -10
  44. data/lib/deepl/requests/glossary/create.rb +6 -2
  45. data/lib/deepl/requests/glossary/language_pairs.rb +20 -4
  46. data/lib/deepl/requests/language_resources.rb +41 -0
  47. data/lib/deepl/requests/languages.rb +28 -4
  48. data/lib/deepl/requests/languages_for_resource.rb +54 -0
  49. data/lib/deepl/requests/multilingual_glossary/create.rb +37 -0
  50. data/lib/deepl/requests/multilingual_glossary/destroy.rb +36 -0
  51. data/lib/deepl/requests/multilingual_glossary/destroy_dictionary.rb +51 -0
  52. data/lib/deepl/requests/multilingual_glossary/entries.rb +66 -0
  53. data/lib/deepl/requests/multilingual_glossary/find.rb +35 -0
  54. data/lib/deepl/requests/multilingual_glossary/helpers.rb +63 -0
  55. data/lib/deepl/requests/multilingual_glossary/list.rb +39 -0
  56. data/lib/deepl/requests/multilingual_glossary/replace_dictionary.rb +41 -0
  57. data/lib/deepl/requests/multilingual_glossary/update.rb +40 -0
  58. data/lib/deepl/requests/rephrase.rb +5 -9
  59. data/lib/deepl/requests/spoken_terms/create.rb +37 -0
  60. data/lib/deepl/requests/spoken_terms/destroy.rb +36 -0
  61. data/lib/deepl/requests/spoken_terms/destroy_term_list.rb +41 -0
  62. data/lib/deepl/requests/spoken_terms/entries.rb +46 -0
  63. data/lib/deepl/requests/spoken_terms/find.rb +35 -0
  64. data/lib/deepl/requests/spoken_terms/helpers.rb +67 -0
  65. data/lib/deepl/requests/spoken_terms/list.rb +35 -0
  66. data/lib/deepl/requests/spoken_terms/replace_term_list.rb +41 -0
  67. data/lib/deepl/requests/spoken_terms/update.rb +40 -0
  68. data/lib/deepl/requests/style_profile/base.rb +83 -0
  69. data/lib/deepl/requests/style_profile/create.rb +35 -0
  70. data/lib/deepl/requests/style_profile/destroy.rb +34 -0
  71. data/lib/deepl/requests/style_profile/find.rb +33 -0
  72. data/lib/deepl/requests/style_profile/list.rb +43 -0
  73. data/lib/deepl/requests/style_profile/update.rb +35 -0
  74. data/lib/deepl/requests/style_rule/create.rb +1 -1
  75. data/lib/deepl/requests/style_rule/create_custom_instruction.rb +4 -2
  76. data/lib/deepl/requests/style_rule/destroy.rb +4 -2
  77. data/lib/deepl/requests/style_rule/destroy_custom_instruction.rb +5 -3
  78. data/lib/deepl/requests/style_rule/find.rb +4 -2
  79. data/lib/deepl/requests/style_rule/find_custom_instruction.rb +5 -3
  80. data/lib/deepl/requests/style_rule/helpers.rb +35 -0
  81. data/lib/deepl/requests/style_rule/list.rb +1 -1
  82. data/lib/deepl/requests/style_rule/update.rb +4 -2
  83. data/lib/deepl/requests/style_rule/update_configured_rules.rb +4 -2
  84. data/lib/deepl/requests/style_rule/update_custom_instruction.rb +5 -3
  85. data/lib/deepl/requests/translate.rb +55 -20
  86. data/lib/deepl/requests/translation_memory/base.rb +44 -0
  87. data/lib/deepl/requests/translation_memory/create_export.rb +41 -0
  88. data/lib/deepl/requests/translation_memory/create_import.rb +53 -0
  89. data/lib/deepl/requests/translation_memory/destroy.rb +37 -0
  90. data/lib/deepl/requests/translation_memory/download_export.rb +39 -0
  91. data/lib/deepl/requests/translation_memory/find.rb +38 -0
  92. data/lib/deepl/requests/translation_memory/find_job.rb +38 -0
  93. data/lib/deepl/requests/translation_memory/list.rb +6 -18
  94. data/lib/deepl/requests/translation_memory/segments.rb +47 -0
  95. data/lib/deepl/requests/translation_memory/storage_base.rb +79 -0
  96. data/lib/deepl/requests/translation_memory/upload_file.rb +47 -0
  97. data/lib/deepl/requests/usage.rb +28 -1
  98. data/lib/deepl/requests/write_improvements.rb +35 -0
  99. data/lib/deepl/resources/admin_usage_report.rb +73 -0
  100. data/lib/deepl/resources/custom_tag_usage_report.rb +59 -0
  101. data/lib/deepl/resources/developer_key.rb +57 -0
  102. data/lib/deepl/resources/document_handle.rb +24 -25
  103. data/lib/deepl/resources/document_translation_status.rb +23 -4
  104. data/lib/deepl/resources/language.rb +10 -2
  105. data/lib/deepl/resources/language_resource.rb +57 -0
  106. data/lib/deepl/resources/language_support.rb +56 -0
  107. data/lib/deepl/resources/multilingual_glossary.rb +112 -0
  108. data/lib/deepl/resources/spoken_terms.rb +61 -0
  109. data/lib/deepl/resources/style_profile.rb +44 -0
  110. data/lib/deepl/resources/style_rule.rb +3 -0
  111. data/lib/deepl/resources/text.rb +18 -2
  112. data/lib/deepl/resources/translation_memory.rb +10 -1
  113. data/lib/deepl/resources/translation_memory_export.rb +37 -0
  114. data/lib/deepl/resources/translation_memory_import.rb +29 -0
  115. data/lib/deepl/resources/translation_memory_job.rb +162 -0
  116. data/lib/deepl/resources/translation_memory_segments.rb +80 -0
  117. data/lib/deepl/resources/usage.rb +93 -4
  118. data/lib/deepl/style_rule_api.rb +11 -0
  119. data/lib/deepl/translation_memory_api.rb +373 -1
  120. data/lib/deepl/utils/client_builder.rb +54 -0
  121. data/lib/deepl/utils/document_poller.rb +70 -0
  122. data/lib/deepl/utils/exception_builder.rb +3 -2
  123. data/lib/deepl/utils/http_client_builder.rb +79 -0
  124. data/lib/deepl/utils/number_parser.rb +34 -0
  125. data/lib/deepl/utils/term_validator.rb +33 -0
  126. data/lib/deepl/utils/time_parser.rb +40 -0
  127. data/lib/deepl.rb +190 -34
  128. data/lib/http_client_options.rb +21 -1
  129. data/lib/version.rb +1 -1
  130. data/spec/fixtures.rb +33 -0
  131. data/spec/offline/admin_client_spec.rb +551 -0
  132. data/spec/offline/client_spec.rb +273 -0
  133. data/spec/{api → offline}/configuration_spec.rb +45 -3
  134. data/spec/{constants → offline}/constants_spec.rb +23 -1
  135. data/spec/offline/correct_text_spec.rb +50 -0
  136. data/spec/offline/deprecation_spec.rb +32 -0
  137. data/spec/offline/errors_spec.rb +92 -0
  138. data/spec/offline/glossaries_spec.rb +98 -0
  139. data/spec/offline/http_client_spec.rb +319 -0
  140. data/spec/offline/languages_spec.rb +155 -0
  141. data/spec/offline/module_methods_spec.rb +13 -0
  142. data/spec/offline/multilingual_glossaries_spec.rb +358 -0
  143. data/spec/offline/offline_helper.rb +71 -0
  144. data/spec/offline/rephrase_text_spec.rb +73 -0
  145. data/spec/offline/spoken_terms_spec.rb +215 -0
  146. data/spec/offline/style_profiles_spec.rb +266 -0
  147. data/spec/offline/style_rules_spec.rb +318 -0
  148. data/spec/offline/translate_document_spec.rb +606 -0
  149. data/spec/offline/translate_text_spec.rb +286 -0
  150. data/spec/offline/translation_memories_client_spec.rb +256 -0
  151. data/spec/offline/translation_memories_spec.rb +463 -0
  152. data/spec/offline/usage_spec.rb +151 -0
  153. data/spec/real_api/admin_client_spec.rb +77 -0
  154. data/spec/real_api/correct_text_spec.rb +20 -0
  155. data/spec/real_api/glossaries_spec.rb +28 -0
  156. data/spec/real_api/languages_spec.rb +27 -0
  157. data/spec/real_api/multilingual_glossaries_spec.rb +53 -0
  158. data/spec/real_api/proxy_spec.rb +41 -0
  159. data/spec/real_api/real_api_helper.rb +65 -0
  160. data/spec/real_api/rephrase_text_spec.rb +16 -0
  161. data/spec/real_api/spoken_terms_spec.rb +56 -0
  162. data/spec/real_api/style_profiles_spec.rb +46 -0
  163. data/spec/real_api/style_rules_spec.rb +46 -0
  164. data/spec/real_api/translate_document_spec.rb +18 -0
  165. data/spec/real_api/translate_text_spec.rb +16 -0
  166. data/spec/real_api/translation_memories_spec.rb +56 -0
  167. data/spec/real_api/usage_spec.rb +20 -0
  168. data/spec/spec_helper.rb +1 -50
  169. metadata +134 -63
  170. data/.circleci/config.yml +0 -27
  171. data/.github/workflows/add_issues_to_kanban.yml +0 -16
  172. data/spec/api/api_spec.rb +0 -20
  173. data/spec/api/deepl_spec.rb +0 -262
  174. data/spec/integration_tests/document_api_spec.rb +0 -164
  175. data/spec/integration_tests/document_error_paths_spec.rb +0 -33
  176. data/spec/integration_tests/glossary_api_spec.rb +0 -114
  177. data/spec/integration_tests/glossary_error_paths_spec.rb +0 -107
  178. data/spec/integration_tests/integration_test_utils.rb +0 -170
  179. data/spec/integration_tests/languages_api_spec.rb +0 -54
  180. data/spec/integration_tests/languages_error_paths_spec.rb +0 -25
  181. data/spec/integration_tests/rephrase_api_spec.rb +0 -90
  182. data/spec/integration_tests/rephrase_error_paths_spec.rb +0 -53
  183. data/spec/integration_tests/smoke_test_spec.rb +0 -24
  184. data/spec/integration_tests/style_rule_api_spec.rb +0 -117
  185. data/spec/integration_tests/style_rule_error_paths_spec.rb +0 -45
  186. data/spec/integration_tests/translate_api_spec.rb +0 -98
  187. data/spec/integration_tests/translate_error_paths_spec.rb +0 -48
  188. data/spec/integration_tests/translation_memory_api_spec.rb +0 -54
  189. data/spec/integration_tests/translation_memory_error_paths_spec.rb +0 -19
  190. data/spec/integration_tests/usage_api_spec.rb +0 -29
  191. data/spec/integration_tests/usage_error_paths_spec.rb +0 -18
  192. data/spec/requests/extra_body_parameters_types_spec.rb +0 -82
  193. data/spec/requests/glossary/create_spec.rb +0 -44
  194. data/spec/requests/glossary/destroy_spec.rb +0 -27
  195. data/spec/requests/glossary/entries_spec.rb +0 -27
  196. data/spec/requests/glossary/find_spec.rb +0 -28
  197. data/spec/requests/glossary/language_pairs_spec.rb +0 -27
  198. data/spec/requests/glossary/list_spec.rb +0 -27
  199. data/spec/requests/languages_spec.rb +0 -27
  200. data/spec/requests/rephrase_spec.rb +0 -46
  201. data/spec/requests/style_rule/create_custom_instruction_spec.rb +0 -30
  202. data/spec/requests/style_rule/create_spec.rb +0 -29
  203. data/spec/requests/style_rule/destroy_custom_instruction_spec.rb +0 -28
  204. data/spec/requests/style_rule/destroy_spec.rb +0 -27
  205. data/spec/requests/style_rule/find_custom_instruction_spec.rb +0 -29
  206. data/spec/requests/style_rule/find_spec.rb +0 -28
  207. data/spec/requests/style_rule/list_spec.rb +0 -27
  208. data/spec/requests/style_rule/update_configured_rules_spec.rb +0 -31
  209. data/spec/requests/style_rule/update_custom_instruction_spec.rb +0 -32
  210. data/spec/requests/style_rule/update_spec.rb +0 -29
  211. data/spec/requests/translate_spec.rb +0 -283
  212. data/spec/requests/translation_memory/list_spec.rb +0 -27
  213. data/spec/requests/usage_spec.rb +0 -27
  214. data/spec/resources/custom_instruction_spec.rb +0 -32
  215. data/spec/resources/glossary_spec.rb +0 -38
  216. data/spec/resources/language_pair_spec.rb +0 -23
  217. data/spec/resources/language_spec.rb +0 -45
  218. data/spec/resources/style_rule_spec.rb +0 -68
  219. data/spec/resources/text_spec.rb +0 -23
  220. data/spec/resources/translation_memory_spec.rb +0 -35
  221. data/spec/resources/usage_spec.rb +0 -35
  222. data/spec/support/live_mock_server.rb +0 -12
  223. data/spec/support/managed_glossary.rb +0 -17
  224. data/spec/support/managed_style_rule.rb +0 -17
  225. data/spec/support/managed_translation_memory.rb +0 -7
@@ -0,0 +1,162 @@
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
+ # The outcome of a translation memory import or export job.
10
+
11
+ class TranslationMemoryJobResult
12
+ STATUS_AWAITING_INPUT = 'awaiting_input'
13
+ STATUS_PROCESSING = 'processing'
14
+ STATUS_COMPLETED = 'completed'
15
+ STATUS_DOWNLOADED = 'downloaded'
16
+ STATUS_FAILED = 'failed'
17
+ STATUS_EXPIRED = 'expired'
18
+
19
+ attr_reader :status, :required_action, :download_url, :expires_at, :error_message,
20
+ :translation_memory_id, :skipped_segment_count
21
+
22
+ def initialize(result)
23
+ status_metadata = result['status_metadata'] || {}
24
+ error = result['error'] || {}
25
+
26
+ @status = result['status']
27
+ @required_action = status_metadata['required_action']
28
+ @download_url = result['download_url']
29
+ @expires_at = Utils::TimeParser.parse_optional_utc_time(result['expires_at'])
30
+ @error_message = error['message']
31
+ @translation_memory_id = result['translation_memory_id']
32
+ @skipped_segment_count = result['skipped_segment_count']
33
+ end
34
+
35
+ ##
36
+ # Checks if the job terminated. Note that this could be due to an error as well, but means
37
+ # no further waiting is necessary.
38
+ #
39
+ # @return [true] if so
40
+
41
+ def finished?
42
+ [STATUS_COMPLETED, STATUS_DOWNLOADED, STATUS_FAILED, STATUS_EXPIRED].include?(status)
43
+ end
44
+
45
+ ##
46
+ # Checks if the job is still waiting for the TMX file to be uploaded. Note that the API
47
+ # detects an upload asynchronously, so a job keeps reporting this status for a while after
48
+ # its file has been uploaded.
49
+ #
50
+ # @return [true] if so
51
+
52
+ def awaiting_input?
53
+ status == STATUS_AWAITING_INPUT
54
+ end
55
+
56
+ ##
57
+ # Checks if there was an error during the job, including it having expired.
58
+ #
59
+ # @return [true] if so
60
+
61
+ def error?
62
+ [STATUS_FAILED, STATUS_EXPIRED].include?(status)
63
+ end
64
+
65
+ def to_s
66
+ "TranslationMemoryJobResult: Status: #{status} - Error message: #{error_message}"
67
+ end
68
+ end
69
+
70
+ ##
71
+ # Status of a translation memory import or export job. The API returns exactly one result.
72
+
73
+ class TranslationMemoryJob < Base
74
+ OPERATION_IMPORT = 'import'
75
+ OPERATION_EXPORT = 'export'
76
+
77
+ attr_reader :job_id, :product, :operation, :creation_time, :updated_time, :results,
78
+ :translation_memory_id, :display_name, :source_content_type,
79
+ :source_content_length
80
+
81
+ def initialize(job, *args)
82
+ super(*args)
83
+ extract_basic_fields(job)
84
+ extract_parameters(job)
85
+ @results = (job['results'] || []).map { |result| TranslationMemoryJobResult.new(result) }
86
+ end
87
+
88
+ ##
89
+ # The single result of the job, or nil if the API returned none.
90
+ #
91
+ # @return [DeepL::Resources::TranslationMemoryJobResult, nil]
92
+
93
+ def result
94
+ results.first
95
+ end
96
+
97
+ def status
98
+ result&.status
99
+ end
100
+
101
+ def finished?
102
+ result ? result.finished? : false
103
+ end
104
+
105
+ def awaiting_input?
106
+ result ? result.awaiting_input? : false
107
+ end
108
+
109
+ def error?
110
+ result ? result.error? : false
111
+ end
112
+
113
+ ##
114
+ # Checks if the job succeeded: its status is +completed+. Unlike +finished?+, this is
115
+ # false for a failed or expired job.
116
+ #
117
+ # @return [Boolean]
118
+
119
+ def done?
120
+ status == TranslationMemoryJobResult::STATUS_COMPLETED
121
+ end
122
+
123
+ ##
124
+ # Checks if the job has not failed: its status is not +failed+ or +expired+. Also true
125
+ # while the job is still running.
126
+ #
127
+ # @return [Boolean]
128
+
129
+ def ok?
130
+ !error?
131
+ end
132
+
133
+ def error_message
134
+ result&.error_message
135
+ end
136
+
137
+ def to_s
138
+ "TranslationMemoryJob: #{operation} - ID: #{job_id} - Status: #{status}"
139
+ end
140
+
141
+ private
142
+
143
+ def extract_basic_fields(job)
144
+ @job_id = job['job_id']
145
+ @product = job['product']
146
+ @operation = job['operation']
147
+ @creation_time = Utils::TimeParser.parse_optional_utc_time(job['creation_time'])
148
+ @updated_time = Utils::TimeParser.parse_optional_utc_time(job['updated_time'])
149
+ end
150
+
151
+ def extract_parameters(job)
152
+ parameters = job['parameters'] || {}
153
+ source_file = job['source_file'] || {}
154
+
155
+ @translation_memory_id = parameters['translation_memory_id']
156
+ @display_name = parameters['display_name']
157
+ @source_content_type = source_file['content_type']
158
+ @source_content_length = source_file['content_length']
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,80 @@
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
+ class TranslationMemoryTargetSegment
9
+ attr_reader :target_segment_id, :target_language, :target_text, :creation_time,
10
+ :updated_time, :last_used_time
11
+
12
+ def initialize(target_segment)
13
+ @target_segment_id = target_segment['target_segment_id']
14
+ @target_language = target_segment['target_language']
15
+ @target_text = target_segment['target_text']
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
+ )
21
+ end
22
+
23
+ def to_s
24
+ "#{target_language}: #{target_text}"
25
+ end
26
+ end
27
+
28
+ class TranslationMemorySegment
29
+ attr_reader :source_segment_id, :source_text, :targets, :creation_time, :updated_time,
30
+ :last_used_time
31
+
32
+ def initialize(segment)
33
+ @source_segment_id = segment['source_segment_id']
34
+ @source_text = segment['source_text']
35
+ @targets = (segment['targets'] || []).map do |target|
36
+ TranslationMemoryTargetSegment.new(target)
37
+ end
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'])
41
+ end
42
+
43
+ def to_s
44
+ "#{source_segment_id} - #{source_text}"
45
+ end
46
+ end
47
+
48
+ ##
49
+ # One page of the segments of a translation memory. Pagination is cursor-based: pass
50
+ # `next_page_cursor` as the `page_cursor` option of the next request until it is nil.
51
+
52
+ class TranslationMemorySegments < Base
53
+ attr_reader :segments, :segment_count, :next_page_cursor
54
+
55
+ def initialize(segments_response, *args)
56
+ super(*args)
57
+ @segments = (segments_response['segments'] || []).map do |segment|
58
+ TranslationMemorySegment.new(segment)
59
+ end
60
+ # Note that this is the number of segments stored in the translation memory, it is not
61
+ # reduced by a text filter.
62
+ @segment_count = segments_response['segment_count'] || 0
63
+ @next_page_cursor = segments_response['next_page_cursor']
64
+ end
65
+
66
+ ##
67
+ # Checks whether another page of segments can be requested.
68
+ #
69
+ # @return [true] if so
70
+
71
+ def next_page?
72
+ !next_page_cursor.nil?
73
+ end
74
+
75
+ def to_s
76
+ "#{segments.size} of #{segment_count} segment(s)"
77
+ end
78
+ end
79
+ end
80
+ end
@@ -6,21 +6,110 @@
6
6
  module DeepL
7
7
  module Resources
8
8
  class Usage < Base
9
- attr_reader :character_count, :character_limit
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
- def initialize(character_count, character_limit, *args)
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[document team_document api_key_character speech_to_text_minutes
29
+ speech_to_speech_minutes].freeze
30
+ KEYWORD_FIELDS = (DETAIL_TYPES + %i[products start_time end_time]).freeze
31
+
32
+ attr_reader :character, *KEYWORD_FIELDS
33
+
34
+ ##
35
+ # Each keyword argument is nil when the response does not contain the field.
36
+ #
37
+ # @param [Detail, nil] document Document usage, for accounts with a document limit.
38
+ # @param [Detail, nil] team_document Document usage of the whole team.
39
+ # @param [Detail, nil] api_key_character Character usage of this API key (API Pro).
40
+ # @param [Detail, nil] speech_to_text_minutes Speech-to-text usage in minutes (API Pro).
41
+ # @param [Detail, nil] speech_to_speech_minutes Speech-to-speech usage in minutes (API Pro).
42
+ # @param [Array<UsageProduct>, nil] products Per-product usage (API Pro).
43
+ # @param [Time, nil] start_time Start of the current billing period (API Pro).
44
+ # @param [Time, nil] end_time End of the current billing period (API Pro).
45
+
46
+ def initialize(character_count, character_limit, *args, **fields)
12
47
  super(*args)
13
48
 
49
+ unknown = fields.keys - KEYWORD_FIELDS
50
+ raise ArgumentError, "unknown keywords: #{unknown.join(', ')}" unless unknown.empty?
51
+
14
52
  @character_count = character_count
15
53
  @character_limit = character_limit
54
+ @character = Detail.new(character_count, character_limit) unless
55
+ character_count.nil? || character_limit.nil?
56
+ KEYWORD_FIELDS.each { |field| instance_variable_set("@#{field}", fields[field]) }
57
+ end
58
+
59
+ ##
60
+ # @deprecated Use +character.count+ instead.
61
+ def character_count
62
+ deprecated(:character_count, 'character.count')
63
+ @character_count
64
+ end
65
+
66
+ ##
67
+ # @deprecated Use +character.limit+ instead.
68
+ def character_limit
69
+ deprecated(:character_limit, 'character.limit')
70
+ @character_limit
16
71
  end
17
72
 
18
73
  def to_s
19
- "#{character_count} / #{character_limit}"
74
+ "#{@character_count} / #{@character_limit}"
20
75
  end
21
76
 
77
+ ##
78
+ # @deprecated Use +character.limit_reached?+ or +any_limit_reached?+ instead.
22
79
  def quota_exceeded?
23
- character_count >= character_limit
80
+ deprecated(:quota_exceeded?, 'character.limit_reached? or any_limit_reached?')
81
+ @character_count >= @character_limit
82
+ end
83
+
84
+ ##
85
+ # True if the character, document or team document limit has been reached. The API Pro
86
+ # usage types (api_key_character and the speech minutes) are not considered; call
87
+ # limit_reached? on them directly.
88
+ def any_limit_reached?
89
+ [character, document, team_document].compact.any?(&:limit_reached?)
90
+ end
91
+
92
+ private
93
+
94
+ def deprecated(name, replacement)
95
+ Deprecation.warn_once("DeepL::Resources::Usage##{name}",
96
+ "[DEPRECATION] DeepL::Resources::Usage##{name} is deprecated, " \
97
+ "use #{replacement} instead.", uplevel: 3)
98
+ end
99
+ end
100
+
101
+ class UsageProduct
102
+ attr_reader :product_type, :billing_unit, :api_key_unit_count, :account_unit_count
103
+
104
+ def initialize(product)
105
+ @product_type = product['product_type']
106
+ @billing_unit = product['billing_unit']
107
+ @api_key_unit_count = product['api_key_unit_count']
108
+ @account_unit_count = product['account_unit_count']
109
+ end
110
+
111
+ def to_s
112
+ "#{product_type}: #{api_key_unit_count} / #{account_unit_count} #{billing_unit}"
24
113
  end
25
114
  end
26
115
  end
@@ -4,47 +4,57 @@
4
4
  # frozen_string_literal: true
5
5
 
6
6
  module DeepL
7
+ # @deprecated Use the style rule methods of DeepL::Client instead.
7
8
  class StyleRuleApi
8
9
  def initialize(api, options = {})
9
10
  @api = api
10
11
  @options = options
11
12
  end
12
13
 
14
+ # @deprecated Use DeepL::Client#get_all_style_rules instead.
13
15
  def list(options = {})
14
16
  DeepL::Requests::StyleRule::List.new(@api, options).request
15
17
  end
16
18
 
19
+ # @deprecated Use DeepL::Client#create_style_rule instead.
17
20
  def create(name, language, options = {})
18
21
  DeepL::Requests::StyleRule::Create.new(@api, name, language, options).request
19
22
  end
20
23
 
24
+ # @deprecated Use DeepL::Client#get_style_rule instead.
21
25
  def find(style_id, options = {})
22
26
  DeepL::Requests::StyleRule::Find.new(@api, style_id, options).request
23
27
  end
24
28
 
29
+ # @deprecated Use DeepL::Client#update_style_rule_name instead.
25
30
  def update_name(style_id, name, options = {})
26
31
  DeepL::Requests::StyleRule::Update.new(@api, style_id, name, options).request
27
32
  end
28
33
 
34
+ # @deprecated Use DeepL::Client#delete_style_rule instead.
29
35
  def destroy(style_id, options = {})
30
36
  DeepL::Requests::StyleRule::Destroy.new(@api, style_id, options).request
31
37
  end
32
38
 
39
+ # @deprecated Use DeepL::Client#update_style_rule_configured_rules instead.
33
40
  def update_configured_rules(style_id, configured_rules, options = {})
34
41
  DeepL::Requests::StyleRule::UpdateConfiguredRules.new(@api, style_id, configured_rules,
35
42
  options).request
36
43
  end
37
44
 
45
+ # @deprecated Use DeepL::Client#create_style_rule_custom_instruction instead.
38
46
  def create_custom_instruction(style_id, label, prompt, source_language = nil, options = {})
39
47
  DeepL::Requests::StyleRule::CreateCustomInstruction.new(@api, style_id, label, prompt,
40
48
  source_language, options).request
41
49
  end
42
50
 
51
+ # @deprecated Use DeepL::Client#get_style_rule_custom_instruction instead.
43
52
  def find_custom_instruction(style_id, instruction_id, options = {})
44
53
  DeepL::Requests::StyleRule::FindCustomInstruction.new(@api, style_id, instruction_id,
45
54
  options).request
46
55
  end
47
56
 
57
+ # @deprecated Use DeepL::Client#update_style_rule_custom_instruction instead.
48
58
  def update_custom_instruction(style_id, instruction_id, label,
49
59
  prompt, source_language = nil,
50
60
  options = {})
@@ -53,6 +63,7 @@ module DeepL
53
63
  options).request
54
64
  end
55
65
 
66
+ # @deprecated Use DeepL::Client#delete_style_rule_custom_instruction instead.
56
67
  def destroy_custom_instruction(style_id, instruction_id, options = {})
57
68
  DeepL::Requests::StyleRule::DestroyCustomInstruction.new(@api, style_id, instruction_id,
58
69
  options).request