deepl-rb 2.5.2 → 3.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.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/add_issues_to_kanban.yml +16 -0
  3. data/.gitlab-ci.yml +135 -0
  4. data/.rubocop.yml +27 -0
  5. data/CHANGELOG.md +34 -0
  6. data/CODE_OF_CONDUCT.md +132 -0
  7. data/CONTRIBUTING.md +37 -0
  8. data/Gemfile +3 -1
  9. data/LICENSE.md +1 -0
  10. data/README.md +115 -5
  11. data/Rakefile +7 -5
  12. data/SECURITY.md +58 -0
  13. data/VERSION +1 -1
  14. data/deepl-rb.gemspec +36 -20
  15. data/lib/deepl/api.rb +11 -1
  16. data/lib/deepl/configuration.rb +34 -3
  17. data/lib/deepl/document_api.rb +120 -0
  18. data/lib/deepl/exceptions/authorization_failed.rb +3 -0
  19. data/lib/deepl/exceptions/bad_request.rb +3 -0
  20. data/lib/deepl/exceptions/document_translation_error.rb +15 -0
  21. data/lib/deepl/exceptions/error.rb +6 -0
  22. data/lib/deepl/exceptions/limit_exceeded.rb +7 -0
  23. data/lib/deepl/exceptions/not_found.rb +3 -0
  24. data/lib/deepl/exceptions/not_supported.rb +3 -0
  25. data/lib/deepl/exceptions/quota_exceeded.rb +3 -0
  26. data/lib/deepl/exceptions/request_entity_too_large.rb +4 -1
  27. data/lib/deepl/exceptions/request_error.rb +4 -2
  28. data/lib/deepl/exceptions/server_error.rb +18 -0
  29. data/lib/deepl/glossary_api.rb +3 -0
  30. data/lib/deepl/requests/base.rb +89 -34
  31. data/lib/deepl/requests/document/download.rb +44 -0
  32. data/lib/deepl/requests/document/get_status.rb +44 -0
  33. data/lib/deepl/requests/document/upload.rb +64 -0
  34. data/lib/deepl/requests/glossary/create.rb +15 -1
  35. data/lib/deepl/requests/glossary/destroy.rb +8 -1
  36. data/lib/deepl/requests/glossary/entries.rb +8 -1
  37. data/lib/deepl/requests/glossary/find.rb +8 -1
  38. data/lib/deepl/requests/glossary/language_pairs.rb +9 -2
  39. data/lib/deepl/requests/glossary/list.rb +9 -2
  40. data/lib/deepl/requests/languages.rb +9 -2
  41. data/lib/deepl/requests/translate.rb +33 -11
  42. data/lib/deepl/requests/usage.rb +9 -2
  43. data/lib/deepl/resources/base.rb +3 -0
  44. data/lib/deepl/resources/document_handle.rb +57 -0
  45. data/lib/deepl/resources/document_translation_status.rb +54 -0
  46. data/lib/deepl/resources/glossary.rb +3 -0
  47. data/lib/deepl/resources/language.rb +3 -0
  48. data/lib/deepl/resources/language_pair.rb +3 -0
  49. data/lib/deepl/resources/text.rb +3 -0
  50. data/lib/deepl/resources/usage.rb +3 -0
  51. data/lib/deepl/utils/backoff_timer.rb +46 -0
  52. data/lib/deepl/utils/exception_builder.rb +18 -13
  53. data/lib/deepl.rb +47 -0
  54. data/lib/http_client_options.rb +22 -0
  55. data/license_checker.sh +8 -0
  56. data/spec/api/api_spec.rb +8 -4
  57. data/spec/api/configuration_spec.rb +92 -18
  58. data/spec/api/deepl_spec.rb +225 -86
  59. data/spec/fixtures/vcr_cassettes/deepl_document.yml +95 -0
  60. data/spec/fixtures/vcr_cassettes/deepl_document_download.yml +1214 -0
  61. data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +812 -23
  62. data/spec/fixtures/vcr_cassettes/deepl_languages.yml +28 -17
  63. data/spec/fixtures/vcr_cassettes/deepl_translate.yml +161 -53
  64. data/spec/fixtures/vcr_cassettes/deepl_usage.yml +93 -3
  65. data/spec/fixtures/vcr_cassettes/glossaries.yml +1237 -15
  66. data/spec/fixtures/vcr_cassettes/languages.yml +159 -44
  67. data/spec/fixtures/vcr_cassettes/translate_texts.yml +9742 -12
  68. data/spec/fixtures/vcr_cassettes/usage.yml +134 -2
  69. data/spec/integration_tests/document_api_spec.rb +143 -0
  70. data/spec/integration_tests/integration_test_utils.rb +170 -0
  71. data/spec/requests/glossary/create_spec.rb +23 -13
  72. data/spec/requests/glossary/destroy_spec.rb +33 -17
  73. data/spec/requests/glossary/entries_spec.rb +31 -17
  74. data/spec/requests/glossary/find_spec.rb +31 -17
  75. data/spec/requests/glossary/language_pairs_spec.rb +17 -7
  76. data/spec/requests/glossary/list_spec.rb +21 -11
  77. data/spec/requests/languages_spec.rb +31 -21
  78. data/spec/requests/translate_spec.rb +125 -131
  79. data/spec/requests/usage_spec.rb +17 -7
  80. data/spec/resources/glossary_spec.rb +15 -12
  81. data/spec/resources/language_pair_spec.rb +10 -7
  82. data/spec/resources/language_spec.rb +21 -18
  83. data/spec/resources/text_spec.rb +10 -7
  84. data/spec/resources/usage_spec.rb +16 -13
  85. data/spec/spec_helper.rb +63 -6
  86. metadata +32 -9
