deepl-rb 2.5.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
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,17 +1,27 @@
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::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
+
6
17
  let(:api) { build_deepl_api }
7
- let(:id) { 'd9ad833f-c818-430c-a3c9-47071384fa3e' }
18
+ let(:id) { '9ab5dac2-b7b2-4b4a-808a-e8e305df5ecb' }
8
19
  let(:options) { {} }
9
- subject { DeepL::Requests::Glossary::Find.new(api, id, options) }
10
20
 
11
21
  describe '#initialize' do
12
- context 'When building a request' do
13
- it 'should create a request object' do
14
- expect(subject).to be_a(described_class)
22
+ context 'when building a request' do
23
+ it 'creates a request object' do
24
+ expect(glossary_find).to be_a(described_class)
15
25
  end
16
26
  end
17
27
  end
@@ -21,11 +31,11 @@ describe DeepL::Requests::Glossary::Find do
21
31
  VCR.use_cassette('glossaries') { example.call }
22
32
  end
23
33
 
24
- context 'When performing a valid request' do
25
- it 'should return a glossary object' do
26
- glossary = subject.request
34
+ context 'when performing a valid request' do
35
+ it 'returns a glossary object' do
36
+ glossary = glossary_find.request
27
37
  expect(glossary).to be_a(DeepL::Resources::Glossary)
28
- expect(glossary.id).to eq('d9ad833f-c818-430c-a3c9-47071384fa3e')
38
+ expect(glossary.id).to eq('9ab5dac2-b7b2-4b4a-808a-e8e305df5ecb')
29
39
  expect(glossary.name).to eq('Mi Glosario')
30
40
  expect(glossary.ready).to be(true).or be(false)
31
41
  expect(glossary.source_lang).to eq('en')
@@ -35,19 +45,23 @@ describe DeepL::Requests::Glossary::Find do
35
45
  end
36
46
  end
37
47
 
38
- context 'When requesting a non existing glossary with a valid id' do
48
+ context 'when requesting a non existing glossary with a valid id' do
49
+ subject(:glossary_find) { described_class.new(api, id, options) }
50
+
39
51
  let(:id) { 'a0af40e1-d81b-4aab-a95c-7cafbcfd1eb1' }
40
- subject { DeepL::Requests::Glossary::Find.new(api, id, options) }
41
- it 'should raise a not found error' do
42
- expect { subject.request }.to raise_error(DeepL::Exceptions::NotFound)
52
+
53
+ it 'raises a not found error' do
54
+ expect { glossary_find.request }.to raise_error(DeepL::Exceptions::NotFound)
43
55
  end
44
56
  end
45
57
 
46
- context 'When requesting a non existing glossary with an invalid id' do
58
+ context 'when requesting a non existing glossary with an invalid id' do
59
+ subject(:glossary_find) { described_class.new(api, id, options) }
60
+
47
61
  let(:id) { 'invalid-uuid' }
48
- subject { DeepL::Requests::Glossary::Find.new(api, id, options) }
49
- it 'should raise a bad request error' do
50
- expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest)
62
+
63
+ it 'raises a bad request error' do
64
+ expect { glossary_find.request }.to raise_error(DeepL::Exceptions::BadRequest)
51
65
  end
52
66
  end
53
67
  end
@@ -1,16 +1,26 @@
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::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
+
6
17
  let(:api) { build_deepl_api }
7
18
  let(:options) { {} }
