deepl-rb 3.6.0 → 3.7.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 (131) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +27 -0
  3. data/.github/workflows/add_issues_to_kanban.yml +16 -0
  4. data/.gitlab-ci.yml +177 -0
  5. data/.rubocop.yml +51 -0
  6. data/CHANGELOG.md +40 -3
  7. data/CODE_OF_CONDUCT.md +132 -0
  8. data/CONTRIBUTING.md +37 -0
  9. data/Gemfile +24 -0
  10. data/README.md +153 -22
  11. data/Rakefile +58 -0
  12. data/SECURITY.md +58 -0
  13. data/VERSION +1 -0
  14. data/deepl-rb.gemspec +167 -0
  15. data/lib/deepl/api.rb +22 -0
  16. data/lib/deepl/configuration.rb +59 -0
  17. data/lib/deepl/constants/base_constant.rb +18 -0
  18. data/lib/deepl/constants/formality.rb +16 -0
  19. data/lib/deepl/constants/model_type.rb +14 -0
  20. data/lib/deepl/constants/split_sentences.rb +14 -0
  21. data/lib/deepl/constants/tag_handling.rb +13 -0
  22. data/lib/deepl/constants/tone.rb +20 -0
  23. data/lib/deepl/constants/writing_style.rb +20 -0
  24. data/lib/deepl/document_api.rb +121 -0
  25. data/lib/deepl/exceptions/authorization_failed.rb +14 -0
  26. data/lib/deepl/exceptions/bad_request.rb +16 -0
  27. data/lib/deepl/exceptions/document_translation_error.rb +15 -0
  28. data/lib/deepl/exceptions/error.rb +14 -0
  29. data/lib/deepl/exceptions/limit_exceeded.rb +18 -0
  30. data/lib/deepl/exceptions/not_found.rb +16 -0
  31. data/lib/deepl/exceptions/not_supported.rb +14 -0
  32. data/lib/deepl/exceptions/quota_exceeded.rb +14 -0
  33. data/lib/deepl/exceptions/request_entity_too_large.rb +15 -0
  34. data/lib/deepl/exceptions/request_error.rb +21 -0
  35. data/lib/deepl/exceptions/server_error.rb +18 -0
  36. data/lib/deepl/glossary_api.rb +38 -0
  37. data/lib/deepl/requests/base.rb +212 -0
  38. data/lib/deepl/requests/document/download.rb +44 -0
  39. data/lib/deepl/requests/document/get_status.rb +44 -0
  40. data/lib/deepl/requests/document/upload.rb +75 -0
  41. data/lib/deepl/requests/glossary/create.rb +59 -0
  42. data/lib/deepl/requests/glossary/destroy.rb +37 -0
  43. data/lib/deepl/requests/glossary/entries.rb +37 -0
  44. data/lib/deepl/requests/glossary/find.rb +38 -0
  45. data/lib/deepl/requests/glossary/language_pairs.rb +38 -0
  46. data/lib/deepl/requests/glossary/list.rb +37 -0
  47. data/lib/deepl/requests/languages.rb +37 -0
  48. data/lib/deepl/requests/rephrase.rb +55 -0
  49. data/lib/deepl/requests/style_rule/create.rb +46 -0
  50. data/lib/deepl/requests/style_rule/create_custom_instruction.rb +45 -0
  51. data/lib/deepl/requests/style_rule/destroy.rb +39 -0
  52. data/lib/deepl/requests/style_rule/destroy_custom_instruction.rb +40 -0
  53. data/lib/deepl/requests/style_rule/find.rb +40 -0
  54. data/lib/deepl/requests/style_rule/find_custom_instruction.rb +41 -0
  55. data/lib/deepl/requests/style_rule/list.rb +59 -0
  56. data/lib/deepl/requests/style_rule/update.rb +42 -0
  57. data/lib/deepl/requests/style_rule/update_configured_rules.rb +41 -0
  58. data/lib/deepl/requests/style_rule/update_custom_instruction.rb +47 -0
  59. data/lib/deepl/requests/translate.rb +106 -0
  60. data/lib/deepl/requests/translation_memory/list.rb +58 -0
  61. data/lib/deepl/requests/usage.rb +33 -0
  62. data/lib/deepl/resources/base.rb +17 -0
  63. data/lib/deepl/resources/document_handle.rb +57 -0
  64. data/lib/deepl/resources/document_translation_status.rb +54 -0
  65. data/lib/deepl/resources/glossary.rb +28 -0
  66. data/lib/deepl/resources/language.rb +30 -0
  67. data/lib/deepl/resources/language_pair.rb +23 -0
  68. data/lib/deepl/resources/style_rule.rb +86 -0
  69. data/lib/deepl/resources/text.rb +24 -0
  70. data/lib/deepl/resources/translation_memory.rb +25 -0
  71. data/lib/deepl/resources/usage.rb +27 -0
  72. data/lib/deepl/style_rule_api.rb +61 -0
  73. data/lib/deepl/translation_memory_api.rb +17 -0
  74. data/lib/deepl/utils/backoff_timer.rb +46 -0
  75. data/lib/deepl/utils/exception_builder.rb +34 -0
  76. data/lib/deepl.rb +175 -0
  77. data/lib/http_client_options.rb +22 -0
  78. data/lib/version.rb +8 -0
  79. data/spec/api/api_spec.rb +20 -0
  80. data/spec/api/configuration_spec.rb +122 -0
  81. data/spec/api/deepl_spec.rb +460 -0
  82. data/spec/constants/constants_spec.rb +158 -0
  83. data/spec/fixtures/vcr_cassettes/deepl_document.yml +95 -0
  84. data/spec/fixtures/vcr_cassettes/deepl_document_download.yml +1214 -0
  85. data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +1163 -0
  86. data/spec/fixtures/vcr_cassettes/deepl_languages.yml +54 -0
  87. data/spec/fixtures/vcr_cassettes/deepl_rephrase.yml +87 -0
  88. data/spec/fixtures/vcr_cassettes/deepl_translate.yml +358 -0
  89. data/spec/fixtures/vcr_cassettes/deepl_usage.yml +129 -0
  90. data/spec/fixtures/vcr_cassettes/glossaries.yml +1702 -0
  91. data/spec/fixtures/vcr_cassettes/languages.yml +229 -0
  92. data/spec/fixtures/vcr_cassettes/rephrase_texts.yml +401 -0
  93. data/spec/fixtures/vcr_cassettes/style_rules.yml +92 -0
  94. data/spec/fixtures/vcr_cassettes/style_rules_crud.yml +926 -0
  95. data/spec/fixtures/vcr_cassettes/translate_texts.yml +10630 -0
  96. data/spec/fixtures/vcr_cassettes/translation_memories.yml +74 -0
  97. data/spec/fixtures/vcr_cassettes/usage.yml +171 -0
  98. data/spec/integration_tests/document_api_spec.rb +178 -0
  99. data/spec/integration_tests/integration_test_utils.rb +170 -0
  100. data/spec/integration_tests/style_rule_api_spec.rb +135 -0
  101. data/spec/integration_tests/translation_memory_api_spec.rb +70 -0
  102. data/spec/requests/extra_body_parameters_types_spec.rb +82 -0
  103. data/spec/requests/glossary/create_spec.rb +65 -0
  104. data/spec/requests/glossary/destroy_spec.rb +66 -0
  105. data/spec/requests/glossary/entries_spec.rb +62 -0
  106. data/spec/requests/glossary/find_spec.rb +68 -0
  107. data/spec/requests/glossary/language_pairs_spec.rb +40 -0
  108. data/spec/requests/glossary/list_spec.rb +54 -0
  109. data/spec/requests/languages_spec.rb +68 -0
  110. data/spec/requests/rephrase_spec.rb +172 -0
  111. data/spec/requests/style_rule/create_custom_instruction_spec.rb +54 -0
  112. data/spec/requests/style_rule/create_spec.rb +45 -0
  113. data/spec/requests/style_rule/destroy_custom_instruction_spec.rb +54 -0
  114. data/spec/requests/style_rule/destroy_spec.rb +54 -0
  115. data/spec/requests/style_rule/find_custom_instruction_spec.rb +56 -0
  116. data/spec/requests/style_rule/find_spec.rb +56 -0
  117. data/spec/requests/style_rule/list_spec.rb +58 -0
  118. data/spec/requests/style_rule/update_configured_rules_spec.rb +52 -0
  119. data/spec/requests/style_rule/update_custom_instruction_spec.rb +58 -0
  120. data/spec/requests/style_rule/update_spec.rb +48 -0
  121. data/spec/requests/translate_spec.rb +492 -0
  122. data/spec/requests/translation_memory/list_spec.rb +61 -0
  123. data/spec/requests/usage_spec.rb +43 -0
  124. data/spec/resources/glossary_spec.rb +38 -0
  125. data/spec/resources/language_pair_spec.rb +23 -0
  126. data/spec/resources/language_spec.rb +45 -0
  127. data/spec/resources/text_spec.rb +23 -0
  128. data/spec/resources/translation_memory_spec.rb +35 -0
  129. data/spec/resources/usage_spec.rb +35 -0
  130. data/spec/spec_helper.rb +92 -0
  131. metadata +130 -3
