deepl-rb 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +7 -4
  3. data/README.md +94 -0
  4. data/VERSION +1 -1
  5. data/deepl-rb.gemspec +24 -4
  6. data/lib/deepl/exceptions/not_found.rb +13 -0
  7. data/lib/deepl/glossary_api.rb +35 -0
  8. data/lib/deepl/requests/base.rb +21 -4
  9. data/lib/deepl/requests/glossary/create.rb +45 -0
  10. data/lib/deepl/requests/glossary/destroy.rb +30 -0
  11. data/lib/deepl/requests/glossary/entries.rb +30 -0
  12. data/lib/deepl/requests/glossary/find.rb +31 -0
  13. data/lib/deepl/requests/glossary/language_pairs.rb +31 -0
  14. data/lib/deepl/requests/glossary/list.rb +30 -0
  15. data/lib/deepl/resources/glossary.rb +25 -0
  16. data/lib/deepl/resources/language_pair.rb +20 -0
  17. data/lib/deepl.rb +15 -0
  18. data/spec/api/deepl_spec.rb +189 -0
  19. data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +374 -0
  20. data/spec/fixtures/vcr_cassettes/deepl_languages.yml +3 -1
  21. data/spec/fixtures/vcr_cassettes/deepl_translate.yml +212 -1
  22. data/spec/fixtures/vcr_cassettes/deepl_usage.yml +3 -1
  23. data/spec/fixtures/vcr_cassettes/glossaries.yml +480 -0
  24. data/spec/fixtures/vcr_cassettes/languages.yml +9 -3
  25. data/spec/fixtures/vcr_cassettes/translate_texts.yml +340 -322
  26. data/spec/fixtures/vcr_cassettes/usage.yml +3 -1
  27. data/spec/requests/glossary/create_spec.rb +55 -0
  28. data/spec/requests/glossary/destroy_spec.rb +50 -0
  29. data/spec/requests/glossary/entries_spec.rb +48 -0
  30. data/spec/requests/glossary/find_spec.rb +54 -0
  31. data/spec/requests/glossary/language_pairs_spec.rb +30 -0
  32. data/spec/requests/glossary/list_spec.rb +44 -0
  33. data/spec/resources/glossary_spec.rb +35 -0
  34. data/spec/resources/language_pair_spec.rb +20 -0
  35. data/spec/spec_helper.rb +2 -2
  36. metadata +23 -3
@@ -2,11 +2,13 @@
2
2
  http_interactions:
3
3
  - request:
4
4
  method: get
5
- uri: https://api-free.deepl.com/v2/usage?auth_key=VALID_TOKEN
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:
@@ -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,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
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.4.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: 2022-01-24 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,29 +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
65
66
  - lib/deepl/exceptions/not_supported.rb
66
67
  - lib/deepl/exceptions/quota_exceeded.rb
67
68
  - lib/deepl/exceptions/request_error.rb
69
+ - lib/deepl/glossary_api.rb
68
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
69
77
  - lib/deepl/requests/languages.rb
70
78
  - lib/deepl/requests/translate.rb
71
79
  - lib/deepl/requests/usage.rb
72
80
  - lib/deepl/resources/base.rb
81
+ - lib/deepl/resources/glossary.rb
73
82
  - lib/deepl/resources/language.rb
83
+ - lib/deepl/resources/language_pair.rb
74
84
  - lib/deepl/resources/text.rb
75
85
  - lib/deepl/resources/usage.rb
76
86
  - spec/api/api_spec.rb
77
87
  - spec/api/configuration_spec.rb
78
88
  - spec/api/deepl_spec.rb
89
+ - spec/fixtures/vcr_cassettes/deepl_glossaries.yml
79
90
  - spec/fixtures/vcr_cassettes/deepl_languages.yml
80
91
  - spec/fixtures/vcr_cassettes/deepl_translate.yml
81
92
  - spec/fixtures/vcr_cassettes/deepl_usage.yml
93
+ - spec/fixtures/vcr_cassettes/glossaries.yml
82
94
  - spec/fixtures/vcr_cassettes/languages.yml
83
95
  - spec/fixtures/vcr_cassettes/translate_texts.yml
84
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
85
103
  - spec/requests/languages_spec.rb
86
104
  - spec/requests/translate_spec.rb
87
105
  - spec/requests/usage_spec.rb
106
+ - spec/resources/glossary_spec.rb
107
+ - spec/resources/language_pair_spec.rb
88
108
  - spec/resources/language_spec.rb
89
109
  - spec/resources/text_spec.rb
90
110
  - spec/resources/usage_spec.rb
@@ -108,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
128
  - !ruby/object:Gem::Version
109
129
  version: '0'
110
130
  requirements: []
111
- rubygems_version: 3.1.2
131
+ rubygems_version: 3.3.7
112
132
  signing_key:
113
133
  specification_version: 4
114
134
  summary: A simple ruby wrapper for the DeepL API