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.
Files changed (66) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +1 -13
  3. data/.rubocop.yml +8 -4
  4. data/Gemfile +6 -0
  5. data/README.md +133 -25
  6. data/Rakefile +2 -0
  7. data/VERSION +1 -1
  8. data/deepl-rb.gemspec +38 -10
  9. data/lib/deepl/api.rb +2 -0
  10. data/lib/deepl/configuration.rb +4 -2
  11. data/lib/deepl/exceptions/authorization_failed.rb +2 -0
  12. data/lib/deepl/exceptions/bad_request.rb +4 -0
  13. data/lib/deepl/exceptions/error.rb +2 -0
  14. data/lib/deepl/exceptions/limit_exceeded.rb +2 -0
  15. data/lib/deepl/exceptions/not_found.rb +13 -0
  16. data/lib/deepl/exceptions/not_supported.rb +11 -0
  17. data/lib/deepl/exceptions/quota_exceeded.rb +2 -0
  18. data/lib/deepl/exceptions/request_entity_too_large.rb +12 -0
  19. data/lib/deepl/exceptions/request_error.rb +4 -1
  20. data/lib/deepl/glossary_api.rb +35 -0
  21. data/lib/deepl/requests/base.rb +23 -11
  22. data/lib/deepl/requests/glossary/create.rb +45 -0
  23. data/lib/deepl/requests/glossary/destroy.rb +30 -0
  24. data/lib/deepl/requests/glossary/entries.rb +30 -0
  25. data/lib/deepl/requests/glossary/find.rb +31 -0
  26. data/lib/deepl/requests/glossary/language_pairs.rb +31 -0
  27. data/lib/deepl/requests/glossary/list.rb +30 -0
  28. data/lib/deepl/requests/languages.rb +30 -0
  29. data/lib/deepl/requests/translate.rb +8 -2
  30. data/lib/deepl/requests/usage.rb +2 -0
  31. data/lib/deepl/resources/base.rb +2 -0
  32. data/lib/deepl/resources/glossary.rb +25 -0
  33. data/lib/deepl/resources/language.rb +27 -0
  34. data/lib/deepl/resources/language_pair.rb +20 -0
  35. data/lib/deepl/resources/text.rb +2 -0
  36. data/lib/deepl/resources/usage.rb +2 -0
  37. data/lib/deepl/utils/exception_builder.rb +29 -0
  38. data/lib/deepl.rb +28 -0
  39. data/spec/api/api_spec.rb +2 -0
  40. data/spec/api/configuration_spec.rb +4 -2
  41. data/spec/api/deepl_spec.rb +213 -0
  42. data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +374 -0
  43. data/spec/fixtures/vcr_cassettes/deepl_languages.yml +43 -0
  44. data/spec/fixtures/vcr_cassettes/deepl_translate.yml +215 -5
  45. data/spec/fixtures/vcr_cassettes/deepl_usage.yml +12 -7
  46. data/spec/fixtures/vcr_cassettes/glossaries.yml +480 -0
  47. data/spec/fixtures/vcr_cassettes/languages.yml +114 -0
  48. data/spec/fixtures/vcr_cassettes/translate_texts.yml +199 -33
  49. data/spec/fixtures/vcr_cassettes/usage.yml +12 -7
  50. data/spec/requests/glossary/create_spec.rb +55 -0
  51. data/spec/requests/glossary/destroy_spec.rb +50 -0
  52. data/spec/requests/glossary/entries_spec.rb +48 -0
  53. data/spec/requests/glossary/find_spec.rb +54 -0
  54. data/spec/requests/glossary/language_pairs_spec.rb +30 -0
  55. data/spec/requests/glossary/list_spec.rb +44 -0
  56. data/spec/requests/languages_spec.rb +58 -0
  57. data/spec/requests/translate_spec.rb +139 -4
  58. data/spec/requests/usage_spec.rb +2 -0
  59. data/spec/resources/glossary_spec.rb +35 -0
  60. data/spec/resources/language_pair_spec.rb +20 -0
  61. data/spec/resources/language_spec.rb +42 -0
  62. data/spec/resources/text_spec.rb +4 -2
  63. data/spec/resources/usage_spec.rb +5 -3
  64. data/spec/spec_helper.rb +6 -4
  65. metadata +49 -8
  66. data/Gemfile.lock +0 -113
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fedf9315cf7b163148b9eebbbded4f80c93c4ae3
4
- data.tar.gz: aa81c5a04442a74fc5099acb060bbbbd71e9bf00
2
+ SHA256:
3
+ metadata.gz: d0f4d3d2ddeeb7156fd35df3906d1a176d9e66b64c8d936ff2fb7fc3681cb437
4
+ data.tar.gz: 2d15451265aed72177fdf9692e44a5ca5f19750db4c620a009e482a708232ddf
5
5
  SHA512:
