deepl-rb 3.6.0 → 3.6.1

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 (103) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +27 -0
  3. data/.github/workflows/add_issues_to_kanban.yml +16 -0
  4. data/.gitlab-ci.yml +172 -0
  5. data/.rubocop.yml +51 -0
  6. data/CHANGELOG.md +22 -3
  7. data/CODE_OF_CONDUCT.md +132 -0
  8. data/CONTRIBUTING.md +37 -0
  9. data/Gemfile +24 -0
  10. data/Rakefile +56 -0
  11. data/SECURITY.md +58 -0
  12. data/VERSION +1 -0
  13. data/deepl-rb.gemspec +140 -0
  14. data/lib/deepl/api.rb +22 -0
  15. data/lib/deepl/configuration.rb +59 -0
  16. data/lib/deepl/constants/base_constant.rb +18 -0
  17. data/lib/deepl/constants/formality.rb +16 -0
  18. data/lib/deepl/constants/model_type.rb +14 -0
  19. data/lib/deepl/constants/split_sentences.rb +14 -0
  20. data/lib/deepl/constants/tag_handling.rb +13 -0
  21. data/lib/deepl/constants/tone.rb +20 -0
  22. data/lib/deepl/constants/writing_style.rb +20 -0
  23. data/lib/deepl/document_api.rb +121 -0
  24. data/lib/deepl/exceptions/authorization_failed.rb +14 -0
  25. data/lib/deepl/exceptions/bad_request.rb +16 -0
  26. data/lib/deepl/exceptions/document_translation_error.rb +15 -0
  27. data/lib/deepl/exceptions/error.rb +14 -0
  28. data/lib/deepl/exceptions/limit_exceeded.rb +18 -0
  29. data/lib/deepl/exceptions/not_found.rb +16 -0
  30. data/lib/deepl/exceptions/not_supported.rb +14 -0
  31. data/lib/deepl/exceptions/quota_exceeded.rb +14 -0
  32. data/lib/deepl/exceptions/request_entity_too_large.rb +15 -0
  33. data/lib/deepl/exceptions/request_error.rb +21 -0
  34. data/lib/deepl/exceptions/server_error.rb +18 -0
  35. data/lib/deepl/glossary_api.rb +38 -0
  36. data/lib/deepl/requests/base.rb +196 -0
  37. data/lib/deepl/requests/document/download.rb +44 -0
  38. data/lib/deepl/requests/document/get_status.rb +44 -0
  39. data/lib/deepl/requests/document/upload.rb +74 -0
  40. data/lib/deepl/requests/glossary/create.rb +59 -0
  41. data/lib/deepl/requests/glossary/destroy.rb +37 -0
  42. data/lib/deepl/requests/glossary/entries.rb +37 -0
  43. data/lib/deepl/requests/glossary/find.rb +38 -0
  44. data/lib/deepl/requests/glossary/language_pairs.rb +38 -0
  45. data/lib/deepl/requests/glossary/list.rb +37 -0
  46. data/lib/deepl/requests/languages.rb +37 -0
  47. data/lib/deepl/requests/rephrase.rb +55 -0
  48. data/lib/deepl/requests/style_rule/list.rb +59 -0
  49. data/lib/deepl/requests/translate.rb +93 -0
  50. data/lib/deepl/requests/usage.rb +33 -0
  51. data/lib/deepl/resources/base.rb +17 -0
  52. data/lib/deepl/resources/document_handle.rb +57 -0
  53. data/lib/deepl/resources/document_translation_status.rb +54 -0
  54. data/lib/deepl/resources/glossary.rb +28 -0
  55. data/lib/deepl/resources/language.rb +30 -0
  56. data/lib/deepl/resources/language_pair.rb +23 -0
  57. data/lib/deepl/resources/style_rule.rb +85 -0
  58. data/lib/deepl/resources/text.rb +24 -0
  59. data/lib/deepl/resources/usage.rb +27 -0
  60. data/lib/deepl/style_rule_api.rb +17 -0
  61. data/lib/deepl/utils/backoff_timer.rb +46 -0
  62. data/lib/deepl/utils/exception_builder.rb +34 -0
  63. data/lib/deepl.rb +158 -0
  64. data/lib/http_client_options.rb +22 -0
  65. data/lib/version.rb +8 -0
  66. data/spec/api/api_spec.rb +20 -0
  67. data/spec/api/configuration_spec.rb +122 -0
  68. data/spec/api/deepl_spec.rb +460 -0
  69. data/spec/constants/constants_spec.rb +158 -0
  70. data/spec/fixtures/vcr_cassettes/deepl_document.yml +95 -0
  71. data/spec/fixtures/vcr_cassettes/deepl_document_download.yml +1214 -0
  72. data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +1163 -0
  73. data/spec/fixtures/vcr_cassettes/deepl_languages.yml +54 -0
  74. data/spec/fixtures/vcr_cassettes/deepl_rephrase.yml +87 -0
  75. data/spec/fixtures/vcr_cassettes/deepl_translate.yml +358 -0
  76. data/spec/fixtures/vcr_cassettes/deepl_usage.yml +129 -0
  77. data/spec/fixtures/vcr_cassettes/glossaries.yml +1702 -0
  78. data/spec/fixtures/vcr_cassettes/languages.yml +229 -0
  79. data/spec/fixtures/vcr_cassettes/rephrase_texts.yml +401 -0
  80. data/spec/fixtures/vcr_cassettes/style_rules.yml +92 -0
  81. data/spec/fixtures/vcr_cassettes/translate_texts.yml +10630 -0
  82. data/spec/fixtures/vcr_cassettes/usage.yml +171 -0
  83. data/spec/integration_tests/document_api_spec.rb +178 -0
  84. data/spec/integration_tests/integration_test_utils.rb +170 -0
  85. data/spec/integration_tests/style_rule_api_spec.rb +79 -0
  86. data/spec/requests/extra_body_parameters_types_spec.rb +82 -0
  87. data/spec/requests/glossary/create_spec.rb +65 -0
  88. data/spec/requests/glossary/destroy_spec.rb +66 -0
  89. data/spec/requests/glossary/entries_spec.rb +62 -0
  90. data/spec/requests/glossary/find_spec.rb +68 -0
  91. data/spec/requests/glossary/language_pairs_spec.rb +40 -0
  92. data/spec/requests/glossary/list_spec.rb +54 -0
  93. data/spec/requests/languages_spec.rb +68 -0
  94. data/spec/requests/rephrase_spec.rb +172 -0
  95. data/spec/requests/translate_spec.rb +493 -0
  96. data/spec/requests/usage_spec.rb +43 -0
  97. data/spec/resources/glossary_spec.rb +38 -0
  98. data/spec/resources/language_pair_spec.rb +23 -0
  99. data/spec/resources/language_spec.rb +45 -0
  100. data/spec/resources/text_spec.rb +23 -0
  101. data/spec/resources/usage_spec.rb +35 -0
  102. data/spec/spec_helper.rb +92 -0
  103. metadata +102 -2
