deepl-rb 3.2.0 → 3.4.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 (96) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -27
  3. data/README.md +71 -0
  4. metadata +3 -95
  5. data/.circleci/config.yml +0 -27
  6. data/.github/workflows/add_issues_to_kanban.yml +0 -16
  7. data/.gitlab-ci.yml +0 -135
  8. data/.rubocop.yml +0 -51
  9. data/CODE_OF_CONDUCT.md +0 -132
  10. data/CONTRIBUTING.md +0 -37
  11. data/Gemfile +0 -24
  12. data/Rakefile +0 -53
  13. data/SECURITY.md +0 -58
  14. data/VERSION +0 -1
  15. data/deepl-rb.gemspec +0 -132
  16. data/lib/deepl/api.rb +0 -22
  17. data/lib/deepl/configuration.rb +0 -59
  18. data/lib/deepl/constants/base_constant.rb +0 -18
  19. data/lib/deepl/constants/formality.rb +0 -16
  20. data/lib/deepl/constants/model_type.rb +0 -14
  21. data/lib/deepl/constants/split_sentences.rb +0 -14
  22. data/lib/deepl/constants/tag_handling.rb +0 -13
  23. data/lib/deepl/constants/tone.rb +0 -20
  24. data/lib/deepl/constants/writing_style.rb +0 -20
  25. data/lib/deepl/document_api.rb +0 -121
  26. data/lib/deepl/exceptions/authorization_failed.rb +0 -14
  27. data/lib/deepl/exceptions/bad_request.rb +0 -16
  28. data/lib/deepl/exceptions/document_translation_error.rb +0 -15
  29. data/lib/deepl/exceptions/error.rb +0 -14
  30. data/lib/deepl/exceptions/limit_exceeded.rb +0 -18
  31. data/lib/deepl/exceptions/not_found.rb +0 -16
  32. data/lib/deepl/exceptions/not_supported.rb +0 -14
  33. data/lib/deepl/exceptions/quota_exceeded.rb +0 -14
  34. data/lib/deepl/exceptions/request_entity_too_large.rb +0 -15
  35. data/lib/deepl/exceptions/request_error.rb +0 -21
  36. data/lib/deepl/exceptions/server_error.rb +0 -18
  37. data/lib/deepl/glossary_api.rb +0 -38
  38. data/lib/deepl/requests/base.rb +0 -163
  39. data/lib/deepl/requests/document/download.rb +0 -44
  40. data/lib/deepl/requests/document/get_status.rb +0 -44
  41. data/lib/deepl/requests/document/upload.rb +0 -64
  42. data/lib/deepl/requests/glossary/create.rb +0 -59
  43. data/lib/deepl/requests/glossary/destroy.rb +0 -37
  44. data/lib/deepl/requests/glossary/entries.rb +0 -37
  45. data/lib/deepl/requests/glossary/find.rb +0 -38
  46. data/lib/deepl/requests/glossary/language_pairs.rb +0 -38
  47. data/lib/deepl/requests/glossary/list.rb +0 -37
  48. data/lib/deepl/requests/languages.rb +0 -37
  49. data/lib/deepl/requests/rephrase.rb +0 -54
  50. data/lib/deepl/requests/translate.rb +0 -82
  51. data/lib/deepl/requests/usage.rb +0 -33
  52. data/lib/deepl/resources/base.rb +0 -17
  53. data/lib/deepl/resources/document_handle.rb +0 -57
  54. data/lib/deepl/resources/document_translation_status.rb +0 -54
  55. data/lib/deepl/resources/glossary.rb +0 -28
  56. data/lib/deepl/resources/language.rb +0 -30
  57. data/lib/deepl/resources/language_pair.rb +0 -23
  58. data/lib/deepl/resources/text.rb +0 -24
  59. data/lib/deepl/resources/usage.rb +0 -27
  60. data/lib/deepl/utils/backoff_timer.rb +0 -46
  61. data/lib/deepl/utils/exception_builder.rb +0 -34
  62. data/lib/deepl.rb +0 -142
  63. data/lib/http_client_options.rb +0 -22
  64. data/spec/api/api_spec.rb +0 -20
  65. data/spec/api/configuration_spec.rb +0 -122
  66. data/spec/api/deepl_spec.rb +0 -431
  67. data/spec/constants/constants_spec.rb +0 -158
  68. data/spec/fixtures/vcr_cassettes/deepl_document.yml +0 -95
  69. data/spec/fixtures/vcr_cassettes/deepl_document_download.yml +0 -1214
  70. data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +0 -1163
  71. data/spec/fixtures/vcr_cassettes/deepl_languages.yml +0 -54
  72. data/spec/fixtures/vcr_cassettes/deepl_translate.yml +0 -358
  73. data/spec/fixtures/vcr_cassettes/deepl_usage.yml +0 -129
  74. data/spec/fixtures/vcr_cassettes/glossaries.yml +0 -1702
  75. data/spec/fixtures/vcr_cassettes/languages.yml +0 -229
  76. data/spec/fixtures/vcr_cassettes/rephrase_texts.yml +0 -401
  77. data/spec/fixtures/vcr_cassettes/translate_texts.yml +0 -10372
  78. data/spec/fixtures/vcr_cassettes/usage.yml +0 -171
  79. data/spec/integration_tests/document_api_spec.rb +0 -155
  80. data/spec/integration_tests/integration_test_utils.rb +0 -170
  81. data/spec/requests/glossary/create_spec.rb +0 -65
  82. data/spec/requests/glossary/destroy_spec.rb +0 -66
  83. data/spec/requests/glossary/entries_spec.rb +0 -62
  84. data/spec/requests/glossary/find_spec.rb +0 -68
  85. data/spec/requests/glossary/language_pairs_spec.rb +0 -40
  86. data/spec/requests/glossary/list_spec.rb +0 -54
  87. data/spec/requests/languages_spec.rb +0 -68
  88. data/spec/requests/rephrase_spec.rb +0 -172
  89. data/spec/requests/translate_spec.rb +0 -410
  90. data/spec/requests/usage_spec.rb +0 -43
  91. data/spec/resources/glossary_spec.rb +0 -38
  92. data/spec/resources/language_pair_spec.rb +0 -23
  93. data/spec/resources/language_spec.rb +0 -45
  94. data/spec/resources/text_spec.rb +0 -23
  95. data/spec/resources/usage_spec.rb +0 -35
  96. data/spec/spec_helper.rb +0 -92