@@ -1,8 +1,19 @@
1
+ # Copyright 2018 Daniel Herzog
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  require 'spec_helper'
4
7
 
5
8
  describe DeepL::Requests::Translate do
9
+ subject(:translate) { described_class.new(api, text, source_lang, target_lang, 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
+
6
17
  let(:tags_str) { 'p,strong,span' }
7
18
  let(:tags_array) { %w[p strong span] }
8
19
 
@@ -12,187 +23,170 @@ describe DeepL::Requests::Translate do
12
23
  let(:target_lang) { 'ES' }
13
24
  let(:options) { {} }
14
25
 
15
- subject { DeepL::Requests::Translate.new(api, text, source_lang, target_lang, options) }
16
-
17
26
  describe '#initialize' do
18
- context 'When building a request' do
19
- it 'should create a request object' do
20
- expect(subject).to be_a(DeepL::Requests::Translate)
27
+ context 'when building a request' do
28
+ it 'creates a request object' do
29
+ expect(translate).to be_a(described_class)
21
30
  end
22
31
  end
23
32
 
24
33
  context 'when using `splitting_tags` options' do
25
- it 'should work with a nil values' do
26
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, splitting_tags: nil)
27
- expect(request.options[:splitting_tags]).to eq(nil)
28
- end
29
-
30
- it 'should work with a blank list' do
31
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, splitting_tags: '')
32
- expect(request.options[:splitting_tags]).to eq('')
34
+ it 'works with a nil values' do
35
+ request = described_class.new(api, nil, nil, nil, splitting_tags: nil)
36
+ expect(request.options[:splitting_tags]).to be_nil
33
37
  end
34
38
 
35
- it 'should work with a comma-separated list' do
36
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, splitting_tags: tags_str)
37
- expect(request.options[:splitting_tags]).to eq(tags_str)
39
+ it 'works with a blank list' do
40
+ request = described_class.new(api, nil, nil, nil, splitting_tags: '')
41
+ expect(request.options[:splitting_tags]).to eq([])
38
42
  end
39
43
 