8
- subject { DeepL::Requests::Glossary::LanguagePairs.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(described_class)
21
+ context 'when building a request' do
22
+ it 'creates a request object' do
23
+ expect(language_pairs_obj).to be_a(described_class)
14
24
  end
15
25
  end
16
26
  end
@@ -20,9 +30,9 @@ describe DeepL::Requests::Glossary::LanguagePairs do
20
30
  VCR.use_cassette('glossaries') { example.call }
21
31
  end
22
32
 
23
- context 'When requesting a list of all language pairs supported by glossaries' do
24
- it 'should return a language pairs object' do
25
- language_pairs = subject.request
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
26
36
  expect(language_pairs).to be_an(Array)
27
37
  end
28
38
  end
@@ -1,16 +1,26 @@
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::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
+
6
17
  let(:api) { build_deepl_api }
7
18
  let(:options) { {} }
8
- subject { DeepL::Requests::Glossary::List.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(described_class)
21
+ context 'when building a request' do
22
+ it 'creates a request object' do
23
+ expect(glossary_list).to be_a(described_class)
14
24
  end
15
25
  end
16
26
  end
@@ -20,23 +30,23 @@ describe DeepL::Requests::Glossary::List do
20
30
  VCR.use_cassette('glossaries') { example.call }
21
31
  end
22
32
 
23
- context 'When requesting a list of all glossaries' do
24
- it 'should return a glossaries object' do
25
- glossaries = subject.request
33
+ context 'when requesting a list of all glossaries' do
34
+ it 'returns a glossaries object' do
35
+ glossaries = glossary_list.request
26
36
  expect(glossaries).to be_an(Array)
27
37
  end
28
38
  end
29
39
 
30
- context 'When performing a bad request' do
31
- context 'When using an invalid token' do
40
+ context 'when performing a bad request' do
41
+ context 'when using an invalid token' do
32
42
  let(:api) do
33
43
  api = build_deepl_api
34
44
  api.configuration.auth_key = 'invalid'
35
45
  api
36
46
  end
37
47
 
38
- it 'should raise an unauthorized error' do
39
- expect { subject.request }.to raise_error(DeepL::Exceptions::AuthorizationFailed)
48
+ it 'raises an unauthorized error' do
49
+ expect { glossary_list.request }.to raise_error(DeepL::Exceptions::AuthorizationFailed)
40
50
  end
41
51
  end
42
52
  end
@@ -1,16 +1,26 @@
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::Requests::Languages do
9
+ subject(:languages_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
+
6
17
  let(:api) { build_deepl_api }
7
18
  let(:options) { {} }
8
- subject { DeepL::Requests::Languages.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(described_class)
21
+ context 'when building a request' do
22
+ it 'creates a request object' do
23
+ expect(languages_obj).to be_a(described_class)
14
24
  end
15
25
  end
16
26
  end
@@ -20,38 +30,38 @@ describe DeepL::Requests::Languages do
20
30
  VCR.use_cassette('languages') { example.call }
21
31
  end
22
32
 
23
- context 'When requesting source languages' do
24
- it 'should return an usage object' do
25
- languages = subject.request
33
+ context 'when requesting source languages' do
34
+ it 'returns a valid list of source languages' do
35
+ languages = languages_obj.request
26
36
 
27
37
  expect(languages).to be_an(Array)
28
- expect(languages.size).to eq(24)
29
- expect(languages.any? { |l| l.code == 'EN' && l.name == 'English' }).to be_truthy
30
- expect(languages.any? { |l| l.code == 'ES' && l.name == 'Spanish' }).to be_truthy
38
+ expect(languages.size).to eq(29)
39
+ expect(languages).to(be_any { |l| l.code == 'EN' && l.name == 'English' })
40
+ expect(languages).to(be_any { |l| l.code == 'ES' && l.name == 'Spanish' })
31
41
  end
32
42
  end
33
43
 
34
- context 'When requesting target languages' do
44
+ context 'when requesting target languages' do
35
45
  let(:options) { { type: :target } }
36
46
 
37
- it 'should return an usage object' do
38
- languages = subject.request
47
+ it 'returns a valid list of target languages' do
48
+ languages = languages_obj.request
39
49
 
40
50
  expect(languages).to be_an(Array)
41
- expect(languages.size).to eq(26)
42
- expect(languages.any? { |l| l.code == 'EN' && l.name == 'English' }).to be_falsey
43
- expect(languages.any? { |l| l.code == 'ES' && l.name == 'Spanish' }).to be_truthy
44
- expect(languages.any? { |l| l.code == 'EN-US' && l.name == 'English (American)' })
45
- .to be_truthy
51
+ expect(languages.size).to eq(31)
52
+ expect(languages).not_to(be_any { |l| l.code == 'EN' && l.name == 'English' })
53
+ expect(languages).to(be_any { |l| l.code == 'ES' && l.name == 'Spanish' })
54
+ expect(languages)
55
+ .to(be_any { |l| l.code == 'EN-US' && l.name == 'English (American)' })
46
56
  end
47
57
  end
48
58
 
49
- context 'When using an invalid type' do
59
+ context 'when using an invalid type' do
50
60
  let(:options) { { type: :invalid } }
51
61
 
52
- it 'should return an usage object' do
62
+ it 'returns an usage object' do
53
63
  message = "Parameter 'type' is invalid. 'source' and 'target' are valid values."
54
- expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
64
+ expect { languages_obj.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
55
65
  end
56
66
  end
57
67
  end