data/lib/deepl.rb ADDED
@@ -0,0 +1,158 @@
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/style_rule/list'
35
+ require 'deepl/requests/languages'
36
+ require 'deepl/requests/translate'
37
+ require 'deepl/requests/usage'
38
+ require 'deepl/requests/rephrase'
39
+
40
+ # -- Responses and resources
41
+ require 'deepl/resources/base'
42
+ require 'deepl/resources/document_handle'
43
+ require 'deepl/resources/document_translation_status'
44
+ require 'deepl/resources/glossary'
45
+ require 'deepl/resources/style_rule'
46
+ require 'deepl/resources/language'
47
+ require 'deepl/resources/language_pair'
48
+ require 'deepl/resources/text'
49
+ require 'deepl/resources/usage'
50
+
51
+ # -- Utils
52
+ require 'deepl/utils/exception_builder'
53
+ require 'deepl/utils/backoff_timer'
54
+
55
+ # -- Constants
56
+ require 'deepl/constants/base_constant'
57
+ require 'deepl/constants/formality'
58
+ require 'deepl/constants/model_type'
59
+ require 'deepl/constants/split_sentences'
60
+ require 'deepl/constants/tag_handling'
61
+ require 'deepl/constants/tone'
62
+ require 'deepl/constants/writing_style'
63
+
64
+ # -- HTTP Utils
65
+ require 'http_client_options'
66
+
67
+ # -- Version
68
+ require 'version'
69
+
70
+ # -- Other wrappers
71
+ require 'deepl/api'
72
+ require 'deepl/configuration'
73
+ require 'deepl/document_api'
74
+ require 'deepl/glossary_api'
75
+ require 'deepl/style_rule_api'
76
+
77
+ # -- Gem interface
78
+ module DeepL
79
+ extend self
80
+
81
+ ## -- API shortcuts
82
+
83
+ def api
84
+ @api ||= API.new(configuration)
85
+ end
86
+
87
+ def languages(options = {})
88
+ Requests::Languages.new(api, options).request
89
+ end
90
+
91
+ def translate(text, source_lang, target_lang, options = {})
92
+ configure if @configuration.nil?
93
+ Requests::Translate.new(api, text, source_lang, target_lang, options).request
94
+ end
95
+
96
+ def document(options = {})
97
+ configure if @configuration.nil?
98
+ DocumentApi.new(api, options)
99
+ end
100
+
101
+ def glossaries(options = {})
102
+ configure if @configuration.nil?
103
+ GlossaryApi.new(api, options)
104
+ end
105
+
106
+ def style_rules(options = {})
107
+ configure if @configuration.nil?
108
+ StyleRuleApi.new(api, options)
109
+ end
110
+
111
+ def rephrase(text, target_lang = nil, writing_style = nil, tone = nil, options = {}) # rubocop:disable Metrics/ParameterLists
112
+ configure if @configuration.nil?
113
+ Requests::Rephrase.new(api, text, target_lang, writing_style, tone, options).request
114
+ end
115
+
116
+ def usage(options = {})
117
+ configure if @configuration.nil?
118
+ Requests::Usage.new(api, options).request
119
+ end
120
+
121
+ def http_client
122
+ @http_client
123
+ end
124
+
125
+ def with_session(client_options = HTTPClientOptions.new()) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
126
+ 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
127
+
128
+ has_proxy = client_options.proxy.key?('proxy_addr') and client_options.proxy.key?('proxy_port')
129
+ begin
130
+ uri = URI(configuration.host)
131
+ http = Net::HTTP.new(uri.host, uri.port, has_proxy ? client_options.proxy['proxy_addr'] : nil,
132
+ has_proxy ? client_options.proxy['proxy_port'] : nil)
133
+ http.use_ssl = client_options.enable_ssl_verification
134
+ http.ca_file = client_options.cert_path if client_options.cert_path
135
+ http.open_timeout = client_options.open_timeout unless client_options.open_timeout.nil?
136
+ http.read_timeout = client_options.read_timeout unless client_options.read_timeout.nil?
137
+ http.write_timeout = client_options.write_timeout unless client_options.write_timeout.nil?
138
+ http.ssl_timeout = client_options.ssl_timeout unless client_options.ssl_timeout.nil?
139
+ http.start
140
+ @http_client = http
141
+ api.update_http_client(http)
142
+ yield
143
+ ensure
144
+ http.finish
145
+ end
146
+ end
147
+
148
+ # -- Configuration
149
+
150
+ def configuration
151
+ @configuration ||= Configuration.new
152
+ end
153
+
154
+ def configure
155
+ yield configuration if block_given?
156
+ configuration.validate!
157
+ end
158
+ end
@@ -0,0 +1,22 @@
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/lib/version.rb ADDED
@@ -0,0 +1,8 @@
1
+ # Copyright 2025 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.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ VERSION = '3.6.1'
8
+ end
@@ -0,0 +1,20 @@
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
@@ -0,0 +1,122 @@
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