40
- it 'should convert arrays to strings' do
41
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, splitting_tags: tags_array)
42
- expect(request.options[:splitting_tags]).to eq(tags_str)
44
+ it 'works with a comma-separated list and converts strings to an array' do
45
+ request = described_class.new(api, nil, nil, nil, splitting_tags: tags_str)
46
+ expect(request.options[:splitting_tags]).to eq(tags_array)
43
47
  end
44
48
 
45
- it 'should leave strings as they are' do
46
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, splitting_tags: tags_str)
47
- expect(request.options[:splitting_tags]).to eq(tags_str)
49
+ it 'works with an array of tags and leaves it as is' do
50
+ request = described_class.new(api, nil, nil, nil, splitting_tags: tags_array)
51
+ expect(request.options[:splitting_tags]).to eq(tags_array)
48
52
  end
49
53
  end
50
54
 
51
55
  context 'when using `non_splitting_tags` options' do
52
- it 'should work with a nil values' do
53
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: nil)
54
- expect(request.options[:non_splitting_tags]).to eq(nil)
55
- end
56
-
57
- it 'should work with a blank list' do
58
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: '')
59
- expect(request.options[:non_splitting_tags]).to eq('')
56
+ it 'works with a nil values' do
57
+ request = described_class.new(api, nil, nil, nil, non_splitting_tags: nil)
58
+ expect(request.options[:non_splitting_tags]).to be_nil
60
59
  end
61
60
 
62
- it 'should work with a comma-separated list' do
63
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: tags_str)
64
- expect(request.options[:non_splitting_tags]).to eq(tags_str)
61
+ it 'works with a blank list' do
62
+ request = described_class.new(api, nil, nil, nil, non_splitting_tags: '')
63
+ expect(request.options[:non_splitting_tags]).to eq([])
65
64
  end
66
65
 
67
- it 'should convert arrays to strings' do
68
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: tags_array)
69
- expect(request.options[:non_splitting_tags]).to eq(tags_str)
66
+ it 'works with a comma-separated list and converts strings to an array' do
67
+ request = described_class.new(api, nil, nil, nil, non_splitting_tags: tags_str)
68
+ expect(request.options[:non_splitting_tags]).to eq(tags_array)
70
69
  end
71
70
 
72
- it 'should leave strings as they are' do
73
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: tags_str)
74
- expect(request.options[:non_splitting_tags]).to eq(tags_str)
71
+ it 'works with an array and leaves it as it is' do
72
+ request = described_class.new(api, nil, nil, nil, non_splitting_tags: tags_array)
73
+ expect(request.options[:non_splitting_tags]).to eq(tags_array)
75
74
  end
76
75
  end
77
76
 
78
77
  context 'when using `ignore_tags` options' do
79
- it 'should work with a nil values' do
80
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: nil)
81
- expect(request.options[:ignore_tags]).to eq(nil)
82
- end
83
-
84
- it 'should work with a blank list' do
85
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: '')
86
- expect(request.options[:ignore_tags]).to eq('')
78
+ it 'works with a nil values' do
79
+ request = described_class.new(api, nil, nil, nil, ignore_tags: nil)
80
+ expect(request.options[:ignore_tags]).to be_nil
87
81
  end
88
82
 
89
- it 'should work with a comma-separated list' do
90
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: tags_str)
91
- expect(request.options[:ignore_tags]).to eq(tags_str)
83
+ it 'works with a blank list' do
84
+ request = described_class.new(api, nil, nil, nil, ignore_tags: '')
85
+ expect(request.options[:ignore_tags]).to eq([])
92
86
  end
93
87
 
94
- it 'should convert arrays to strings' do
95
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: tags_array)
96
- expect(request.options[:ignore_tags]).to eq(tags_str)
88
+ it 'works with a comma-separated list and converts a string to an array' do
89
+ request = described_class.new(api, nil, nil, nil, ignore_tags: tags_str)
90
+ expect(request.options[:ignore_tags]).to eq(tags_array)
97
91
  end
98
92
 