@@ -0,0 +1,135 @@
1
+ # Copyright 2025 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
+ require 'spec_helper'
7
+
8
+ describe DeepL::StyleRuleApi do
9
+ before do
10
+ VCR.turn_off!
11
+ WebMock.allow_net_connect!
12
+ end
13
+
14
+ after do
15
+ VCR.turn_on!
16
+ WebMock.disable_net_connect!
17
+ end
18
+
19
+ describe '#translate_with_style_rules' do
20
+ it 'when performing a request with style_id' do
21
+ skip 'Only runs on mock server' if real_server?
22
+
23
+ source_lang = 'DE'
24
+ target_lang = 'EN-US'
25
+ text = 'Hallo, Welt!'
26
+ style_id = 'dca2e053-8ae5-45e6-a0d2-881156e7f4e4'
27
+
28
+ result = DeepL.translate(text, source_lang, target_lang, { style_rule: style_id })
29
+ expect(result).to be_a(DeepL::Resources::Text)
30
+ end
31
+
32
+ it 'when performing a request with style_rule object' do
33
+ skip 'Only runs on mock server' if real_server?
34
+
35
+ source_lang = 'DE'
36
+ target_lang = 'EN-US'
37
+ text = 'Hallo, Welt!'
38
+ style_rule = build_test_style_rule
39
+
40
+ result = DeepL.translate(text, source_lang, target_lang, { style_rule: style_rule })
41
+ expect(result).to be_a(DeepL::Resources::Text)
42
+ end
43
+ end
44
+
45
+ describe '#list_style_rules' do
46
+ it 'when requesting a list of all style rules' do
47
+ skip 'Only runs on mock server' if real_server?
48
+ style_rules = DeepL.style_rules.list(page: 0, page_size: 10, detailed: true)
49
+ expect(style_rules).to be_an(Array)
50
+ expect(style_rules.length).to eq(1)
51
+ expect(style_rules[0].style_id).to eq('dca2e053-8ae5-45e6-a0d2-881156e7f4e4')
52
+ expect(style_rules[0].name).to eq('Default Style Rule')
53
+ expect(style_rules[0].configured_rules).to be_a(DeepL::Resources::ConfiguredRules)
54
+ expect(style_rules[0].custom_instructions).to be_an(Array)
55
+ end
56
+
57
+ it 'when requesting a list of all style rules without detailed' do
58
+ skip 'Only runs on mock server' if real_server?
59
+ style_rules = DeepL.style_rules.list
60
+ expect(style_rules).to be_an(Array)
61
+ expect(style_rules.length).to eq(1)
62
+ expect(style_rules[0].style_id).to eq('dca2e053-8ae5-45e6-a0d2-881156e7f4e4')
63
+ expect(style_rules[0].configured_rules).to be_nil
64
+ expect(style_rules[0].custom_instructions).to be_nil
65
+ end
66
+ end
67
+
68
+ describe 'style rule management operations' do
69
+ it 'when performing all management operations on style rules' do # rubocop:disable RSpec/ExampleLength,RSpec/MultipleExpectations
70
+ skip 'Only runs on mock server' if real_server?
71
+
72
+ # Create a style rule
73
+ style_rule = DeepL.style_rules.create('Test Style Rule', 'en')
74
+ expect(style_rule).to be_a(DeepL::Resources::StyleRule)
75
+ expect(style_rule.style_id).not_to be_nil
76
+ style_id = style_rule.style_id
77
+
78
+ # Find the style rule
79
+ found_rule = DeepL.style_rules.find(style_id)
80
+ expect(found_rule).to be_a(DeepL::Resources::StyleRule)
81
+ expect(found_rule.style_id).to eq(style_id)
82
+
83
+ # Update the style rule name
84
+ updated_rule = DeepL.style_rules.update_name(style_id, 'Updated Style Rule')
85
+ expect(updated_rule).to be_a(DeepL::Resources::StyleRule)
86
+
87
+ # Update configured rules
88
+ configured_rules = {
89
+ 'dates_and_times' => { 'calendar_era' => 'use_bc_and_ad' }
90
+ }
91
+ updated_rule = DeepL.style_rules.update_configured_rules(style_id, configured_rules)
92
+ expect(updated_rule).to be_a(DeepL::Resources::StyleRule)
93
+
94
+ # Create a custom instruction
95
+ custom_instruction = DeepL.style_rules.create_custom_instruction(
96
+ style_id, 'Test Instruction', 'Always use formal language'
97
+ )
98
+ expect(custom_instruction).to be_a(DeepL::Resources::CustomInstruction)
99
+ expect(custom_instruction.id).not_to be_nil
100
+ instruction_id = custom_instruction.id
101
+
102
+ # Find a custom instruction
103
+ fetched_instruction = DeepL.style_rules.find_custom_instruction(style_id, instruction_id)
104
+ expect(fetched_instruction).to be_a(DeepL::Resources::CustomInstruction)
105
+ expect(fetched_instruction.id).to eq(instruction_id)
106
+
107
+ # Update a custom instruction
108
+ updated_instruction = DeepL.style_rules.update_custom_instruction(
109
+ style_id, instruction_id, 'Updated Instruction', 'Use casual language'
110
+ )
111
+ expect(updated_instruction).to be_a(DeepL::Resources::CustomInstruction)
112
+
113
+ # Destroy the custom instruction
114
+ deleted_instruction_id = DeepL.style_rules.destroy_custom_instruction(style_id,
115
+ instruction_id)
116
+ expect(deleted_instruction_id).to eq(instruction_id)
117
+
118
+ # Destroy the style rule
119
+ deleted_style_id = DeepL.style_rules.destroy(style_id)
120
+ expect(deleted_style_id).to eq(style_id)
121
+ end
122
+ end
123
+
124
+ def build_test_style_rule
125
+ style_rule_data = {
126
+ 'style_id' => 'dca2e053-8ae5-45e6-a0d2-881156e7f4e4',
127
+ 'name' => 'Default Style Rule',
128
+ 'creation_time' => '2025-01-01T00:00:00Z',
129
+ 'updated_time' => '2025-01-01T00:00:00Z',
130
+ 'language' => 'en',
131
+ 'version' => 1
132
+ }
133
+ DeepL::Resources::StyleRule.new(style_rule_data, nil, nil)
134
+ end
135
+ 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
+ require 'spec_helper'
7
+
8
+ describe DeepL::TranslationMemoryApi do
9
+ before do
10
+ VCR.turn_off!
11
+ WebMock.allow_net_connect!
12
+ end
13
+
14
+ after do
15
+ VCR.turn_on!
16
+ WebMock.disable_net_connect!
17
+ end
18
+
19
+ describe '#translate_with_translation_memory' do
20
+ it 'when performing a request with translation_memory_id' do
21
+ skip 'Only runs on mock server' if real_server?
22
+
23
+ source_lang = 'DE'
24
+ target_lang = 'EN'
25
+ text = 'Protonenstrahl'
26
+ translation_memory_id = 'a74d88fb-ed2a-4943-a664-a4512398b994'
27
+
28
+ result = DeepL.translate(text, source_lang, target_lang,
29
+ { translation_memory: translation_memory_id })
30
+ expect(result).to be_a(DeepL::Resources::Text)
31
+ end
32
+
33
+ it 'when performing a request with translation_memory resource object' do
34
+ skip 'Only runs on mock server' if real_server?
35
+
36
+ source_lang = 'DE'
37
+ target_lang = 'EN'
38
+ text = 'Protonenstrahl'
39
+ translation_memory = build_test_translation_memory
40
+
41
+ result = DeepL.translate(text, source_lang, target_lang,
42
+ { translation_memory: translation_memory })
43
+ expect(result).to be_a(DeepL::Resources::Text)
44
+ end
45
+ end
46
+
47
+ describe '#list_translation_memories' do
48
+ it 'when requesting a list of all translation memories' do
49
+ skip 'Only runs on mock server' if real_server?
50
+
51
+ translation_memories = DeepL.translation_memories.list
52
+ expect(translation_memories).to be_an(Array)
53
+ expect(translation_memories).not_to be_empty
54
+ expect(translation_memories.first).to be_a(DeepL::Resources::TranslationMemory)
55
+ expect(translation_memories.first.translation_memory_id).not_to be_nil
56
+ expect(translation_memories.first.name).not_to be_nil
57
+ end
58
+ end
59
+
60
+ def build_test_translation_memory
61
+ tm_data = {
62
+ 'translation_memory_id' => 'a74d88fb-ed2a-4943-a664-a4512398b994',
63
+ 'name' => 'Default Translation Memory',
64
+ 'source_language' => 'DE',
65
+ 'target_languages' => %w[EN ES FR],
66
+ 'segment_count' => 3542
67
+ }
68
+ DeepL::Resources::TranslationMemory.new(tm_data, nil, nil)
69
+ end
70
+ end
@@ -0,0 +1,82 @@
1
+ # Copyright 2025 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
+ require 'spec_helper'
7
+
8
+ describe 'DeepL::Requests::Base extra_body_parameters type preservation' do
9
+ let(:api) { build_deepl_api }
10
+
11
+ around do |tests|
12
+ tmp_env = replace_env_preserving_deepl_vars_except_mock_server
13
+ tests.call
14
+ ENV.replace(tmp_env)
15
+ end
16
+
17
+ describe 'apply_extra_body_parameters_to_json' do
18
+ it 'preserves boolean true' do
19
+ request = DeepL::Requests::Translate.new(
20
+ api, 'test', 'EN', 'ES',
21
+ extra_body_parameters: { enable_beta_languages: true }
22
+ )
23
+
24
+ payload = {}
25
+ request.send(:apply_extra_body_parameters_to_json, payload)
26
+
27
+ expect(payload[:enable_beta_languages]).to be(true)
28
+ expect(payload[:enable_beta_languages]).to be_a(TrueClass)
29
+ end
30
+
31
+ it 'preserves boolean false' do
32
+ request = DeepL::Requests::Translate.new(
33
+ api, 'test', 'EN', 'ES',
34
+ extra_body_parameters: { some_flag: false }
35
+ )
36
+
37
+ payload = {}
38
+ request.send(:apply_extra_body_parameters_to_json, payload)
39
+
40
+ expect(payload[:some_flag]).to be(false)
41
+ expect(payload[:some_flag]).to be_a(FalseClass)
42
+ end
43
+
44
+ it 'preserves integers' do
45
+ request = DeepL::Requests::Translate.new(
46
+ api, 'test', 'EN', 'ES',
47
+ extra_body_parameters: { some_number: 42 }
48
+ )
49
+
50
+ payload = {}
51
+ request.send(:apply_extra_body_parameters_to_json, payload)
52
+
53
+ expect(payload[:some_number]).to eq(42)
54
+ expect(payload[:some_number]).to be_a(Integer)
55
+ end
56
+
57
+ it 'preserves strings' do
58
+ request = DeepL::Requests::Translate.new(
59
+ api, 'test', 'EN', 'ES',
60
+ extra_body_parameters: { some_string: 'hello' }
61
+ )
62
+
63
+ payload = {}
64
+ request.send(:apply_extra_body_parameters_to_json, payload)
65
+
66
+ expect(payload[:some_string]).to eq('hello')
67
+ expect(payload[:some_string]).to be_a(String)
68
+ end
69
+
70
+ it 'allows overriding standard parameters' do
71
+ request = DeepL::Requests::Translate.new(
72
+ api, 'test', 'EN', 'ES',
73
+ extra_body_parameters: { target_lang: 'FR' }
74
+ )
75
+
76
+ payload = { target_lang: 'ES' }
77
+ request.send(:apply_extra_body_parameters_to_json, payload)
78
+
79
+ expect(payload[:target_lang]).to eq('FR')
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,65 @@
1
+ # Copyright 2022 Daniel Herzog
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
+ require 'spec_helper'
7
+
8
+ describe DeepL::Requests::Glossary::Create do
9
+ subject(:create) do
10
+ described_class.new(api, name, source_lang, target_lang, entries, options)
11
+ end
12
+
13
+ around do |tests|
14
+ tmp_env = replace_env_preserving_deepl_vars_except_mock_server
15
+ tests.call
16
+ ENV.replace(tmp_env)
17
+ end
18
+
19
+ let(:api) { build_deepl_api }
20
+ let(:name) { 'Mi Glosario' }
21
+ let(:source_lang) { 'EN' }
22
+ let(:target_lang) { 'ES' }
23
+ let(:entries) do
24
+ [
25
+ %w[Hello Hola],
26
+ %w[World Mundo]
27
+ ]
28
+ end
29
+ let(:entries_format) { 'tsv' }
30
+ let(:options) { {} }
31
+
32
+ describe '#initialize' do
33
+ context 'when building a request' do
34
+ it 'creates a request object' do
35
+ expect(create).to be_a(described_class)
36
+ end
37
+
38
+ it 'sets the default value for the entries format if not specified' do
39
+ request = described_class.new(api, name, source_lang, target_lang,
40
+ entries, options)
41
+ expect(request.entries_format).to eq('tsv')
42
+ end
43
+ end
44
+ end
45
+
46
+ describe '#request' do
47
+ around do |example|
48
+ VCR.use_cassette('glossaries') { example.call }
49
+ end
50
+
51
+ context 'when performing a valid request with two glossary entries' do
52
+ it 'returns a glossaries object' do
53
+ glossary = create.request
54
+ expect(glossary).to be_a(DeepL::Resources::Glossary)
55
+ expect(glossary.id).to be_a(String)
56
+ expect(glossary.name).to eq('Mi Glosario')
57
+ expect(glossary.ready).to be(true).or be(false)
58
+ expect(glossary.source_lang).to eq('en')
59
+ expect(glossary.target_lang).to eq('es')
60
+ expect { Time.iso8601(glossary.creation_time) }.not_to raise_error
61
+ expect(glossary.entry_count).to eq(2)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,66 @@
1
+ # Copyright 2022 Daniel Herzog
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
+ require 'spec_helper'
7
+
8
+ describe DeepL::Requests::Glossary::Destroy do
9
+ subject(:destroy) { described_class.new(api, id) }
10
+
11
+ around do |tests|
12
+ tmp_env = replace_env_preserving_deepl_vars_except_mock_server
13
+ tests.call
14
+ ENV.replace(tmp_env)
15
+ end
16
+
17
+ let(:api) { build_deepl_api }
18
+ let(:id) { '9ab5dac2-b7b2-4b4a-808a-e8e305df5ecb' }
19
+
20
+ describe '#initialize' do
21
+ context 'when building a request' do
22
+ it 'creates a request object' do
23
+ expect(destroy).to be_a(described_class)
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '#request' do
29
+ around do |example|
30
+ VCR.use_cassette('glossaries') { example.call }
31
+ end
32
+
33
+ context 'when performing a valid request' do
34
+ subject(:destroy) { described_class.new(api, new_glossary.id) }
35
+
36
+ let(:new_glossary) do
37
+ DeepL::Requests::Glossary::Create.new(api, 'fixture', 'EN', 'ES', [%w[Hello Hola]]).request
38
+ end
39
+
40
+ it 'returns an empty object' do
41
+ response = destroy.request
42
+ expect(response).to eq(new_glossary.id)
43
+ end
44
+ end
45
+
46
+ context 'when deleting a non existing glossary with a valid id' do
47
+ subject(:destroy) { described_class.new(api, id) }
48
+
49
+ let(:id) { '00000000-0000-0000-0000-000000000000' }
50
+
51
+ it 'raises a not found error' do
52
+ expect { destroy.request }.to raise_error(DeepL::Exceptions::NotFound)
53
+ end
54
+ end
55
+
56
+ context 'when deleting a non existing glossary with an invalid id' do
57
+ subject(:destroy) { described_class.new(api, id) }
58
+
59
+ let(:id) { 'invalid-uuid' }
60
+
61
+ it 'raises a bad request error' do
62
+ expect { destroy.request }.to raise_error(DeepL::Exceptions::BadRequest)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,62 @@
1
+ # Copyright 2022 Daniel Herzog
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
+ require 'spec_helper'
7
+
8
+ describe DeepL::Requests::Glossary::Entries do
9
+ subject(:entries_obj) { described_class.new(api, id) }
10
+
11
+ around do |tests|
12
+ tmp_env = replace_env_preserving_deepl_vars_except_mock_server
13
+ tests.call
14
+ ENV.replace(tmp_env)
15
+ end
16
+
17
+ let(:api) { build_deepl_api }
18
+ let(:id) { '9ab5dac2-b7b2-4b4a-808a-e8e305df5ecb' }
19
+
20
+ describe '#initialize' do
21
+ context 'when building a request' do
22
+ it 'creates a request object' do
23
+ expect(entries_obj).to be_a(described_class)
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '#request' do
29
+ around do |example|
30
+ VCR.use_cassette('glossaries') { example.call }
31
+ end
32
+
33
+ context 'when performing a valid request' do
34
+ it 'returns a list of entries in TSV format' do
35
+ entries = entries_obj.request
36
+ expect(entries).to be_a(Array)
37
+ expect(entries).to all(be_a(Array))
38
+ expect(entries.size).to eq(2)
39
+ end
40
+ end
41
+
42
+ context 'when requesting entries with a valid but non existing glossary id' do
43
+ subject(:entries_obj) { described_class.new(api, id) }
44
+
45
+ let(:id) { '00000000-0000-0000-0000-000000000000' }
46
+
47
+ it 'raises a not found error' do
48
+ expect { entries_obj.request }.to raise_error(DeepL::Exceptions::NotFound)
49
+ end
50
+ end
51
+
52
+ context 'when requesting entries with an invalid glossary id' do
53
+ subject(:entries_obj) { described_class.new(api, id) }
54
+
55
+ let(:id) { 'invalid-uuid' }
56
+
57
+ it 'raises a bad request error' do
58
+ expect { entries_obj.request }.to raise_error(DeepL::Exceptions::BadRequest)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,68 @@
1
+ # Copyright 2022 Daniel Herzog
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
+ require 'spec_helper'
7
+
8
+ describe DeepL::Requests::Glossary::Find do
9
+ subject(:glossary_find) { described_class.new(api, id, options) }
10
+
11
+ around do |tests|
12
+ tmp_env = replace_env_preserving_deepl_vars_except_mock_server
13
+ tests.call
14
+ ENV.replace(tmp_env)
15
+ end
16
+
17
+ let(:api) { build_deepl_api }
18
+ let(:id) { '9ab5dac2-b7b2-4b4a-808a-e8e305df5ecb' }
19
+ let(:options) { {} }
20
+
21
+ describe '#initialize' do
22
+ context 'when building a request' do
23
+ it 'creates a request object' do
24
+ expect(glossary_find).to be_a(described_class)
25
+ end
26
+ end
27
+ end
28
+
29
+ describe '#request' do
30
+ around do |example|
31
+ VCR.use_cassette('glossaries') { example.call }
32
+ end
33
+
34
+ context 'when performing a valid request' do
35
+ it 'returns a glossary object' do
36
+ glossary = glossary_find.request
37
+ expect(glossary).to be_a(DeepL::Resources::Glossary)
38
+ expect(glossary.id).to eq('9ab5dac2-b7b2-4b4a-808a-e8e305df5ecb')
39
+ expect(glossary.name).to eq('Mi Glosario')
40
+ expect(glossary.ready).to be(true).or be(false)
41
+ expect(glossary.source_lang).to eq('en')
42
+ expect(glossary.target_lang).to eq('es')
43
+ expect { Time.iso8601(glossary.creation_time) }.not_to raise_error
44
+ expect(glossary.entry_count).to eq(2)
45
+ end
46
+ end
47
+
48
+ context 'when requesting a non existing glossary with a valid id' do
49
+ subject(:glossary_find) { described_class.new(api, id, options) }
50
+
51
+ let(:id) { 'a0af40e1-d81b-4aab-a95c-7cafbcfd1eb1' }
52
+
53
+ it 'raises a not found error' do
54
+ expect { glossary_find.request }.to raise_error(DeepL::Exceptions::NotFound)
55
+ end
56
+ end
57
+
58
+ context 'when requesting a non existing glossary with an invalid id' do
59
+ subject(:glossary_find) { described_class.new(api, id, options) }
60
+
61
+ let(:id) { 'invalid-uuid' }
62
+
63
+ it 'raises a bad request error' do
64
+ expect { glossary_find.request }.to raise_error(DeepL::Exceptions::BadRequest)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,40 @@
1
+ # Copyright 2022 Daniel Herzog
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
+ require 'spec_helper'
7
+
8
+ describe DeepL::Requests::Glossary::LanguagePairs do
9
+ subject(:language_pairs_obj) { described_class.new(api, options) }
10
+
11
+ around do |tests|
12
+ tmp_env = replace_env_preserving_deepl_vars_except_mock_server
13
+ tests.call
14
+ ENV.replace(tmp_env)
15
+ end
16
+
17
+ let(:api) { build_deepl_api }
18
+ let(:options) { {} }
19
+
20
+ describe '#initialize' do
21
+ context 'when building a request' do
22
+ it 'creates a request object' do
23
+ expect(language_pairs_obj).to be_a(described_class)
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '#request' do
29
+ around do |example|
30
+ VCR.use_cassette('glossaries') { example.call }
31
+ end
32
+
33
+ context 'when requesting a list of all language pairs supported by glossaries' do
34
+ it 'returns a language pairs object' do
35
+ language_pairs = language_pairs_obj.request
36
+ expect(language_pairs).to be_an(Array)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,54 @@
1
+ # Copyright 2022 Daniel Herzog
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
+ require 'spec_helper'
7
+
8
+ describe DeepL::Requests::Glossary::List do
9
+ subject(:glossary_list) { described_class.new(api, options) }
10
+
11
+ around do |tests|
12
+ tmp_env = replace_env_preserving_deepl_vars_except_mock_server
13
+ tests.call
14
+ ENV.replace(tmp_env)
15
+ end
16
+
17
+ let(:api) { build_deepl_api }
18
+ let(:options) { {} }
19
+
20
+ describe '#initialize' do
21
+ context 'when building a request' do
22
+ it 'creates a request object' do
23
+ expect(glossary_list).to be_a(described_class)
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '#request' do
29
+ around do |example|
30
+ VCR.use_cassette('glossaries') { example.call }
31
+ end
32
+
33
+ context 'when requesting a list of all glossaries' do
34
+ it 'returns a glossaries object' do
35
+ glossaries = glossary_list.request
36
+ expect(glossaries).to be_an(Array)
37
+ end
38
+ end
39
+
40
+ context 'when performing a bad request' do
41
+ context 'when using an invalid token' do
42
+ let(:api) do
43
+ api = build_deepl_api
44
+ api.configuration.auth_key = 'invalid'
45
+ api
46
+ end
47
+
48
+ it 'raises an unauthorized error' do
49
+ expect { glossary_list.request }.to raise_error(DeepL::Exceptions::AuthorizationFailed)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end