deepl-rb 2.2.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.circleci/config.yml +1 -13
- data/.rubocop.yml +1 -0
- data/Gemfile +6 -0
- data/README.md +55 -24
- data/Rakefile +2 -0
- data/VERSION +1 -1
- data/deepl-rb.gemspec +16 -10
- data/lib/deepl.rb +9 -0
- data/lib/deepl/api.rb +2 -0
- data/lib/deepl/configuration.rb +2 -0
- data/lib/deepl/exceptions/authorization_failed.rb +2 -0
- data/lib/deepl/exceptions/bad_request.rb +4 -0
- data/lib/deepl/exceptions/error.rb +2 -0
- data/lib/deepl/exceptions/limit_exceeded.rb +2 -0
- data/lib/deepl/exceptions/quota_exceeded.rb +11 -0
- data/lib/deepl/exceptions/request_error.rb +4 -1
- data/lib/deepl/requests/base.rb +4 -1
- data/lib/deepl/requests/languages.rb +28 -0
- data/lib/deepl/requests/translate.rb +3 -0
- data/lib/deepl/requests/usage.rb +2 -0
- data/lib/deepl/resources/base.rb +2 -0
- data/lib/deepl/resources/language.rb +20 -0
- data/lib/deepl/resources/text.rb +2 -0
- data/lib/deepl/resources/usage.rb +2 -0
- data/spec/api/api_spec.rb +2 -0
- data/spec/api/configuration_spec.rb +3 -1
- data/spec/api/deepl_spec.rb +24 -0
- data/spec/fixtures/vcr_cassettes/deepl_languages.yml +41 -0
- data/spec/fixtures/vcr_cassettes/deepl_translate.yml +4 -5
- data/spec/fixtures/vcr_cassettes/deepl_usage.yml +10 -7
- data/spec/fixtures/vcr_cassettes/languages.yml +108 -0
- data/spec/fixtures/vcr_cassettes/translate_texts.yml +69 -33
- data/spec/fixtures/vcr_cassettes/usage.yml +10 -7
- data/spec/requests/languages_spec.rb +58 -0
- data/spec/requests/translate_spec.rb +6 -3
- data/spec/requests/usage_spec.rb +2 -0
- data/spec/resources/language_spec.rb +20 -0
- data/spec/resources/text_spec.rb +4 -2
- data/spec/resources/usage_spec.rb +5 -3
- data/spec/spec_helper.rb +3 -1
- metadata +27 -8
- data/Gemfile.lock +0 -113
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f531ff13d2ba139bab65f6027f2e673f6480b097847d898531ecc7a9b247faea
|
4
|
+
data.tar.gz: e12727852eff8153dbb3a95d853d0fbd5ef5c703f02eaf7dedd089320e6873a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d783cba5c207fb772589bc3f328a942919eff214778ea739a6924022bb7589a1898ab62a1cad2b9f6b177f857beb1bd0c5fbe290eba1467c31b26ab9cad461e
|
7
|
+
data.tar.gz: f14387ba9bc6e7ca3b97176fe1d51f4fa001ca9caba1dc6377d13e4c573896f953575f6efa682bbf55d6704657a53eb73686f9ec51711298549b49a2e5a07701
|
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.
|
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
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,39 @@ 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://
|
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
|
+
|
48
76
|
### Translate
|
49
77
|
|
50
78
|
To translate a simple text, use the `translate` method:
|
@@ -79,18 +107,6 @@ puts translations.first.class
|
|
79
107
|
# => DeepL::Resources::Text
|
80
108
|
```
|
81
109
|
|
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
|
-
| `IT` | Italian
|
91
|
-
| `NL` | Dutch
|
92
|
-
| `PL` | Polish
|
93
|
-
|
94
110
|
You can also use custom query parameters, like `tag_handling`, `split_sentences`, `non_splitting_tags` or `ignore_tags`:
|
95
111
|
|
96
112
|
```rb
|
@@ -111,7 +127,7 @@ The following parameters will be automatically converted:
|
|
111
127
|
| `non_splitting_tags` | Converts arrays to strings joining by commas
|
112
128
|
| `ignore_tags` | Converts arrays to strings joining by commas
|
113
129
|
|
114
|
-
###
|
130
|
+
### Monitor usage
|
115
131
|
|
116
132
|
To check current API usage, use:
|
117
133
|
|
@@ -128,11 +144,12 @@ puts usage.character_limit
|
|
128
144
|
|
129
145
|
You can capture and process exceptions that may be raised during API calls. These are all the possible exceptions:
|
130
146
|
|
131
|
-
| Exception class |
|
147
|
+
| Exception class | Description |
|
132
148
|
| --------------- | ----------- |
|
133
|
-
| `DeepL::Exceptions::AuthorizationFailed` | The authorization process has failed. Check your auth_key value. |
|
149
|
+
| `DeepL::Exceptions::AuthorizationFailed` | The authorization process has failed. Check your `auth_key` value. |
|
134
150
|
| `DeepL::Exceptions::BadRequest` | Something is wrong in your request. Check `exception.message` for more information. |
|
135
151
|
| `DeepL::Exceptions::LimitExceeded` | You've reached the API's call limit. |
|
152
|
+
| `DeepL::Exceptions::QuotaExceeded` | You've reached the API's character limit. |
|
136
153
|
| `DeepL::Exceptions::RequestError` | An unkown request error. Check `exception.response` and `exception.request` for more information. |
|
137
154
|
|
138
155
|
An exampling of handling a generic exception:
|
@@ -146,9 +163,31 @@ rescue DeepL::Exceptions::RequestError => e
|
|
146
163
|
puts "Response body: #{e.response.body}"
|
147
164
|
puts "Request body: #{e.request.body}"
|
148
165
|
end
|
166
|
+
```
|
167
|
+
|
168
|
+
## Integrations
|
169
|
+
|
170
|
+
### Ruby on Rails
|
171
|
+
|
172
|
+
You may use this gem as a standalone service by creating an initializer on your
|
173
|
+
`config/initializers` folder with your DeepL configuration. For example:
|
149
174
|
|
175
|
+
```rb
|
176
|
+
# config/initializers/deepl.rb
|
177
|
+
DeepL.configure do |config|
|
178
|
+
# Your configuration goes here
|
179
|
+
end
|
150
180
|
```
|
151
181
|
|
182
|
+
Since the DeepL service is defined globally, you can use service anywhere in your code
|
183
|
+
(controllers, models, views, jobs, plain ruby objects… you name it).
|
184
|
+
|
185
|
+
### i18n-tasks
|
186
|
+
|
187
|
+
You may also take a look at [`i18n-tasks`](https://github.com/glebm/i18n-tasks), which is a gem
|
188
|
+
that helps you find and manage missing and unused translations. `deepl-rb` is used as one of the
|
189
|
+
backend services to translate content.
|
190
|
+
|
152
191
|
## Development
|
153
192
|
|
154
193
|
Clone the repository, and install its dependencies:
|
@@ -164,11 +203,3 @@ To run tests (rspec and rubocop), use
|
|
164
203
|
```
|
165
204
|
bundle exec rake test
|
166
205
|
```
|
167
|
-
|
168
|
-
## Contributors
|
169
|
-
|
170
|
-
This project has been developed by:
|
171
|
-
|
172
|
-
| Avatar | Name | Nickname | Email |
|
173
|
-
| ------ | ---- | -------- | ----- |
|
174
|
-
| ![](http://www.gravatar.com/avatar/2ae6d81e0605177ba9e17b19f54e6b6c.jpg?s=64) | Daniel Herzog | Wikiti | [info@danielherzog.es](mailto:info@danielherzog.es)
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.3.0
|
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.
|
5
|
+
# stub: deepl-rb 2.3.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "deepl-rb".freeze
|
9
|
-
s.version = "2.
|
9
|
+
s.version = "2.3.0"
|
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 = "
|
14
|
+
s.date = "2021-05-17"
|
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,41 +34,48 @@ 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/quota_exceeded.rb",
|
38
38
|
"lib/deepl/exceptions/request_error.rb",
|
39
39
|
"lib/deepl/requests/base.rb",
|
40
|
+
"lib/deepl/requests/languages.rb",
|
40
41
|
"lib/deepl/requests/translate.rb",
|
41
42
|
"lib/deepl/requests/usage.rb",
|
42
43
|
"lib/deepl/resources/base.rb",
|
44
|
+
"lib/deepl/resources/language.rb",
|
43
45
|
"lib/deepl/resources/text.rb",
|
44
46
|
"lib/deepl/resources/usage.rb",
|
45
47
|
"spec/api/api_spec.rb",
|
46
48
|
"spec/api/configuration_spec.rb",
|
47
49
|
"spec/api/deepl_spec.rb",
|
50
|
+
"spec/fixtures/vcr_cassettes/deepl_languages.yml",
|
48
51
|
"spec/fixtures/vcr_cassettes/deepl_translate.yml",
|
49
52
|
"spec/fixtures/vcr_cassettes/deepl_usage.yml",
|
53
|
+
"spec/fixtures/vcr_cassettes/languages.yml",
|
50
54
|
"spec/fixtures/vcr_cassettes/translate_texts.yml",
|
51
55
|
"spec/fixtures/vcr_cassettes/usage.yml",
|
56
|
+
"spec/requests/languages_spec.rb",
|
52
57
|
"spec/requests/translate_spec.rb",
|
53
58
|
"spec/requests/usage_spec.rb",
|
59
|
+
"spec/resources/language_spec.rb",
|
54
60
|
"spec/resources/text_spec.rb",
|
55
61
|
"spec/resources/usage_spec.rb",
|
56
62
|
"spec/spec_helper.rb"
|
57
63
|
]
|
58
64
|
s.homepage = "http://github.com/wikiti/deepl-rb".freeze
|
59
65
|
s.licenses = ["MIT".freeze]
|
60
|
-
s.rubygems_version = "
|
66
|
+
s.rubygems_version = "3.1.2".freeze
|
61
67
|
s.summary = "A simple ruby wrapper for the DeepL API".freeze
|
62
68
|
|
63
69
|
if s.respond_to? :specification_version then
|
64
70
|
s.specification_version = 4
|
71
|
+
end
|
65
72
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
s.add_dependency(%q<juwelier>.freeze, [">= 0"])
|
70
|
-
end
|
73
|
+
if s.respond_to? :add_runtime_dependency then
|
74
|
+
s.add_development_dependency(%q<juwelier>.freeze, [">= 0"])
|
75
|
+
s.add_development_dependency(%q<byebug>.freeze, [">= 0"])
|
71
76
|
else
|
72
77
|
s.add_dependency(%q<juwelier>.freeze, [">= 0"])
|
78
|
+
s.add_dependency(%q<byebug>.freeze, [">= 0"])
|
73
79
|
end
|
74
80
|
end
|
75
81
|
|
data/lib/deepl.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# -- Dependencies
|
2
4
|
require 'json'
|
3
5
|
require 'net/http'
|
@@ -8,14 +10,17 @@ require 'deepl/exceptions/request_error'
|
|
8
10
|
require 'deepl/exceptions/authorization_failed'
|
9
11
|
require 'deepl/exceptions/bad_request'
|
10
12
|
require 'deepl/exceptions/limit_exceeded'
|
13
|
+
require 'deepl/exceptions/quota_exceeded'
|
11
14
|
|
12
15
|
# -- Requests
|
13
16
|
require 'deepl/requests/base'
|
17
|
+
require 'deepl/requests/languages'
|
14
18
|
require 'deepl/requests/translate'
|
15
19
|
require 'deepl/requests/usage'
|
16
20
|
|
17
21
|
# -- Responses and resources
|
18
22
|
require 'deepl/resources/base'
|
23
|
+
require 'deepl/resources/language'
|
19
24
|
require 'deepl/resources/text'
|
20
25
|
require 'deepl/resources/usage'
|
21
26
|
|
@@ -33,6 +38,10 @@ module DeepL
|
|
33
38
|
@api ||= API.new(configuration)
|
34
39
|
end
|
35
40
|
|
41
|
+
def languages(options = {})
|
42
|
+
Requests::Languages.new(api, options).request
|
43
|
+
end
|
44
|
+
|
36
45
|
def translate(text, source_lang, target_lang, options = {})
|
37
46
|
configure if @configuration.nil?
|
38
47
|
Requests::Translate.new(api, text, source_lang, target_lang, options).request
|
data/lib/deepl/api.rb
CHANGED
data/lib/deepl/configuration.rb
CHANGED
@@ -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
|
-
'
|
15
|
+
'Unknown error.'
|
13
16
|
end
|
14
17
|
end
|
15
18
|
end
|
data/lib/deepl/requests/base.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module DeepL
|
2
4
|
module Requests
|
3
5
|
class Base
|
@@ -32,7 +34,7 @@ module DeepL
|
|
32
34
|
|
33
35
|
def post(payload)
|
34
36
|
request = Net::HTTP::Post.new(uri.request_uri)
|
35
|
-
request.set_form_data(payload.
|
37
|
+
request.set_form_data(payload.compact)
|
36
38
|
response = http.request(request)
|
37
39
|
|
38
40
|
validate_response!(request, response)
|
@@ -62,6 +64,7 @@ module DeepL
|
|
62
64
|
when '400' then raise Exceptions::BadRequest.new(request, response)
|
63
65
|
when '403' then raise Exceptions::AuthorizationFailed.new(request, response)
|
64
66
|
when '429' then raise Exceptions::LimitExceeded.new(request, response)
|
67
|
+
when '456' then raise Exceptions::QuotaExceeded.new(request, response)
|
65
68
|
else raise Exceptions::RequestError.new(request, response)
|
66
69
|
end
|
67
70
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DeepL
|
4
|
+
module Requests
|
5
|
+
class Languages < Base
|
6
|
+
def initialize(api, options = {})
|
7
|
+
super(api, options)
|
8
|
+
end
|
9
|
+
|
10
|
+
def request
|
11
|
+
build_languages(*get)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def build_languages(request, response)
|
17
|
+
data = JSON.parse(response.body)
|
18
|
+
data.map do |language|
|
19
|
+
Resources::Language.new(language['language'], language['name'], request, response)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def path
|
24
|
+
'languages'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|