99
- it 'should leave strings as they are' do
100
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: tags_str)
101
- expect(request.options[:ignore_tags]).to eq(tags_str)
93
+ it 'works with an array and leaves it as it is' do
94
+ request = described_class.new(api, nil, nil, nil, ignore_tags: tags_array)
95
+ expect(request.options[:ignore_tags]).to eq(tags_array)
102
96
  end
103
97
  end
104
98
 
105
99
  context 'when using `split_sentences` options' do
106
- it 'should convert `true` to `1`' do
107
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: true)
100
+ it 'converts `true` to `1`' do
101
+ request = described_class.new(api, nil, nil, nil, split_sentences: true)
108
102
  expect(request.options[:split_sentences]).to eq('1')
109
103
  end
110
104
 
111
- it 'should convert `false` to `0`' do
112
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: false)
105
+ it 'converts `false` to `0`' do
106
+ request = described_class.new(api, nil, nil, nil, split_sentences: false)
113
107
  expect(request.options[:split_sentences]).to eq('0')
114
108
  end
115
109
 
116
- it 'should leave `0` as is' do
117
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: '0')
110
+ it 'leaves `0` as is' do
111
+ request = described_class.new(api, nil, nil, nil, split_sentences: '0')
118
112
  expect(request.options[:split_sentences]).to eq('0')
119
113
  end
120
114
 
121
- it 'should leave `nonewlines` as is' do
122
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: 'nonewlines')
115
+ it 'leaves `nonewlines` as is' do
116
+ request = described_class.new(api, nil, nil, nil, split_sentences: 'nonewlines')
123
117
  expect(request.options[:split_sentences]).to eq('nonewlines')
124
118
  end
125
119
 
126
- it 'should leave `1` as is' do
127
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: '1')
120
+ it 'leaves `1` as is' do
121
+ request = described_class.new(api, nil, nil, nil, split_sentences: '1')
128
122
  expect(request.options[:split_sentences]).to eq('1')
129
123
  end
130
124
  end
131
125
 
132
126
  context 'when using `preserve_formatting` options' do
133
- it 'should convert `true` to `1`' do
134
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: true)
135
- expect(request.options[:preserve_formatting]).to eq('1')
127
+ it 'leaves `true` as is' do
128
+ request = described_class.new(api, nil, nil, nil, preserve_formatting: true)
129
+ expect(request.options[:preserve_formatting]).to be(true)
136
130
  end
137
131
 
138
- it 'should convert `false` to `0`' do
139
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: false)
140
- expect(request.options[:preserve_formatting]).to eq('0')
132
+ it 'leaves `false` as is' do
133
+ request = described_class.new(api, nil, nil, nil, preserve_formatting: false)
134
+ expect(request.options[:preserve_formatting]).to be(false)
141
135
  end
142
136
 
143
- it 'should leave `0` as is' do
144
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: '0')
145
- expect(request.options[:preserve_formatting]).to eq('0')
137
+ it 'converts `0` to `false`' do
138
+ request = described_class.new(api, nil, nil, nil, preserve_formatting: '0')
139
+ expect(request.options[:preserve_formatting]).to be(false)
146
140
  end
147
141
 
148
- it 'should leave `1` as is' do
149
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: '1')
150
- expect(request.options[:preserve_formatting]).to eq('1')
142
+ it 'converts `1` to `true`' do
143
+ request = described_class.new(api, nil, nil, nil, preserve_formatting: '1')
144
+ expect(request.options[:preserve_formatting]).to be(true)
151
145
  end
152
146
  end
153
147
 
154
148
  context 'when using `outline_detection` options' do
155
- it 'should convert `true` to `1`' do
156
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: true)
157
- expect(request.options[:outline_detection]).to eq('1')
149
+ it 'leaves `true` as is' do
150
+ request = described_class.new(api, nil, nil, nil, outline_detection: true)
151
+ expect(request.options[:outline_detection]).to be(true)
158
152
  end
159
153
 
160
- it 'should convert `false` to `0`' do
161
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: false)
162
- expect(request.options[:outline_detection]).to eq('0')
154
+ it 'leaves `false` as is' do
155
+ request = described_class.new(api, nil, nil, nil, outline_detection: false)
156
+ expect(request.options[:outline_detection]).to be(false)
163
157
  end
