deepl-rb 2.3.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +7 -4
  3. data/README.md +102 -1
  4. data/VERSION +1 -1
  5. data/deepl-rb.gemspec +25 -4
  6. data/lib/deepl/configuration.rb +1 -1
  7. data/lib/deepl/exceptions/not_found.rb +13 -0
  8. data/lib/deepl/exceptions/not_supported.rb +11 -0
  9. data/lib/deepl/glossary_api.rb +35 -0
  10. data/lib/deepl/requests/base.rb +21 -4
  11. data/lib/deepl/requests/glossary/create.rb +45 -0
  12. data/lib/deepl/requests/glossary/destroy.rb +30 -0
  13. data/lib/deepl/requests/glossary/entries.rb +30 -0
  14. data/lib/deepl/requests/glossary/find.rb +31 -0
  15. data/lib/deepl/requests/glossary/language_pairs.rb +31 -0
  16. data/lib/deepl/requests/glossary/list.rb +30 -0
  17. data/lib/deepl/requests/languages.rb +3 -1
  18. data/lib/deepl/requests/translate.rb +1 -0
  19. data/lib/deepl/resources/glossary.rb +25 -0
  20. data/lib/deepl/resources/language.rb +8 -1
  21. data/lib/deepl/resources/language_pair.rb +20 -0
  22. data/lib/deepl.rb +16 -0
  23. data/spec/api/deepl_spec.rb +189 -0
  24. data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +374 -0
  25. data/spec/fixtures/vcr_cassettes/deepl_languages.yml +3 -1
  26. data/spec/fixtures/vcr_cassettes/deepl_translate.yml +212 -1
  27. data/spec/fixtures/vcr_cassettes/deepl_usage.yml +3 -1
  28. data/spec/fixtures/vcr_cassettes/glossaries.yml +480 -0
  29. data/spec/fixtures/vcr_cassettes/languages.yml +9 -3
  30. data/spec/fixtures/vcr_cassettes/translate_texts.yml +340 -286
  31. data/spec/fixtures/vcr_cassettes/usage.yml +3 -1
  32. data/spec/requests/glossary/create_spec.rb +55 -0
  33. data/spec/requests/glossary/destroy_spec.rb +50 -0
  34. data/spec/requests/glossary/entries_spec.rb +48 -0
  35. data/spec/requests/glossary/find_spec.rb +54 -0
  36. data/spec/requests/glossary/language_pairs_spec.rb +30 -0
  37. data/spec/requests/glossary/list_spec.rb +44 -0
  38. data/spec/requests/translate_spec.rb +51 -0
  39. data/spec/resources/glossary_spec.rb +35 -0
  40. data/spec/resources/language_pair_spec.rb +20 -0
  41. data/spec/resources/language_spec.rb +23 -1
  42. data/spec/spec_helper.rb +2 -2
  43. metadata +24 -3
@@ -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
@@ -90,6 +90,11 @@ describe DeepL::Requests::Translate do
90
90
  expect(request.options[:split_sentences]).to eq('0')
91
91
  end
92
92
 
93
+ it 'should leave `nonewlines` as is' do
94
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: 'nonewlines')
95
+ expect(request.options[:split_sentences]).to eq('nonewlines')
96
+ end
97
+
93
98
  it 'should leave `1` as is' do
94
99
  request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: '1')
95
100
  expect(request.options[:split_sentences]).to eq('1')
@@ -117,6 +122,52 @@ describe DeepL::Requests::Translate do
117
122
  expect(request.options[:preserve_formatting]).to eq('1')
118
123
  end
119
124
  end
125
+
126
+ context 'when using `outline_detection` options' do
127
+ it 'should convert `true` to `1`' do
128
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: true)
129
+ expect(request.options[:outline_detection]).to eq('1')
130
+ end
131
+
132
+ it 'should convert `false` to `0`' do
133
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: false)
134
+ expect(request.options[:outline_detection]).to eq('0')
135
+ end
136
+
137
+ it 'should leave `0` as is' do
138
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: '0')
139
+ expect(request.options[:outline_detection]).to eq('0')
140
+ end
141
+
142
+ it 'should leave `1` as is' do
143
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: '1')
144
+ expect(request.options[:outline_detection]).to eq('1')
145
+ end
146
+ end
147
+
148
+ context 'when using `glossary_id` options' do
149
+ it 'should work with a nil values' do
150
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, glossary_id: nil)
151
+ expect(request.options[:glossary_id]).to eq(nil)
152
+ end
153
+
154
+ it 'should work with a string' do
155
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, glossary_id: 'sample_id')
156
+ expect(request.options[:glossary_id]).to eq('sample_id')
157
+ end
158
+ end
159
+
160
+ context 'when using `formality` options' do
161
+ it 'should work with a nil values' do
162
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, formality: nil)
163
+ expect(request.options[:formality]).to eq(nil)
164
+ end
165
+
166
+ it 'should work with a string' do
167
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, formality: 'more')
168
+ expect(request.options[:formality]).to eq('more')
169
+ end
170
+ end
120
171
  end
121
172
 
122
173
  describe '#request' do
