deepl-rb 2.2.1 → 2.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.circleci/config.yml +1 -13
- data/.rubocop.yml +8 -4
- data/Gemfile +6 -0
- data/README.md +133 -25
- data/Rakefile +2 -0
- data/VERSION +1 -1
- data/deepl-rb.gemspec +38 -10
- data/lib/deepl/api.rb +2 -0
- data/lib/deepl/configuration.rb +4 -2
- data/lib/deepl/exceptions/authorization_failed.rb +2 -0
- data/lib/deepl/exceptions/bad_request.rb +4 -0
- data/lib/deepl/exceptions/error.rb +2 -0
- data/lib/deepl/exceptions/limit_exceeded.rb +2 -0
- data/lib/deepl/exceptions/not_found.rb +13 -0
- data/lib/deepl/exceptions/not_supported.rb +11 -0
- data/lib/deepl/exceptions/quota_exceeded.rb +2 -0
- data/lib/deepl/exceptions/request_entity_too_large.rb +12 -0
- data/lib/deepl/exceptions/request_error.rb +4 -1
- data/lib/deepl/glossary_api.rb +35 -0
- data/lib/deepl/requests/base.rb +23 -11
- data/lib/deepl/requests/glossary/create.rb +45 -0
- data/lib/deepl/requests/glossary/destroy.rb +30 -0
- data/lib/deepl/requests/glossary/entries.rb +30 -0
- data/lib/deepl/requests/glossary/find.rb +31 -0
- data/lib/deepl/requests/glossary/language_pairs.rb +31 -0
- data/lib/deepl/requests/glossary/list.rb +30 -0
- data/lib/deepl/requests/languages.rb +30 -0
- data/lib/deepl/requests/translate.rb +8 -2
- data/lib/deepl/requests/usage.rb +2 -0
- data/lib/deepl/resources/base.rb +2 -0
- data/lib/deepl/resources/glossary.rb +25 -0
- data/lib/deepl/resources/language.rb +27 -0
- data/lib/deepl/resources/language_pair.rb +20 -0
- data/lib/deepl/resources/text.rb +2 -0
- data/lib/deepl/resources/usage.rb +2 -0
- data/lib/deepl/utils/exception_builder.rb +29 -0
- data/lib/deepl.rb +28 -0
- data/spec/api/api_spec.rb +2 -0
- data/spec/api/configuration_spec.rb +4 -2
- data/spec/api/deepl_spec.rb +213 -0
- data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +374 -0
- data/spec/fixtures/vcr_cassettes/deepl_languages.yml +43 -0
- data/spec/fixtures/vcr_cassettes/deepl_translate.yml +215 -5
- data/spec/fixtures/vcr_cassettes/deepl_usage.yml +12 -7
- data/spec/fixtures/vcr_cassettes/glossaries.yml +480 -0
- data/spec/fixtures/vcr_cassettes/languages.yml +114 -0
- data/spec/fixtures/vcr_cassettes/translate_texts.yml +199 -33
- data/spec/fixtures/vcr_cassettes/usage.yml +12 -7
- data/spec/requests/glossary/create_spec.rb +55 -0
- data/spec/requests/glossary/destroy_spec.rb +50 -0
- data/spec/requests/glossary/entries_spec.rb +48 -0
- data/spec/requests/glossary/find_spec.rb +54 -0
- data/spec/requests/glossary/language_pairs_spec.rb +30 -0
- data/spec/requests/glossary/list_spec.rb +44 -0
- data/spec/requests/languages_spec.rb +58 -0
- data/spec/requests/translate_spec.rb +139 -4
- data/spec/requests/usage_spec.rb +2 -0
- data/spec/resources/glossary_spec.rb +35 -0
- data/spec/resources/language_pair_spec.rb +20 -0
- data/spec/resources/language_spec.rb +42 -0
- data/spec/resources/text_spec.rb +4 -2
- data/spec/resources/usage_spec.rb +5 -3
- data/spec/spec_helper.rb +6 -4
- metadata +49 -8
- data/Gemfile.lock +0 -113
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
http_interactions:
|
|
3
3
|
- request:
|
|
4
4
|
method: get
|
|
5
|
-
uri: https://api.deepl.com/v2/usage
|
|
5
|
+
uri: https://api-free.deepl.com/v2/usage
|
|
6
6
|
body:
|
|
7
7
|
encoding: US-ASCII
|
|
8
8
|
string: ''
|
|
9
9
|
headers:
|
|
10
|
+
Authorization:
|
|
11
|
+
- DeepL-Auth-Key VALID_TOKEN
|
|
10
12
|
Accept-Encoding:
|
|
11
13
|
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
|
12
14
|
Accept:
|
|
@@ -21,14 +23,17 @@ http_interactions:
|
|
|
21
23
|
Server:
|
|
22
24
|
- nginx
|
|
23
25
|
Date:
|
|
24
|
-
-
|
|
26
|
+
- Mon, 17 May 2021 14:18:32 GMT
|
|
27
|
+
Content-Type:
|
|
28
|
+
- application/json
|
|
25
29
|
Content-Length:
|
|
26
|
-
- '
|
|
30
|
+
- '48'
|
|
27
31
|
Connection:
|
|
28
32
|
- keep-alive
|
|
33
|
+
Access-Control-Allow-Origin:
|
|
34
|
+
- "*"
|
|
29
35
|
body:
|
|
30
36
|
encoding: UTF-8
|
|
31
|
-
string: '{"character_count":
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
recorded_with: VCR 4.0.0
|
|
37
|
+
string: '{"character_count":353,"character_limit":500000}'
|
|
38
|
+
recorded_at: Mon, 17 May 2021 14:18:32 GMT
|
|
39
|
+
recorded_with: VCR 6.0.0
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe DeepL::Requests::Glossary::Create do
|
|
6
|
+
let(:api) { build_deepl_api }
|
|
7
|
+
let(:name) { 'Mi Glosario' }
|
|
8
|
+
let(:source_lang) { 'EN' }
|
|
9
|
+
let(:target_lang) { 'ES' }
|
|
10
|
+
let(:entries) do
|
|
11
|
+
[
|
|
12
|
+
%w[Hello Hola],
|
|
13
|
+
%w[World Mundo]
|
|
14
|
+
]
|
|
15
|
+
end
|
|
16
|
+
let(:entries_format) { 'tsv' }
|
|
17
|
+
let(:options) { {} }
|
|
18
|
+
subject do
|
|
19
|
+
DeepL::Requests::Glossary::Create.new(api, name, source_lang, target_lang, entries, options)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe '#initialize' do
|
|
23
|
+
context 'When building a request' do
|
|
24
|
+
it 'should create a request object' do
|
|
25
|
+
expect(subject).to be_a(described_class)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'should set the default value for the entries format if not specified' do
|
|
29
|
+
request = DeepL::Requests::Glossary::Create.new(api, name, source_lang, target_lang,
|
|
30
|
+
entries, options)
|
|
31
|
+
expect(request.entries_format).to eq('tsv')
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
describe '#request' do
|
|
37
|
+
around do |example|
|
|
38
|
+
VCR.use_cassette('glossaries') { example.call }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
context 'When performing a valid request with two glossary entries' do
|
|
42
|
+
it 'should return a glossaries object' do
|
|
43
|
+
glossary = subject.request
|
|
44
|
+
expect(glossary).to be_a(DeepL::Resources::Glossary)
|
|
45
|
+
expect(glossary.id).to be_kind_of(String)
|
|
46
|
+
expect(glossary.name).to eq('Mi Glosario')
|
|
47
|
+
expect(glossary.ready).to be(true).or be(false)
|
|
48
|
+
expect(glossary.source_lang).to eq('en')
|
|
49
|
+
expect(glossary.target_lang).to eq('es')
|
|
50
|
+
expect { Time.iso8601(glossary.creation_time) }.not_to raise_error
|
|
51
|
+
expect(glossary.entry_count).to eq(2)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe DeepL::Requests::Glossary::Destroy do
|
|
6
|
+
let(:api) { build_deepl_api }
|
|
7
|
+
let(:id) { '367eef44-b533-4d95-be19-74950c7760e9' }
|
|
8
|
+
subject { DeepL::Requests::Glossary::Destroy.new(api, id) }
|
|
9
|
+
|
|
10
|
+
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)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
describe '#request' do
|
|
19
|
+
around do |example|
|
|
20
|
+
VCR.use_cassette('glossaries') { example.call }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
context 'When performing a valid request' do
|
|
24
|
+
let(:new_glossary) do
|
|
25
|
+
DeepL::Requests::Glossary::Create.new(api, 'fixture', 'EN', 'ES', [%w[Hello Hola]]).request
|
|
26
|
+
end
|
|
27
|
+
subject { DeepL::Requests::Glossary::Destroy.new(api, new_glossary.id) }
|
|
28
|
+
it 'should return an empty object' do
|
|
29
|
+
response = subject.request
|
|
30
|
+
expect(response).to eq(new_glossary.id)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
context 'When deleting a non existing glossary with a valid id' do
|
|
35
|
+
let(:id) { '00000000-0000-0000-0000-000000000000' }
|
|
36
|
+
subject { DeepL::Requests::Glossary::Destroy.new(api, id) }
|
|
37
|
+
it 'should raise a not found error' do
|
|
38
|
+
expect { subject.request }.to raise_error(DeepL::Exceptions::NotFound)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
context 'When deleting a non existing glossary with an invalid id' do
|
|
43
|
+
let(:id) { 'invalid-uuid' }
|
|
44
|
+
subject { DeepL::Requests::Glossary::Destroy.new(api, id) }
|
|
45
|
+
it 'should raise a bad request error' do
|
|
46
|
+
expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe DeepL::Requests::Glossary::Entries do
|
|
6
|
+
let(:api) { build_deepl_api }
|
|
7
|
+
let(:id) { '012a5576-b551-4d4c-b917-ce01bc8debb6' }
|
|
8
|
+
subject { DeepL::Requests::Glossary::Entries.new(api, id) }
|
|
9
|
+
|
|
10
|
+
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)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
describe '#request' do
|
|
19
|
+
around do |example|
|
|
20
|
+
VCR.use_cassette('glossaries') { example.call }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
context 'When performing a valid request' do
|
|
24
|
+
it 'should return a list of entries in TSV format' do
|
|
25
|
+
entries = subject.request
|
|
26
|
+
expect(entries).to be_kind_of(Array)
|
|
27
|
+
expect(entries).to all(be_a(Array))
|
|
28
|
+
expect(entries.size).to eq(2)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context 'When requesting entries with a valid but non existing glossary id' do
|
|
33
|
+
let(:id) { '00000000-0000-0000-0000-000000000000' }
|
|
34
|
+
subject { DeepL::Requests::Glossary::Entries.new(api, id) }
|
|
35
|
+
it 'should raise a not found error' do
|
|
36
|
+
expect { subject.request }.to raise_error(DeepL::Exceptions::NotFound)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context 'When requesting entries with an invalid glossary id' do
|
|
41
|
+
let(:id) { 'invalid-uuid' }
|
|
42
|
+
subject { DeepL::Requests::Glossary::Entries.new(api, id) }
|
|
43
|
+
it 'should raise a bad request error' do
|
|
44
|
+
expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe DeepL::Requests::Glossary::Find do
|
|
6
|
+
let(:api) { build_deepl_api }
|
|
7
|
+
let(:id) { 'd9ad833f-c818-430c-a3c9-47071384fa3e' }
|
|
8
|
+
let(:options) { {} }
|
|
9
|
+
subject { DeepL::Requests::Glossary::Find.new(api, id, options) }
|
|
10
|
+
|
|
11
|
+
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)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe '#request' do
|
|
20
|
+
around do |example|
|
|
21
|
+
VCR.use_cassette('glossaries') { example.call }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
context 'When performing a valid request' do
|
|
25
|
+
it 'should return a glossary object' do
|
|
26
|
+
glossary = subject.request
|
|
27
|
+
expect(glossary).to be_a(DeepL::Resources::Glossary)
|
|
28
|
+
expect(glossary.id).to eq('d9ad833f-c818-430c-a3c9-47071384fa3e')
|
|
29
|
+
expect(glossary.name).to eq('Mi Glosario')
|
|
30
|
+
expect(glossary.ready).to be(true).or be(false)
|
|
31
|
+
expect(glossary.source_lang).to eq('en')
|
|
32
|
+
expect(glossary.target_lang).to eq('es')
|
|
33
|
+
expect { Time.iso8601(glossary.creation_time) }.not_to raise_error
|
|
34
|
+
expect(glossary.entry_count).to eq(2)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
context 'When requesting a non existing glossary with a valid id' do
|
|
39
|
+
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)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context 'When requesting a non existing glossary with an invalid id' do
|
|
47
|
+
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)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe DeepL::Requests::Glossary::LanguagePairs do
|
|
6
|
+
let(:api) { build_deepl_api }
|
|
7
|
+
let(:options) { {} }
|
|
8
|
+
subject { DeepL::Requests::Glossary::LanguagePairs.new(api, options) }
|
|
9
|
+
|
|
10
|
+
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)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
describe '#request' do
|
|
19
|
+
around do |example|
|
|
20
|
+
VCR.use_cassette('glossaries') { example.call }
|
|
21
|
+
end
|
|
22
|
+
|
|
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
|
|
26
|
+
expect(language_pairs).to be_an(Array)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe DeepL::Requests::Glossary::List do
|
|
6
|
+
let(:api) { build_deepl_api }
|
|
7
|
+
let(:options) { {} }
|
|
8
|
+
subject { DeepL::Requests::Glossary::List.new(api, options) }
|
|
9
|
+
|
|
10
|
+
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)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
describe '#request' do
|
|
19
|
+
around do |example|
|
|
20
|
+
VCR.use_cassette('glossaries') { example.call }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
context 'When requesting a list of all glossaries' do
|
|
24
|
+
it 'should return a glossaries object' do
|
|
25
|
+
glossaries = subject.request
|
|
26
|
+
expect(glossaries).to be_an(Array)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
context 'When performing a bad request' do
|
|
31
|
+
context 'When using an invalid token' do
|
|
32
|
+
let(:api) do
|
|
33
|
+
api = build_deepl_api
|
|
34
|
+
api.configuration.auth_key = 'invalid'
|
|
35
|
+
api
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'should raise an unauthorized error' do
|
|
39
|
+
expect { subject.request }.to raise_error(DeepL::Exceptions::AuthorizationFailed)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe DeepL::Requests::Languages do
|
|
6
|
+
let(:api) { build_deepl_api }
|
|
7
|
+
let(:options) { {} }
|
|
8
|
+
subject { DeepL::Requests::Languages.new(api, options) }
|
|
9
|
+
|
|
10
|
+
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)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
describe '#request' do
|
|
19
|
+
around do |example|
|
|
20
|
+
VCR.use_cassette('languages') { example.call }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
context 'When requesting source languages' do
|
|
24
|
+
it 'should return an usage object' do
|
|
25
|
+
languages = subject.request
|
|
26
|
+
|
|
27
|
+
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
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
context 'When requesting target languages' do
|
|
35
|
+
let(:options) { { type: :target } }
|
|
36
|
+
|
|
37
|
+
it 'should return an usage object' do
|
|
38
|
+
languages = subject.request
|
|
39
|
+
|
|
40
|
+
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
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
context 'When using an invalid type' do
|
|
50
|
+
let(:options) { { type: :invalid } }
|
|
51
|
+
|
|
52
|
+
it 'should return an usage object' do
|
|
53
|
+
message = "Parameter 'type' is invalid. 'source' and 'target' are valid values."
|
|
54
|
+
expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'spec_helper'
|
|
2
4
|
|
|
3
5
|
describe DeepL::Requests::Translate do
|
|
4
|
-
let(:tags_str) { 'p,
|
|
6
|
+
let(:tags_str) { 'p,strong,span' }
|
|
5
7
|
let(:tags_array) { %w[p strong span] }
|
|
6
8
|
|
|
7
9
|
let(:api) { build_deepl_api }
|
|
@@ -9,6 +11,7 @@ describe DeepL::Requests::Translate do
|
|
|
9
11
|
let(:source_lang) { 'EN' }
|
|
10
12
|
let(:target_lang) { 'ES' }
|
|
11
13
|
let(:options) { {} }
|
|
14
|
+
|
|
12
15
|
subject { DeepL::Requests::Translate.new(api, text, source_lang, target_lang, options) }
|
|
13
16
|
|
|
14
17
|
describe '#initialize' do
|
|
@@ -18,6 +21,33 @@ describe DeepL::Requests::Translate do
|
|
|
18
21
|
end
|
|
19
22
|
end
|
|
20
23
|
|
|
24
|
+
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('')
|
|
33
|
+
end
|
|
34
|
+
|
|
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)
|
|
38
|
+
end
|
|
39
|
+
|
|
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)
|
|
43
|
+
end
|
|
44
|
+
|
|
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)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
21
51
|
context 'when using `non_splitting_tags` options' do
|
|
22
52
|
it 'should work with a nil values' do
|
|
23
53
|
request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: nil)
|
|
@@ -88,6 +118,11 @@ describe DeepL::Requests::Translate do
|
|
|
88
118
|
expect(request.options[:split_sentences]).to eq('0')
|
|
89
119
|
end
|
|
90
120
|
|
|
121
|
+
it 'should leave `nonewlines` as is' do
|
|
122
|
+
request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: 'nonewlines')
|
|
123
|
+
expect(request.options[:split_sentences]).to eq('nonewlines')
|
|
124
|
+
end
|
|
125
|
+
|
|
91
126
|
it 'should leave `1` as is' do
|
|
92
127
|
request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: '1')
|
|
93
128
|
expect(request.options[:split_sentences]).to eq('1')
|
|
@@ -115,6 +150,52 @@ describe DeepL::Requests::Translate do
|
|
|
115
150
|
expect(request.options[:preserve_formatting]).to eq('1')
|
|
116
151
|
end
|
|
117
152
|
end
|
|
153
|
+
|
|
154
|
+
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')
|
|
158
|
+
end
|
|
159
|
+
|
|
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')
|
|
163
|
+
end
|
|
164
|
+
|
|
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')
|
|
168
|
+
end
|
|
169
|
+
|
|
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')
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
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)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
it 'should work with a string' do
|
|
183
|
+
request = DeepL::Requests::Translate.new(api, nil, nil, nil, glossary_id: 'sample_id')
|
|
184
|
+
expect(request.options[:glossary_id]).to eq('sample_id')
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
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)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
it 'should work with a string' do
|
|
195
|
+
request = DeepL::Requests::Translate.new(api, nil, nil, nil, formality: 'more')
|
|
196
|
+
expect(request.options[:formality]).to eq('more')
|
|
197
|
+
end
|
|
198
|
+
end
|
|
118
199
|
end
|
|
119
200
|
|
|
120
201
|
describe '#request' do
|
|
@@ -149,6 +230,7 @@ describe DeepL::Requests::Translate do
|
|
|
149
230
|
|
|
150
231
|
context 'When performing a valid request with tag handling' do
|
|
151
232
|
let(:text) { '<p>Sample text</p>' }
|
|
233
|
+
let(:options) { { tag_handling: 'xml' } }
|
|
152
234
|
|
|
153
235
|
it 'should return a text object' do
|
|
154
236
|
text = subject.request
|
|
@@ -161,13 +243,57 @@ describe DeepL::Requests::Translate do
|
|
|
161
243
|
|
|
162
244
|
context 'When performing a valid request and passing a variable' do
|
|
163
245
|
let(:text) { 'Welcome and <code>Hello great World</code> Good Morning!' }
|
|
164
|
-
let(:options) { {
|
|
246
|
+
let(:options) { { tag_handling: 'xml', ignore_tags: %w[code span] } }
|
|
165
247
|
|
|
166
248
|
it 'should return a text object' do
|
|
167
249
|
text = subject.request
|
|
168
250
|
|
|
169
251
|
expect(text).to be_a(DeepL::Resources::Text)
|
|
170
|
-
expect(text.text).to eq('Bienvenido y <code>Hello great World</code> Buenos días!')
|
|
252
|
+
expect(text.text).to eq('Bienvenido y <code>Hello great World</code> ¡Buenos días!')
|
|
253
|
+
expect(text.detected_source_language).to eq('EN')
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
context 'When performing a valid request with an HTML document' do
|
|
258
|
+
let(:text) do
|
|
259
|
+
<<~XML
|
|
260
|
+
<document>
|
|
261
|
+
<meta>
|
|
262
|
+
<title>A document's title</title>
|
|
263
|
+
</meta>
|
|
264
|
+
<content>
|
|
265
|
+
<par>This is the first sentence. Followed by a second one.</par>
|
|
266
|
+
<par>This is the third sentence.</par>
|
|
267
|
+
</content>
|
|
268
|
+
</document>
|
|
269
|
+
XML
|
|
270
|
+
end
|
|
271
|
+
let(:options) do
|
|
272
|
+
{
|
|
273
|
+
tag_handling: 'xml',
|
|
274
|
+
split_sentences: 'nonewlines',
|
|
275
|
+
outline_detection: false,
|
|
276
|
+
splitting_tags: %w[title par]
|
|
277
|
+
}
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
it 'should return a text object' do
|
|
281
|
+
text = subject.request
|
|
282
|
+
|
|
283
|
+
expect(text).to be_a(DeepL::Resources::Text)
|
|
284
|
+
expect(text.text).to eq(
|
|
285
|
+
<<~XML
|
|
286
|
+
<document>
|
|
287
|
+
<meta>
|
|
288
|
+
<title>El título de un documento</title>
|
|
289
|
+
</meta>
|
|
290
|
+
<content>
|
|
291
|
+
<par>Es la primera frase. Seguido de una segunda.</par>
|
|
292
|
+
<par>Esta es la tercera frase.</par>
|
|
293
|
+
</content>
|
|
294
|
+
</document>
|
|
295
|
+
XML
|
|
296
|
+
)
|
|
171
297
|
expect(text.detected_source_language).to eq('EN')
|
|
172
298
|
end
|
|
173
299
|
end
|
|
@@ -198,10 +324,19 @@ describe DeepL::Requests::Translate do
|
|
|
198
324
|
let(:target_lang) { nil }
|
|
199
325
|
|
|
200
326
|
it 'should raise a bad request error' do
|
|
201
|
-
message = "
|
|
327
|
+
message = "Value for 'target_lang' not supported."
|
|
202
328
|
expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
|
|
203
329
|
end
|
|
204
330
|
end
|
|
205
331
|
end
|
|
332
|
+
|
|
333
|
+
context 'When performing a request with too many texts' do
|
|
334
|
+
let(:text) { Array.new(10_000) { |i| "This is the sentence number #{i}" } }
|
|
335
|
+
|
|
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/)
|
|
339
|
+
end
|
|
340
|
+
end
|
|
206
341
|
end
|
|
207
342
|
end
|
data/spec/requests/usage_spec.rb
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe DeepL::Resources::Glossary do
|
|
6
|
+
subject do
|
|
7
|
+
described_class.new({
|
|
8
|
+
'glossary_id' => 'def3a26b-3e84-45b3-84ae-0c0aaf3525f7',
|
|
9
|
+
'name' => 'Mein Glossar',
|
|
10
|
+
'ready' => true,
|
|
11
|
+
'source_lang' => 'EN',
|
|
12
|
+
'target_lang' => 'DE',
|
|
13
|
+
'creation_time' => '2021-08-03T14:16:18.329Z',
|
|
14
|
+
'entry_count' => 1
|
|
15
|
+
}, nil, nil)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
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
|
+
end
|
|
23
|
+
|
|
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)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe DeepL::Resources::LanguagePair do
|
|
6
|
+
subject { described_class.new('en', 'de', nil, nil) }
|
|
7
|
+
|
|
8
|
+
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
|
+
end
|
|
13
|
+
|
|
14
|
+
it 'should assign the attributes' do
|
|
15
|
+
expect(subject.source_lang).to eq('en')
|
|
16
|
+
expect(subject.target_lang).to eq('de')
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|