164
158
 
165
- it 'should leave `0` as is' do
166
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: '0')
167
- expect(request.options[:outline_detection]).to eq('0')
159
+ it 'converts `0` to `false`' do
160
+ request = described_class.new(api, nil, nil, nil, outline_detection: '0')
161
+ expect(request.options[:outline_detection]).to be(false)
168
162
  end
169
163
 
170
- it 'should leave `1` as is' do
171
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: '1')
172
- expect(request.options[:outline_detection]).to eq('1')
164
+ it 'converts `1` to `true`' do
165
+ request = described_class.new(api, nil, nil, nil, outline_detection: '1')
166
+ expect(request.options[:outline_detection]).to be(true)
173
167
  end
174
168
  end
175
169
 
176
170
  context 'when using `glossary_id` options' do
177
- it 'should work with a nil values' do
178
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, glossary_id: nil)
179
- expect(request.options[:glossary_id]).to eq(nil)
171
+ it 'works with a nil values' do
172
+ request = described_class.new(api, nil, nil, nil, glossary_id: nil)
173
+ expect(request.options[:glossary_id]).to be_nil
180
174
  end
181
175
 
182
- it 'should work with a string' do
183
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, glossary_id: 'sample_id')
176
+ it 'works with a string' do
177
+ request = described_class.new(api, nil, nil, nil, glossary_id: 'sample_id')
184
178
  expect(request.options[:glossary_id]).to eq('sample_id')
185
179
  end
186
180
  end
187
181
 
188
182
  context 'when using `formality` options' do
189
- it 'should work with a nil values' do
190
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, formality: nil)
191
- expect(request.options[:formality]).to eq(nil)
183
+ it 'works with a nil values' do
184
+ request = described_class.new(api, nil, nil, nil, formality: nil)
185
+ expect(request.options[:formality]).to be_nil
192
186
  end
193
187
 
194
- it 'should work with a string' do
195
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, formality: 'more')
188
+ it 'works with a string' do
189
+ request = described_class.new(api, nil, nil, nil, formality: 'more')
196
190
  expect(request.options[:formality]).to eq('more')
197
191
  end
198
192
  end
@@ -203,9 +197,9 @@ describe DeepL::Requests::Translate do
203
197
  VCR.use_cassette('translate_texts') { example.call }
204
198
  end
205
199
 
206
- context 'When performing a valid request with one text' do
207
- it 'should return a text object' do
208
- text = subject.request
200
+ context 'when performing a valid request with one text' do
201
+ it 'returns a text object' do
202
+ text = translate.request
209
203
 
210
204
  expect(text).to be_a(DeepL::Resources::Text)
211
205
  expect(text.text).to eq('Texto de muestra')
@@ -213,11 +207,11 @@ describe DeepL::Requests::Translate do
213
207
  end
214
208
  end
215
209
 
216
- context 'When performing a valid request with multiple texts' do
210
+ context 'when performing a valid request with multiple texts' do
217
211
  let(:text) { %w[Sample Word] }
218
212
 
219
- it 'should return a text object' do
220
- texts = subject.request
213
+ it 'returns a text object' do
214
+ texts = translate.request
221
215
 
222
216
  expect(texts).to be_a(Array)
223
217
  expect(texts.first.text).to eq('Muestra')
@@ -228,12 +222,12 @@ describe DeepL::Requests::Translate do
228
222
  end
229
223
  end
230
224
 
231
- context 'When performing a valid request with tag handling' do
225
+ context 'when performing a valid request with tag handling' do
232
226
  let(:text) { '<p>Sample text</p>' }
233
227
  let(:options) { { tag_handling: 'xml' } }
234
228
 
235
- it 'should return a text object' do
236
- text = subject.request
229
+ it 'returns a text object' do
230
+ text = translate.request
237
231
 