@@ -1,27 +0,0 @@
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.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Resources
8
- class Usage < Base
9
- attr_reader :character_count, :character_limit
10
-
11
- def initialize(character_count, character_limit, *args)
12
- super(*args)
13
-
14
- @character_count = character_count
15
- @character_limit = character_limit
16
- end
17
-
18
- def to_s
19
- "#{character_count} / #{character_limit}"
20
- end
21
-
22
- def quota_exceeded?
23
- character_count >= character_limit
24
- end
25
- end
26
- end
27
- end
@@ -1,46 +0,0 @@
1
- # Copyright 2024 DeepL SE (https://www.deepl.com)
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Utils
8
- class BackoffTimer
9
- # Implements exponential-backoff strategy.
10
- # This strategy is based on the GRPC Connection Backoff Protocol:
11
- # https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md
12
-
13
- BACKOFF_INITIAL = 1.0
14
- BACKOFF_MAX = 120.0
15
- BACKOFF_JITTER = 0.23
16
- BACKOFF_MULTIPLIER = 1.6
17
-
18
- attr_reader :num_retries
19
-
20
- def initialize(min_connection_timeout = 10.0)
21
- @num_retries = 0
22
- @backoff = BACKOFF_INITIAL
23
- @deadline = Time.now.to_f + @backoff
24
- @min_connection_timeout = min_connection_timeout
25
- end
26
-
27
- def current_request_timeout
28
- [time_until_deadline, @min_connection_timeout].max
29
- end
30
-
31
- def time_until_deadline
32
- [@deadline - Time.now.to_f, 0.0].max
33
- end
34
-
35
- def sleep_until_deadline
36
- sleep(time_until_deadline)
37
- # Apply multiplier to current backoff time
38
- @backoff = [@backoff * BACKOFF_MULTIPLIER, BACKOFF_MAX].min
39
- # Get deadline by applying jitter as a proportion of backoff:
40
- # if jitter is 0.1, then multiply backoff by random value in [0.9, 1.1]
41
- @deadline = Time.now.to_f + (@backoff * (1 + (BACKOFF_JITTER * rand(-1.0..1.0))))
42
- @num_retries += 1
43
- end
44
- end
45
- end
46
- end
@@ -1,34 +0,0 @@
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.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Utils
8
- class ExceptionBuilder
9
- attr_reader :request, :response
10
-
11
- def self.error_class_from_response_code(code) # rubocop:disable Metrics/CyclomaticComplexity
12
- case code
13
- when 400 then Exceptions::BadRequest
14
- when 401, 403 then Exceptions::AuthorizationFailed
15
- when 404 then Exceptions::NotFound
16
- when 413 then Exceptions::RequestEntityTooLarge
17
- when 429 then Exceptions::LimitExceeded
18
- when 456 then Exceptions::QuotaExceeded
19
- when 500..599 then Exceptions::ServerError
20
- else Exceptions::RequestError
21
- end
22
- end
23
-
24
- def initialize(response)
25
- @response = response
26
- end
27
-
28
- def build
29
- error_class = self.class.error_class_from_response_code(response.code.to_i)
30
- error_class.new(response)
31
- end
32
- end
33
- end
34
- end
data/lib/deepl.rb DELETED
@@ -1,142 +0,0 @@
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.
4
- # frozen_string_literal: true
5
-
6
- # -- Dependencies
7
- require 'json'
8
- require 'net/http'
9
-
10
- # -- Exceptions
11
- require 'deepl/exceptions/error'
12
- require 'deepl/exceptions/request_error'
13
- require 'deepl/exceptions/authorization_failed'
14
- require 'deepl/exceptions/bad_request'
15
- require 'deepl/exceptions/limit_exceeded'
16
- require 'deepl/exceptions/quota_exceeded'
17
- require 'deepl/exceptions/not_found'
18
- require 'deepl/exceptions/not_supported'
19
- require 'deepl/exceptions/request_entity_too_large'
20
- require 'deepl/exceptions/document_translation_error'
21
- require 'deepl/exceptions/server_error'
22
-
23
- # -- Requests
24
- require 'deepl/requests/base'
25
- require 'deepl/requests/document/download'
26
- require 'deepl/requests/document/get_status'
27
- require 'deepl/requests/document/upload'
28
- require 'deepl/requests/glossary/create'
29
- require 'deepl/requests/glossary/destroy'
30
- require 'deepl/requests/glossary/entries'
31
- require 'deepl/requests/glossary/find'
32
- require 'deepl/requests/glossary/language_pairs'
33
- require 'deepl/requests/glossary/list'
34
- require 'deepl/requests/languages'
35
- require 'deepl/requests/translate'
36
- require 'deepl/requests/usage'
37
- require 'deepl/requests/rephrase'
38
-
39
- # -- Responses and resources
40
- require 'deepl/resources/base'
41
- require 'deepl/resources/document_handle'
42
- require 'deepl/resources/document_translation_status'
43
- require 'deepl/resources/glossary'
44
- require 'deepl/resources/language'
45
- require 'deepl/resources/language_pair'
46
- require 'deepl/resources/text'
47
- require 'deepl/resources/usage'
48
-
49
- # -- Utils
50
- require 'deepl/utils/exception_builder'
51
- require 'deepl/utils/backoff_timer'
52
-
53
- # -- Constants
54
- require 'deepl/constants/base_constant'
55
- require 'deepl/constants/formality'
56
- require 'deepl/constants/model_type'
57
- require 'deepl/constants/split_sentences'
58
- require 'deepl/constants/tag_handling'
59
- require 'deepl/constants/tone'
60
- require 'deepl/constants/writing_style'
61
-
62
- # -- HTTP Utils
63
- require 'http_client_options'
64
-
65
- # -- Other wrappers
66
- require 'deepl/api'
67
- require 'deepl/configuration'
68
- require 'deepl/document_api'
69
- require 'deepl/glossary_api'
70
-
71
- # -- Gem interface
72
- module DeepL
73
- extend self
74
-
75
- ## -- API shortcuts
76
-
77
- def api
78
- @api ||= API.new(configuration)
79
- end
80
-
81
- def languages(options = {})
82
- Requests::Languages.new(api, options).request
83
- end
84
-
85
- def translate(text, source_lang, target_lang, options = {})
86
- configure if @configuration.nil?
87
- Requests::Translate.new(api, text, source_lang, target_lang, options).request
88
- end
89
-
90
- def document(options = {})
91
- configure if @configuration.nil?
92
- DocumentApi.new(api, options)
93
- end
94
-
95
- def glossaries(options = {})
96
- configure if @configuration.nil?
97
- GlossaryApi.new(api, options)
98
- end
99
-
100
- def usage(options = {})
101
- configure if @configuration.nil?
102
- Requests::Usage.new(api, options).request
103
- end
104
-
105
- def http_client
106
- @http_client
107
- end
108
-
109
- def with_session(client_options = HTTPClientOptions.new()) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
110
- raise ArgumentError 'This method requires a block to be passed in which contains the actual API calls, see README for example usage.' unless block_given? # rubocop:disable Layout/LineLength
111
-
112
- has_proxy = client_options.proxy.key?('proxy_addr') and client_options.proxy.key?('proxy_port')
113
- begin
114
- uri = URI(configuration.host)
115
- http = Net::HTTP.new(uri.host, uri.port, has_proxy ? client_options.proxy['proxy_addr'] : nil,
116
- has_proxy ? client_options.proxy['proxy_port'] : nil)
117
- http.use_ssl = client_options.enable_ssl_verification
118
- http.ca_file = client_options.cert_path if client_options.cert_path
119
- http.open_timeout = client_options.open_timeout unless client_options.open_timeout.nil?
120
- http.read_timeout = client_options.read_timeout unless client_options.read_timeout.nil?
121
- http.write_timeout = client_options.write_timeout unless client_options.write_timeout.nil?
122
- http.ssl_timeout = client_options.ssl_timeout unless client_options.ssl_timeout.nil?
123
- http.start
124
- @http_client = http
125
- api.update_http_client(http)
126
- yield
127
- ensure
128
- http.finish
129
- end
130
- end
131
-
132
- # -- Configuration
133
-
134
- def configuration
135
- @configuration ||= Configuration.new
136
- end
137
-
138
- def configure
139
- yield configuration if block_given?
140
- configuration.validate!
141
- end
142
- end
@@ -1,22 +0,0 @@
1
- # Copyright 2024 DeepL SE (https://www.deepl.com)
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- class HTTPClientOptions
8
- attr_reader :proxy, :send_platform_info, :enable_ssl_verification, :cert_path, :open_timeout,
9
- :read_timeout, :write_timeout, :ssl_timeout
10
-
11
- def initialize(proxy, cert_path, enable_ssl_verification: true, open_timeout: nil, # rubocop:disable Metrics/ParameterLists
12
- read_timeout: nil, write_timeout: nil, ssl_timeout: nil)
13
- @proxy = proxy
14
- @enable_ssl_verification = enable_ssl_verification
15
- @cert_path = cert_path
16
- @open_timeout = open_timeout
17
- @read_timeout = read_timeout
18
- @write_timeout = write_timeout
19
- @ssl_timeout = ssl_timeout
20
- end
21
- end
22
- end
data/spec/api/api_spec.rb DELETED
@@ -1,20 +0,0 @@
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.
4
- # frozen_string_literal: true
5
-
6
- require 'spec_helper'
7
-
8
- describe DeepL::API do
9
- subject(:api) { described_class.new(configuration) }
10
-
11
- let(:configuration) { DeepL::Configuration.new }
12
-
13
- describe '#initialize' do
14
- context 'when building an API object' do
15
- it 'saves the configuration' do
16
- expect(api.configuration).to be(configuration)
17
- end
18
- end
19
- end
20
- end
@@ -1,122 +0,0 @@
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.
4
- # frozen_string_literal: true
5
-
6
- require 'logger'
7
- require 'spec_helper'
8
-
9
- describe DeepL::Configuration do
10
- subject(:config) { described_class.new(attributes) }
11
-
12
- let(:attributes) { {} }
13
-
14
- around do |tests|
15
- tmp_env = ENV.to_hash
16
- ENV.clear
17
- tests.call
18
- ENV.replace(tmp_env)
19
- end
20
-
21
- describe '#initialize' do
22
- context 'when using default configuration attributes' do
23
- it 'uses default attributes' do
24
- expect(config.auth_key).to eq(ENV.fetch('DEEPL_AUTH_KEY', nil))
25
- expect(config.host).to eq('https://api.deepl.com')
26
- expect(config.max_doc_status_queries).to be_nil
27
- expect(config.version).to eq('v2')
28
- end
29
-
30
- it 'sends platform information' do
31
- expect(config.user_agent).to include('deepl-ruby/')
32
- expect(config.user_agent).to include('(')
33
- expect(config.user_agent).to include(')')
34
- expect(config.user_agent).to include(' ruby/')
35
- end
36
- end
37
-
38
- context 'when using custom configuration attributes' do
39
- let(:logger) { Logger.new($stdout) }
40
- let(:attributes) { { auth_key: 'SAMPLE', host: 'https://api-free.deepl.com', logger: logger, max_doc_status_queries: 42, max_network_retries: 43, version: 'v1' } }
41
-
42
- it 'uses custom attributes' do
43
- expect(config.auth_key).to eq(attributes[:auth_key])
44
- expect(config.host).to eq(attributes[:host])
45
- expect(config.max_doc_status_queries).to eq(attributes[:max_doc_status_queries])
46
- expect(config.max_network_retries).to eq(attributes[:max_network_retries])
47
- expect(config.version).to eq(attributes[:version])
48
- end
49
- end
50
-
51
- context 'when using a free key' do
52
- let(:attributes) { { auth_key: '123e4567-e89b-12d3-a456-426614174000:fx' } }
53
-
54
- it 'uses the free API URL' do
55
- expect(config.host).to eq('https://api-free.deepl.com')
56
- end
57
- end
58
-
59
- context 'when using a pro key' do
60
- let(:attributes) { { auth_key: '123e4567-e89b-12d3-a456-426614174000' } }
61
-
62
- it 'uses the pro API URL' do
63
- expect(config.host).to eq('https://api.deepl.com')
64
- end
65
- end
66
-
67
- context 'when opting out of sending platform info' do
68
- subject(:config) { described_class.new(attributes, nil, nil, false) }
69
-
70
- it 'does not send platform information' do
71
- expect(config.user_agent).to include('deepl-ruby/')
72
- expect(config.user_agent).not_to include('(')
73
- expect(config.user_agent).not_to include(')')
74
- expect(config.user_agent).not_to include(' ruby/')
75
- end
76
- end
77
-
78
- context 'when registering an app and sending platform info' do
79
- subject(:config) { described_class.new(attributes, 'MyTestApp', '0.1.3', true) }
80
-
81
- it 'sends platform and app info' do
82
- expect(config.user_agent).to include('deepl-ruby/')
83
- expect(config.user_agent).to include('(')
84
- expect(config.user_agent).to include(')')
85
- expect(config.user_agent).to include(' ruby/')
86
- expect(config.user_agent).to include('MyTestApp/0.1.3')
87
- end
88
- end
89
-
90
- context 'when registering an app and not sending platform info' do
91
- subject(:config) { described_class.new(attributes, 'MyTestApp', '0.1.3', false) }
92
-
93
- it 'sends app, but not platform info' do
94
- expect(config.user_agent).to include('deepl-ruby/')
95
- expect(config.user_agent).not_to include('(')
96
- expect(config.user_agent).not_to include(')')
97
- expect(config.user_agent).not_to include(' ruby/')
98
- expect(config.user_agent).to include('MyTestApp/0.1.3')
99
- end
100
- end
101
- end
102
-
103
- describe '#validate!' do
104
- let(:auth_message) { 'auth_key not provided' }
105
-
106
- context 'when providing a valid auth key' do
107
- let(:attributes) { { auth_key: '' } }
108
-
109
- it 'raises an error' do
110
- expect { config.validate! }.to raise_error(DeepL::Exceptions::Error, auth_message)
111
- end
112
- end
113
-
114
- context 'when providing an invalid auth key' do
115
- let(:attributes) { { auth_key: 'not-empty' } }
116
-
117
- it 'does not raise an error' do
118
- expect { config.validate! }.not_to raise_error
119
- end
120
- end
121
- end
122
- end