6
- metadata.gz: 8ef069fb972692ec56ac2688c85385de934beba10ce3f180fd354e42a14c91568d37dfa9549dc19a3c55824575885ead52d7fbea69fc58322851c2149dd01f0d
7
- data.tar.gz: f92c7383dc6511902f825ba373aaf5c6bf7470a369d9a042dd28bf918409d7b9e80b4c044d50e09522e3c6410e3bae77a7d732b9e590db71022ac7227b4af18a
6
+ metadata.gz: c949370aa698d2c891c8961e17c94b914e7a7f9942584ca2ef8664cad22dcf03169d5512d5caa2d43d3c2ee65a9d19a9eb2231d1b7766eab444430a18d84c75b
7
+ data.tar.gz: aa188f9969cfdbd0b0355d2f58afb828358be8078487e52f2c2ac3bdc518e9a6d5c718c8652461c612a9cf39dba15ff4b157b5335fd3282a570f425bd245ade4
data/.circleci/config.yml CHANGED
@@ -3,26 +3,14 @@ jobs:
3
3
  build:
4
4
  working_directory: ~/deepl-rb
5
5
  docker:
6
- - image: circleci/ruby:2.1
6
+ - image: circleci/ruby:2.7
7
7
  steps:
8
8
  - checkout
9
9
 
10
- # Restore bundle cache
11
- - type: cache-restore
12
- name: Restore bundle cache
13
- key: deepl-rb-bundle-{{ checksum "Gemfile.lock" }}
14
-
15
10
  - run:
16
11
  name: Bundle Install
17
12
  command: bundle install --path vendor/bundle
18
13
 
19
- # Store bundle cache
20
- - type: cache-save
21
- name: Store bundle cache
22
- key: deepl-rb-bundle-{{ checksum "Gemfile.lock" }}
23
- paths:
24
- - vendor/bundle
25
-
26
14
  # Run rspec
27
15
  - type: shell
28
16
  command: |
data/.rubocop.yml CHANGED
@@ -2,15 +2,19 @@
2
2
 
3
3
  AllCops:
4
4
  DisplayCopNames: true
5
+ NewCops: enable
5
6
  Exclude:
6
- - deepl-rb.gemspec
7
- - vendor/**/*
7
+ - deepl-rb.gemspec
8
+ - vendor/**/*
8
9
 
9
10
  Metrics/BlockLength:
10
11
  Exclude:
11
- - "**/*_spec.rb"
12
+ - "**/*_spec.rb"
12
13
 
13
- Metrics/LineLength:
14
+ Metrics/ParameterLists:
15
+ Max: 6
16
+
17
+ Layout/LineLength:
14
18
  Max: 100
15
19
 
16
20
  Style/Documentation:
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  group :development do
@@ -5,6 +7,10 @@ group :development do
5
7
  gem 'juwelier'
6
8
  end
7
9
 
10
+ group :test, :development do
11
+ gem 'byebug', require: 'byebug'
12
+ end
13
+
8
14
  group :test do
9
15
  # Test
10
16
  gem 'codecov', require: false
data/README.md CHANGED
@@ -40,11 +40,42 @@ You can also configure the API host and the API version:
40
40
  ```rb
41
41
  DeepL.configure do |config|
42
42
  config.auth_key = 'your-api-token'
43
- config.host = 'https://test-api.deepl.com' # Default value is 'https://api.deepl.com'
43
+ config.host = 'https://api-free.deepl.com' # Default value is 'https://api.deepl.com'
44
44
  config.version = 'v1' # Default value is 'v2'
45
45
  end
46
46
  ```