238
232
  expect(text).to be_a(DeepL::Resources::Text)
239
233
  expect(text.text).to eq('<p>Texto de muestra</p>')
@@ -241,12 +235,12 @@ describe DeepL::Requests::Translate do
241
235
  end
242
236
  end
243
237
 
244
- context 'When performing a valid request and passing a variable' do
238
+ context 'when performing a valid request and passing a variable' do
245
239
  let(:text) { 'Welcome and <code>Hello great World</code> Good Morning!' }
246
240
  let(:options) { { tag_handling: 'xml', ignore_tags: %w[code span] } }
247
241
 
248
- it 'should return a text object' do
249
- text = subject.request
242
+ it 'returns a text object' do
243
+ text = translate.request
250
244
 
251
245
  expect(text).to be_a(DeepL::Resources::Text)
252
246
  expect(text.text).to eq('Bienvenido y <code>Hello great World</code> ¡Buenos días!')
@@ -254,7 +248,7 @@ describe DeepL::Requests::Translate do
254
248
  end
255
249
  end
256
250
 
257
- context 'When performing a valid request with an HTML document' do
251
+ context 'when performing a valid request with an HTML document' do
258
252
  let(:text) do
259
253
  <<~XML
260
254
  <document>
@@ -277,15 +271,15 @@ describe DeepL::Requests::Translate do
277
271
  }
278
272
  end
279
273
 
280
- it 'should return a text object' do
281
- text = subject.request
274
+ it 'returns a text object' do
275
+ text = translate.request
282
276
 
283
277
  expect(text).to be_a(DeepL::Resources::Text)