@@ -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
@@ -3,7 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe DeepL::Resources::Language do
6
- subject { described_class.new('EN', 'English', nil, nil) }
6
+ subject { described_class.new('EN', 'English', nil, nil, nil) }
7
7
 
8
8
  describe '#initialize' do
9
9
  context 'When building a basic object' do
@@ -15,6 +15,28 @@ describe DeepL::Resources::Language do
15
15
  expect(subject.code).to eq('EN')
16
16
  expect(subject.name).to eq('English')
17
17
  end
18
+
19
+ it 'should not define the supports formality method' do
20
+ expect { subject.supports_formality? }.to raise_error(DeepL::Exceptions::NotSupported)
21
+ end
22
+ end
23
+
24
+ context 'when building a target language object' do
25
+ subject { described_class.new('EN', 'English', true, nil, nil) }
26
+
27
+ it 'should create a resource' do
28
+ expect(subject).to be_a(described_class)
29
+ end
30
+
31
+ it 'should assign the attributes' do
32
+ expect(subject.code).to eq('EN')
33
+ expect(subject.name).to eq('English')
34
+ end
35
+
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
+ end
18
40
  end
19
41
  end
20
42
  end
data/spec/spec_helper.rb CHANGED
@@ -20,8 +20,8 @@ VCR.configure do |config|
20
20
  config.hook_into :webmock
21
21
  config.filter_sensitive_data('VALID_TOKEN') { ENV['DEEPL_AUTH_KEY'] }
22
22
  config.default_cassette_options = {
23
- record: :new_episodes,
24
- match_requests_on: %i[method uri body]
23
+ # record: :new_episodes,
24
+ match_requests_on: %i[method uri body headers]
25
25
  }
26
26
  end
27
27
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deepl-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Herzog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-17 00:00:00.000000000 Z
11
+ date: 2022-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: juwelier
@@ -62,28 +62,49 @@ files:
62
62
  - lib/deepl/exceptions/bad_request.rb
63
63
  - lib/deepl/exceptions/error.rb
64
64
  - lib/deepl/exceptions/limit_exceeded.rb
65
+ - lib/deepl/exceptions/not_found.rb
66
+ - lib/deepl/exceptions/not_supported.rb
65
67
  - lib/deepl/exceptions/quota_exceeded.rb
66
68
  - lib/deepl/exceptions/request_error.rb
69
+ - lib/deepl/glossary_api.rb
67
70
  - lib/deepl/requests/base.rb
71
+ - lib/deepl/requests/glossary/create.rb
72
+ - lib/deepl/requests/glossary/destroy.rb
73
+ - lib/deepl/requests/glossary/entries.rb
74
+ - lib/deepl/requests/glossary/find.rb
75
+ - lib/deepl/requests/glossary/language_pairs.rb
76
+ - lib/deepl/requests/glossary/list.rb
68
77
  - lib/deepl/requests/languages.rb
69
78
  - lib/deepl/requests/translate.rb
70
79
  - lib/deepl/requests/usage.rb
71
80
  - lib/deepl/resources/base.rb
81
+ - lib/deepl/resources/glossary.rb
72
82
  - lib/deepl/resources/language.rb
83
+ - lib/deepl/resources/language_pair.rb
73
84
  - lib/deepl/resources/text.rb
74
85
  - lib/deepl/resources/usage.rb
75
86
  - spec/api/api_spec.rb
76
87
  - spec/api/configuration_spec.rb
77
88
  - spec/api/deepl_spec.rb
89
+ - spec/fixtures/vcr_cassettes/deepl_glossaries.yml
78
90
  - spec/fixtures/vcr_cassettes/deepl_languages.yml
79
91
  - spec/fixtures/vcr_cassettes/deepl_translate.yml
80
92
  - spec/fixtures/vcr_cassettes/deepl_usage.yml
93
+ - spec/fixtures/vcr_cassettes/glossaries.yml
81
94
  - spec/fixtures/vcr_cassettes/languages.yml
82
95
  - spec/fixtures/vcr_cassettes/translate_texts.yml
83
96
  - spec/fixtures/vcr_cassettes/usage.yml
97
+ - spec/requests/glossary/create_spec.rb
98
+ - spec/requests/glossary/destroy_spec.rb
99
+ - spec/requests/glossary/entries_spec.rb
100
+ - spec/requests/glossary/find_spec.rb
101
+ - spec/requests/glossary/language_pairs_spec.rb
102
+ - spec/requests/glossary/list_spec.rb
84
103
  - spec/requests/languages_spec.rb
85
104
  - spec/requests/translate_spec.rb
86
105
  - spec/requests/usage_spec.rb
106
+ - spec/resources/glossary_spec.rb
107
+ - spec/resources/language_pair_spec.rb
87
108
  - spec/resources/language_spec.rb
88
109
  - spec/resources/text_spec.rb
89
110
  - spec/resources/usage_spec.rb
@@ -107,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
128
  - !ruby/object:Gem::Version
108
129
  version: '0'
109
130
  requirements: []
110
- rubygems_version: 3.1.2
131
+ rubygems_version: 3.3.7
111
132
  signing_key:
112
133
  specification_version: 4
113
134
  summary: A simple ruby wrapper for the DeepL API