47
47
 
48
+ ### Available languages
49
+
50
+ Available languages can be retrieved via API:
51
+
52
+ ```rb
53
+ languages = DeepL.languages
54
+
55
+ puts languages.class
56
+ # => Array
57
+ puts languages.first.class
58
+ # => DeepL::Resources::Language
59
+ puts "#{languages.first.code} -> #{languages.first.name}"
60
+ # => "ES -> Spanish"
61
+ ```
62
+
63
+ Note that source and target languages may be different, which can be retrieved by using the `type`
64
+ option:
65
+
66
+ ```rb
67
+ puts DeepL.languages(type: :source).count
68
+ # => 24
69
+ puts DeepL.languages(type: :target).count
70
+ # => 26
71
+ ```
72
+
73
+ All languages are also defined on the
74
+ [official API documentation](https://www.deepl.com/docs-api/translating-text/).
75
+
76
+ Note that target languages may include the `supports_formality` flag, which may be checked
77
+ using the `DeepL::Resources::Language#supports_formality?`.
78
+
48
79
  ### Translate
49
80
 
50
81
  To translate a simple text, use the `translate` method:
@@ -79,20 +110,6 @@ puts translations.first.class
79
110
  # => DeepL::Resources::Text
80
111
  ```
81
112
 
82
- Here's a list of available language codes:
83
-
84
- | Language code | Language
85
- | --------------- | ---------------
86
- | `EN` | English
87
- | `DE` | German
88
- | `FR` | French
89
- | `ES` | Spanish
90
- | `PT` | Portuguese
91
- | `IT` | Italian
92
- | `NL` | Dutch
93
- | `PL` | Polish
94
- | `RU` | Russian
95
-
96
113
  You can also use custom query parameters, like `tag_handling`, `split_sentences`, `non_splitting_tags` or `ignore_tags`:
97
114
 
98
115
  ```rb
@@ -110,10 +127,108 @@ The following parameters will be automatically converted:
110
127
  | --------------------- | ---------------
111
128
  | `preserve_formatting` | Converts `false` to `'0'` and `true` to `'1'`
112
129
  | `split_sentences` | Converts `false` to `'0'` and `true` to `'1'`
130
+ | `outline_detection` | Converts `false` to `'0'` and `true` to `'1'`
131
+ | `splitting_tags` | Converts arrays to strings joining by commas
113
132
  | `non_splitting_tags` | Converts arrays to strings joining by commas
114
133
  | `ignore_tags` | Converts arrays to strings joining by commas
134
+ | `formality` | No conversion applied
135
+ | `glossary_id` | No conversion applied
136
+
137
+ ### Glossaries
138
+
139
+ To create a glossary, use the `glossaries.create` method. The glossary `entries` argument should be an array of text pairs. Each pair includes the source and the target translations.
140
+
141
+ ```rb
142
+ entries = [
143
+ ['Hello World', 'Hola Tierra'],
144
+ ['car', 'auto']
145
+ ]
146
+ glossary = DeepL.glossaries.create 'Mi Glosario', 'EN', 'ES', entries
147
+
148
+ puts glossary.class
149
+ # => DeepL::Resources::Glossary
150
+ puts glossary.id
151
+ # => 'aa48c7f0-0d02-413e-8a06-d5bbf0ca7a6e'
152
+ puts glossary.entry_count
153
+ # => 2
154
+ ```
155
+
156
+ Created glossaries can be used in the `translate` method by specifying the `glossary_id` option:
157
+
158
+ ```rb
159
+ translation = DeepL.translate 'Hello World', 'EN', 'ES', glossary_id: 'aa48c7f0-0d02-413e-8a06-d5bbf0ca7a6e'
160
+
161
+ puts translation.class
162
+ # => DeepL::Resources::Text
163
+ puts translation.text
164
+ # => 'Hola Tierra'
165
+
166
+ translation = DeepL.translate "I wish we had a car.", 'EN', 'ES', glossary_id: 'aa48c7f0-0d02-413e-8a06-d5bbf0ca7a6e'
167
+
168
+ puts translation.class
169
+ # => DeepL::Resources::Text
170
+ puts translation.text
171
+ # => Ojalá tuviéramos un auto.
172
+ ```
173
+
174
+ To list all the glossaries available, use the `glossaries.list` method:
175
+
176
+ ```rb
177
+ glossaries = DeepL.glossaries.list
178
+
179
+ puts glossaries.class
180
+ # => Array
181
+ puts glossaries.first.class
182
+ # => DeepL::Resources::Glossary
183
+ ```
184
+
185
+ To find an existing glossary, use the `glossaries.find` method:
186
+
187
+ ```rb
188
+ glossary = DeepL.glossaries.find 'aa48c7f0-0d02-413e-8a06-d5bbf0ca7a6e'
189
+
190
+ puts glossary.class
191
+ # => DeepL::Resources::Glossary
192
+ ```
193
+
194
+ The glossary resource does not include the glossary entries. To list the glossary entries, use the `glossaries.entries` method:
195
+
196
+ ```rb
197
+ entries = DeepL.glossaries.entries 'aa48c7f0-0d02-413e-8a06-d5bbf0ca7a6e'
198
+
199
+ puts entries.class
200
+ # => Array
201
+ puts entries.size
202
+ # => 2
203
+ pp entries.first
204
+ # => ["Hello World", "Hola Tierra"]
205
+ ```
206
+
207
+ To delete an existing glossary, use the `glossaries.destroy` method:
208
+
209
+ ```rb
210
+ glossary_id = DeepL.glossaries.destroy 'aa48c7f0-0d02-413e-8a06-d5bbf0ca7a6e'
211
+
212
+ puts glossary_id
213
+ # => aa48c7f0-0d02-413e-8a06-d5bbf0ca7a6e
214
+ ```
215
+
216
+ You can list all the language pairs supported by glossaries using the `glossaries.language_pairs` method:
115
217
 
116
- ### Usage
218
+ ```rb
219
+ language_pairs = DeepL.glossaries.language_pairs
220
+
221
+ puts language_pairs.class
222
+ # => Array
223
+ puts language_pairs.first.class
224
+ # => DeepL::Resources::LanguagePair
225
+ puts language_pairs.first.source_lang
226
+ # => en
227
+ puts language_pairs.first.target_lang
228
+ # => de
229
+ ```
230
+
231
+ ### Monitor usage
117
232
 
118
233
  To check current API usage, use:
119
234
 
@@ -137,6 +252,7 @@ You can capture and process exceptions that may be raised during API calls. Thes
137
252
  | `DeepL::Exceptions::LimitExceeded` | You've reached the API's call limit. |
138
253
  | `DeepL::Exceptions::QuotaExceeded` | You've reached the API's character limit. |
139
254
  | `DeepL::Exceptions::RequestError` | An unkown request error. Check `exception.response` and `exception.request` for more information. |
255
+ | `DeepL::Exceptions::NotSupported` | The requested method or API endpoint is not supported. |
140
256
 
141
257
  An exampling of handling a generic exception:
142
258
 
@@ -171,7 +287,7 @@ Since the DeepL service is defined globally, you can use service anywhere in you
171
287
  ### i18n-tasks
172
288
 
173
289
  You may also take a look at [`i18n-tasks`](https://github.com/glebm/i18n-tasks), which is a gem
174
- that helps you find and manage missing and unused translations. `deepl-rb` is used as one the
290
+ that helps you find and manage missing and unused translations. `deepl-rb` is used as one of the
175
291
  backend services to translate content.
176
292
 
177
293
  ## Development
@@ -189,11 +305,3 @@ To run tests (rspec and rubocop), use
189
305
  ```
190
306
  bundle exec rake test
191
307
  ```
192
-
193
- ## Contributors
194
-
195
- This project has been developed by:
196
-
197
- | Avatar | Name | Nickname | Email |
198
- | ------ | ---- | -------- | ----- |
199
- | ![](http://www.gravatar.com/avatar/2ae6d81e0605177ba9e17b19f54e6b6c.jpg?s=64) | Daniel Herzog | Wikiti | [info@danielherzog.es](mailto:info@danielherzog.es)
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rubygems'
2
4
  require 'bundler'
3
5
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.2.1
1
+ 2.5.3
data/deepl-rb.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: deepl-rb 2.2.1 ruby lib
5
+ # stub: deepl-rb 2.5.3 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "deepl-rb".freeze
9
- s.version = "2.2.1"
9
+ s.version = "2.5.3"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Daniel Herzog".freeze]
14
- s.date = "2020-03-08"
14
+ s.date = "2022-09-26"
15
15
  s.description = "A simple ruby wrapper for the DeepL translation API (v1). For more information, check this: https://www.deepl.com/docs/api-reference.html".freeze
16
16
  s.email = "info@danielherzog.es".freeze
17
17
  s.extra_rdoc_files = [
@@ -22,7 +22,6 @@ Gem::Specification.new do |s|
22
22
  ".circleci/config.yml",
23
23
  ".rubocop.yml",
24
24
  "Gemfile",
25
- "Gemfile.lock",
26
25
  "LICENSE.md",
27
26
  "README.md",
28
27
  "Rakefile",
@@ -35,42 +34,71 @@ Gem::Specification.new do |s|
35
34
  "lib/deepl/exceptions/bad_request.rb",
36
35
  "lib/deepl/exceptions/error.rb",
37
36
  "lib/deepl/exceptions/limit_exceeded.rb",
37
+ "lib/deepl/exceptions/not_found.rb",
38
+ "lib/deepl/exceptions/not_supported.rb",
38
39
  "lib/deepl/exceptions/quota_exceeded.rb",
40
+ "lib/deepl/exceptions/request_entity_too_large.rb",
39
41
  "lib/deepl/exceptions/request_error.rb",
42
+ "lib/deepl/glossary_api.rb",
40
43
  "lib/deepl/requests/base.rb",
44
+ "lib/deepl/requests/glossary/create.rb",
45
+ "lib/deepl/requests/glossary/destroy.rb",
46
+ "lib/deepl/requests/glossary/entries.rb",
47
+ "lib/deepl/requests/glossary/find.rb",
48
+ "lib/deepl/requests/glossary/language_pairs.rb",
49
+ "lib/deepl/requests/glossary/list.rb",
50
+ "lib/deepl/requests/languages.rb",
41
51
  "lib/deepl/requests/translate.rb",
42
52
  "lib/deepl/requests/usage.rb",
43
53
  "lib/deepl/resources/base.rb",
54
+ "lib/deepl/resources/glossary.rb",
55
+ "lib/deepl/resources/language.rb",
56
+ "lib/deepl/resources/language_pair.rb",
44
57
  "lib/deepl/resources/text.rb",
45
58
  "lib/deepl/resources/usage.rb",
59
+ "lib/deepl/utils/exception_builder.rb",
46
60
  "spec/api/api_spec.rb",
47
61
  "spec/api/configuration_spec.rb",
48
62
  "spec/api/deepl_spec.rb",
63
+ "spec/fixtures/vcr_cassettes/deepl_glossaries.yml",
64
+ "spec/fixtures/vcr_cassettes/deepl_languages.yml",
49
65
  "spec/fixtures/vcr_cassettes/deepl_translate.yml",
50
66
  "spec/fixtures/vcr_cassettes/deepl_usage.yml",
67
+ "spec/fixtures/vcr_cassettes/glossaries.yml",
68
+ "spec/fixtures/vcr_cassettes/languages.yml",
51
69
  "spec/fixtures/vcr_cassettes/translate_texts.yml",
52
70
  "spec/fixtures/vcr_cassettes/usage.yml",
71
+ "spec/requests/glossary/create_spec.rb",
72
+ "spec/requests/glossary/destroy_spec.rb",
73
+ "spec/requests/glossary/entries_spec.rb",
74
+ "spec/requests/glossary/find_spec.rb",
75
+ "spec/requests/glossary/language_pairs_spec.rb",
76
+ "spec/requests/glossary/list_spec.rb",
77
+ "spec/requests/languages_spec.rb",
53
78
  "spec/requests/translate_spec.rb",
54
79
  "spec/requests/usage_spec.rb",
80
+ "spec/resources/glossary_spec.rb",
81
+ "spec/resources/language_pair_spec.rb",
82
+ "spec/resources/language_spec.rb",
55
83
  "spec/resources/text_spec.rb",
56
84
  "spec/resources/usage_spec.rb",
57
85
  "spec/spec_helper.rb"
58
86
  ]
59
87
  s.homepage = "http://github.com/wikiti/deepl-rb".freeze
60
88
  s.licenses = ["MIT".freeze]
61
- s.rubygems_version = "2.6.14".freeze
89
+ s.rubygems_version = "3.3.7".freeze
62
90
  s.summary = "A simple ruby wrapper for the DeepL API".freeze
63
91
 
64
92
  if s.respond_to? :specification_version then
65
93
  s.specification_version = 4
94
+ end
66
95
 
67
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
68
- s.add_development_dependency(%q<juwelier>.freeze, [">= 0"])
69
- else
70
- s.add_dependency(%q<juwelier>.freeze, [">= 0"])
71
- end
96
+ if s.respond_to? :add_runtime_dependency then
97
+ s.add_development_dependency(%q<juwelier>.freeze, [">= 0"])
98
+ s.add_development_dependency(%q<byebug>.freeze, [">= 0"])
72
99
  else
73
100
  s.add_dependency(%q<juwelier>.freeze, [">= 0"])
101
+ s.add_dependency(%q<byebug>.freeze, [">= 0"])
74
102
  end
75
103
  end
76
104
 
data/lib/deepl/api.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepL
2
4
  class API
3
5
  attr_reader :configuration
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepL
2
4
  class Configuration
3
5
  ATTRIBUTES = %i[auth_key host version].freeze
@@ -6,7 +8,7 @@ module DeepL
6
8
 
7
9
  def initialize(data = {})
8
10
  data.each { |key, value| send("#{key}=", value) }
9
- @auth_key ||= ENV['DEEPL_AUTH_KEY']
11
+ @auth_key ||= ENV.fetch('DEEPL_AUTH_KEY', nil)
10
12
  @host ||= 'https://api.deepl.com'
11
13
  @version ||= 'v2'
12
14
  end
@@ -16,7 +18,7 @@ module DeepL
16
18
  end
17
19
 
18
20
  def attributes
19
- ATTRIBUTES.map { |attr| [attr, send(attr)] }.to_h
21
+ ATTRIBUTES.to_h { |attr| [attr, send(attr)] }
20
22
  end
21
23
 
22
24
  def ==(other)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepL
2
4
  module Exceptions
3
5
  class AuthorizationFailed < RequestError
@@ -1,8 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepL
2
4
  module Exceptions
3
5
  class BadRequest < RequestError
4
6
  def message
5
7
  JSON.parse(response.body)['message']
8
+ rescue JSON::ParserError
9
+ response.body
6
10
  end
7
11
  end
8
12
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepL
2
4
  module Exceptions
3
5
  class Error < StandardError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepL
2
4
  module Exceptions
3
5
  class LimitExceeded < RequestError
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeepL
4
+ module Exceptions
5
+ class NotFound < RequestError
6
+ def message
7
+ JSON.parse(response.body)['message']
8
+ rescue JSON::ParserError
9
+ response.body
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeepL
4
+ module Exceptions
5
+ class NotSupported < Error
6
+ def initialize(msg = 'The feature is not supported')
7
+ super
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepL
2
4
  module Exceptions
3
5
  class QuotaExceeded < RequestError
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeepL
4
+ module Exceptions
5
+ class RequestEntityTooLarge < RequestError
6
+ def message
7
+ 'The request size has reached the supported limit. ' \
8
+ "Make sure that you're not sending more than 50 text parts."
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,15 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepL
2
4
  module Exceptions
3
5
  class RequestError < Error
4
6
  attr_reader :request, :response
5
7
 
6
8
  def initialize(request, response)
9
+ super()
7
10
  @request = request
8
11
  @response = response
9
12
  end
10
13
 
11
14
  def message
12
- 'Unkown error.'
15
+ 'Unknown error.'
13
16
  end
14
17
  end
15
18
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeepL
4
+ class GlossaryApi
5
+ def initialize(api, options = {})
6
+ @api = api
7
+ @options = options
8
+ end
9
+
10
+ def create(name, source_lang, target_lang, entries, options = {})
11
+ DeepL::Requests::Glossary::Create.new(@api, name, source_lang, target_lang, entries, options)
12
+ .request
13
+ end
14
+
15
+ def destroy(glossary_id, options = {})
16
+ DeepL::Requests::Glossary::Destroy.new(@api, glossary_id, options).request
17
+ end
18
+
19
+ def entries(glossary_id, options = {})
20
+ DeepL::Requests::Glossary::Entries.new(@api, glossary_id, options).request
21
+ end
22
+
23
+ def find(glossary_id, options = {})
24
+ DeepL::Requests::Glossary::Find.new(@api, glossary_id, options).request
25
+ end
26
+
27
+ def language_pairs(options = {})
28
+ DeepL::Requests::Glossary::LanguagePairs.new(@api, options).request
29
+ end
30
+
31
+ def list(options = {})
32
+ DeepL::Requests::Glossary::List.new(@api, options).request
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepL
2
4
  module Requests
3
5
  class Base
@@ -22,6 +24,10 @@ module DeepL
22
24
  options[name.to_s] || options[name.to_sym]
23
25
  end
24
26
 
27
+ def delete_option(name)
28
+ options.delete(name.to_s) || options.delete(name.to_sym)
29
+ end
30
+
25
31
  def set_option(name, value)
26
32
  if options.key?(name.to_sym)
27
33
  options[name.to_sym] = value
@@ -31,8 +37,8 @@ module DeepL
31
37
  end
32
38
 
33
39
  def post(payload)
34
- request = Net::HTTP::Post.new(uri.request_uri)
35
- request.set_form_data(payload.reject { |_, v| v.nil? })
40
+ request = Net::HTTP::Post.new(uri.request_uri, headers)
41
+ request.set_form_data(payload.compact)
36
42
  response = http.request(request)
37
43
 
38
44
  validate_response!(request, response)
@@ -40,7 +46,15 @@ module DeepL
40
46
  end
41
47
 
42
48
  def get
43
- request = Net::HTTP::Get.new(uri.request_uri)
49
+ request = Net::HTTP::Get.new(uri.request_uri, headers)
50
+ response = http.request(request)
51
+
52
+ validate_response!(request, response)
53
+ [request, response]
54
+ end
55
+
56
+ def delete
57
+ request = Net::HTTP::Delete.new(uri.request_uri, headers)
44
58
  response = http.request(request)
45
59
 
46
60
  validate_response!(request, response)
@@ -58,13 +72,7 @@ module DeepL
58
72
  def validate_response!(request, response)
59
73
  return if response.is_a?(Net::HTTPSuccess)
60
74
 
61
- case response.code
62
- when '400' then raise Exceptions::BadRequest.new(request, response)
63
- when '403' then raise Exceptions::AuthorizationFailed.new(request, response)
64
- when '429' then raise Exceptions::LimitExceeded.new(request, response)
65
- when '456' then raise Exceptions::QuotaExceeded.new(request, response)
66
- else raise Exceptions::RequestError.new(request, response)
67
- end
75
+ raise Utils::ExceptionBuilder.new(request, response).build
68
76
  end
69
77
 
70
78
  def path
@@ -89,7 +97,11 @@ module DeepL
89
97
  end
90
98
 
91
99
  def query_params
92
- { auth_key: api.configuration.auth_key }.merge(options)
100
+ options
101
+ end
102
+
103
+ def headers
104
+ { 'Authorization' => "DeepL-Auth-Key #{api.configuration.auth_key}" }
93
105
  end
94
106
  end
95
107
  end