284
278
  expect(text.text).to eq(
285
279
  <<~XML
286
280
  <document>
287
281
  <meta>
288
- <title>El título de un documento</title>
282
+ <title>Título de un documento</title>
289
283
  </meta>
290
284
  <content>
291
285
  <par>Es la primera frase. Seguido de una segunda.</par>
@@ -298,44 +292,44 @@ describe DeepL::Requests::Translate do
298
292
  end
299
293
  end
300
294
 
301
- context 'When performing a bad request' do
302
- context 'When using an invalid token' do
295
+ context 'when performing a bad request' do
296
+ context 'when using an invalid token' do
303
297
  let(:api) do
304
298
  api = build_deepl_api
305
299
  api.configuration.auth_key = 'invalid'
306
300
  api
307
301
  end
308
302
 
309
- it 'should raise an unauthorized error' do
310
- expect { subject.request }.to raise_error(DeepL::Exceptions::AuthorizationFailed)
303
+ it 'raises an unauthorized error' do
304
+ expect { translate.request }.to raise_error(DeepL::Exceptions::AuthorizationFailed)
311
305
  end
312
306
  end
313
307
 
314
- context 'When using an invalid text' do
308
+ context 'when using an invalid text' do
315
309
  let(:text) { nil }
316
310
 
317
- it 'should raise a bad request error' do
318
- message = "Parameter 'text' not specified."
319
- expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
311
+ it 'raises a bad request error' do
312
+ message = "Invalid request: Expected 'text' parameter to be an array of strings"
313
+ expect { translate.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
320
314
  end
321
315
  end
322
316
 
323
- context 'When using an invalid target language' do
317
+ context 'when using an invalid target language' do
324
318
  let(:target_lang) { nil }
325
319
 
326
- it 'should raise a bad request error' do
320
+ it 'raises a bad request error' do
327
321
  message = "Value for 'target_lang' not supported."
328
- expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
322
+ expect { translate.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
329
323
  end
330
324
  end
331
325
  end
332
326
 
333
- context 'When performing a request with too many texts' do
327
+ context 'when performing a request with too many texts' do
334
328
  let(:text) { Array.new(10_000) { |i| "This is the sentence number #{i}" } }
335
329
 
336
- it 'should raise a request entity too large error' do
337
- expect { subject.request }.to raise_error(DeepL::Exceptions::RequestEntityTooLarge,
338
- /request size has reached the supported limit/)
330
+ it 'raises a request entity too large error' do
331
+ expect { translate.request }.to raise_error(DeepL::Exceptions::RequestEntityTooLarge,
332
+ /request size has reached the supported limit/)
339
333
  end
340
334
  end
341
335
  end
@@ -1,16 +1,26 @@
1
+ # Copyright 2018 Daniel Herzog
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  require 'spec_helper'
4
7
 
5
8
  describe DeepL::Requests::Usage do
9
+ subject(:usage_req) { 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
+
6
17
  let(:api) { build_deepl_api }
7
18
  let(:options) { {} }
8
- subject { DeepL::Requests::Usage.new(api, options) }
9
19
 
10
20
  describe '#initialize' do
11
- context 'When building a request' do
12
- it 'should create a request object' do
13
- expect(subject).to be_a(DeepL::Requests::Usage)
21
+ context 'when building a request' do
22
+ it 'creates a request object' do
23
+ expect(usage_req).to be_a(described_class)
14
24
  end
15
25
  end
16
26
  end
@@ -20,9 +30,9 @@ describe DeepL::Requests::Usage do
20
30
  VCR.use_cassette('usage') { example.call }
21
31
  end
22
32
 
23
- context 'When performing a valid request' do
24
- it 'should return an usage object' do
25
- usage = subject.request
33
+ context 'when performing a valid request' do
34
+ it 'returns an usage object' do
35
+ usage = usage_req.request
26
36
 
27
37
  expect(usage).to be_a(DeepL::Resources::Usage)
28
38
  expect(usage.character_count).to be_a(Numeric)
@@ -1,9 +1,12 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  require 'spec_helper'
4
7
 
5
8
  describe DeepL::Resources::Glossary do
6
- subject do
9
+ subject(:glossary) do
7
10
  described_class.new({
8
11
  'glossary_id' => 'def3a26b-3e84-45b3-84ae-0c0aaf3525f7',
9
12
  'name' => 'Mein Glossar',
@@ -16,19 +19,19 @@ describe DeepL::Resources::Glossary do
16
19
  end
17
20
 
18
21
  describe '#initialize' do
19
- context 'When building a basic object' do
20
- it 'should create a resource' do
21
- expect(subject).to be_a(described_class)
22
+ context 'when building a basic object' do
23
+ it 'creates a resource' do
24
+ expect(glossary).to be_a(described_class)
22
25
  end
23
26
 
24
- it 'should assign the attributes' do
25
- expect(subject.id).to eq('def3a26b-3e84-45b3-84ae-0c0aaf3525f7')
26
- expect(subject.name).to eq('Mein Glossar')
27
- expect(subject.ready).to eq(true)
28
- expect(subject.source_lang).to eq('EN')
29
- expect(subject.target_lang).to eq('DE')
30
- expect(subject.creation_time).to eq('2021-08-03T14:16:18.329Z')
31
- expect(subject.entry_count).to eq(1)
27
+ it 'assigns the attributes' do
28
+ expect(glossary.id).to eq('def3a26b-3e84-45b3-84ae-0c0aaf3525f7')
29
+ expect(glossary.name).to eq('Mein Glossar')
30
+ expect(glossary.ready).to be(true)
31
+ expect(glossary.source_lang).to eq('EN')
32
+ expect(glossary.target_lang).to eq('DE')
33
+ expect(glossary.creation_time).to eq('2021-08-03T14:16:18.329Z')
34
+ expect(glossary.entry_count).to eq(1)
32
35
  end
33
36
  end
34
37
  end
@@ -1,19 +1,22 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  require 'spec_helper'
4
7
 
5
8
  describe DeepL::Resources::LanguagePair do
6
- subject { described_class.new('en', 'de', nil, nil) }
9
+ subject(:language_pair) { described_class.new('en', 'de', nil, nil) }
7
10
 
8
11
  describe '#initialize' do
9
- context 'When building a basic object' do
10
- it 'should create a resource' do
11
- expect(subject).to be_a(described_class)
12
+ context 'when building a basic object' do
13
+ it 'creates a resource' do
14
+ expect(language_pair).to be_a(described_class)
12
15
  end
13
16
 
14
- it 'should assign the attributes' do
15
- expect(subject.source_lang).to eq('en')
16
- expect(subject.target_lang).to eq('de')
17
+ it 'assigns the attributes' do
18
+ expect(language_pair.source_lang).to eq('en')
19
+ expect(language_pair.target_lang).to eq('de')
17
20
  end
18
21
  end
19
22
  end
@@ -1,41 +1,44 @@
1
+ # Copyright 2021 Daniel Herzog
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  require 'spec_helper'
4
7
 
5
8
  describe DeepL::Resources::Language do
6
- subject { described_class.new('EN', 'English', nil, nil, nil) }
9
+ subject(:language) { described_class.new('EN', 'English', nil, nil, nil) }
7
10
 
8
11
  describe '#initialize' do
9
- context 'When building a basic object' do
10
- it 'should create a resource' do
11
- expect(subject).to be_a(described_class)
12
+ context 'when building a basic object' do
13
+ it 'creates a resource' do
14
+ expect(language).to be_a(described_class)
12
15
  end
13
16
 
14
- it 'should assign the attributes' do
15
- expect(subject.code).to eq('EN')
16
- expect(subject.name).to eq('English')
17
+ it 'assigns the attributes' do
18
+ expect(language.code).to eq('EN')
19
+ expect(language.name).to eq('English')
17
20
  end
18
21
 
19
- it 'should not define the supports formality method' do
20
- expect { subject.supports_formality? }.to raise_error(DeepL::Exceptions::NotSupported)
22
+ it 'does not define the supports formality method' do
23
+ expect { language.supports_formality? }.to raise_error(DeepL::Exceptions::NotSupported)
21
24
  end
22
25
  end
23
26
 
24
27
  context 'when building a target language object' do
25
- subject { described_class.new('EN', 'English', true, nil, nil) }
28
+ subject(:language) { described_class.new('EN', 'English', true, nil, nil) }
26
29
 
27
- it 'should create a resource' do
28
- expect(subject).to be_a(described_class)
30
+ it 'creates a resource' do
31
+ expect(language).to be_a(described_class)
29
32
  end
30
33
 
31
- it 'should assign the attributes' do
32
- expect(subject.code).to eq('EN')
33
- expect(subject.name).to eq('English')
34
+ it 'assigns the attributes' do
35
+ expect(language.code).to eq('EN')
36
+ expect(language.name).to eq('English')
34
37
  end
35
38
 
36
- it 'should include the supports formality method' do
37
- expect { subject.supports_formality? }.not_to raise_error
38
- expect(subject.supports_formality?).to be_truthy
39
+ it 'includes the supports formality method' do
40
+ expect { language.supports_formality? }.not_to raise_error
41
+ expect(language).to be_supports_formality
39
42
  end
40
43
  end
41
44
  end
@@ -1,19 +1,22 @@
1
+ # Copyright 2018 Daniel Herzog
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  require 'spec_helper'
4
7
 
5
8
  describe DeepL::Resources::Text do
6
- subject { described_class.new('Target', 'es', nil, nil) }
9
+ subject(:text) { described_class.new('Target', 'es', nil, nil) }
7
10
 
8
11
  describe '#initialize' do
9
- context 'When building a basic object' do
10
- it 'should create a resource' do
11
- expect(subject).to be_a(described_class)
12
+ context 'when building a basic object' do
13
+ it 'creates a resource' do
14
+ expect(text).to be_a(described_class)
12
15
  end
13
16
 
14
- it 'should assign the attributes' do
15
- expect(subject.text).to eq('Target')
16
- expect(subject.detected_source_language).to eq('es')
17
+ it 'assigns the attributes' do
18
+ expect(text.text).to eq('Target')
19
+ expect(text.detected_source_language).to eq('es')
17
20
  end
18
21
  end
19
22
  end