deepl-rb 1.0.1 → 2.0.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.
- checksums.yaml +4 -4
- data/README.md +24 -3
- data/VERSION +1 -1
- data/deepl-rb.gemspec +12 -6
- data/lib/deepl.rb +9 -2
- data/lib/deepl/requests/base.rb +20 -0
- data/lib/deepl/requests/{translate_text.rb → translate.rb} +15 -1
- data/lib/deepl/requests/usage.rb +24 -0
- data/lib/deepl/resources/usage.rb +22 -0
- data/spec/api/deepl_spec.rb +19 -1
- data/spec/fixtures/vcr_cassettes/deepl_translate.yml +2 -109
- data/spec/fixtures/vcr_cassettes/deepl_usage.yml +34 -0
- data/spec/fixtures/vcr_cassettes/translate_texts.yml +45 -46
- data/spec/fixtures/vcr_cassettes/usage.yml +34 -0
- data/spec/requests/{translate_text_spec.rb → translate_spec.rb} +47 -3
- data/spec/requests/usage_spec.rb +31 -0
- data/spec/resources/usage_spec.rb +30 -0
- metadata +11 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13a3bbfea258695f87a655395197001cef339a23
|
4
|
+
data.tar.gz: 683f5b706dee4a5b8f23d1c02b8a081ec452e46e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc8cb5c628c1c146f3f29c84425205175a714301a8978db7f0c1d3559503f8d3206855502055b02021bdbe90cdcddeab9b28088c4599178e5b4271c1a539ddfe
|
7
|
+
data.tar.gz: e9daaaed2529971d5cd5813ff593a99a4950f559d280dee881b7fd7fc356b7fc0cfbc88a25978414e46cda54872f0180d23b848df49c7106f3867d45af1b6e19
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# DeepL for ruby
|
4
4
|
|
5
|
-
A simple ruby wrapper for the [DeepL translation API (v1)](https://www.deepl.com/
|
5
|
+
A simple ruby wrapper for the [DeepL translation API (v1)](https://www.deepl.com/api.html).
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -90,15 +90,36 @@ Here's a list of available language codes:
|
|
90
90
|
| `NL` | Dutch
|
91
91
|
| `PL` | Polish
|
92
92
|
|
93
|
-
You can also use custom query parameters, like `tag_handling`:
|
93
|
+
You can also use custom query parameters, like `tag_handling` or `split_sentences`:
|
94
94
|
|
95
95
|
```rb
|
96
|
-
translation = DeepL.translate '<p>A sample</p>', 'EN', 'ES',
|
96
|
+
translation = DeepL.translate '<p>A sample</p>', 'EN', 'ES',
|
97
|
+
tag_handling: 'xml', split_sentences: false
|
97
98
|
|
98
99
|
puts translation.text
|
99
100
|
# => "<p>Una muestra</p>"
|
100
101
|
```
|
101
102
|
|
103
|
+
The following parameters will be automatically converted:
|
104
|
+
|
105
|
+
| Parameter | Conversion
|
106
|
+
| --------------------- | ---------------
|
107
|
+
| `preserve_formatting` | Convertes `false` to `'0'` and `true` to `'1'`
|
108
|
+
| `split_sentences` | Convertes `false` to `'0'` and `true` to `'1'`
|
109
|
+
|
110
|
+
### Usage
|
111
|
+
|
112
|
+
To check current API usage, use:
|
113
|
+
|
114
|
+
```rb
|
115
|
+
usage = DeepL.usage
|
116
|
+
|
117
|
+
puts usage.character_count
|
118
|
+
# => 180118
|
119
|
+
puts usage.character_limit
|
120
|
+
# => 1250000
|
121
|
+
```
|
122
|
+
|
102
123
|
### Handle exceptions
|
103
124
|
|
104
125
|
You can capture and process exceptions that may be raised during API calls. These are all the possible exceptions:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.0.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
|
5
|
+
# stub: deepl-rb 2.0.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "deepl-rb".freeze
|
9
|
-
s.version = "
|
9
|
+
s.version = "2.0.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 = "2018-
|
14
|
+
s.date = "2018-05-08"
|
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 = [
|
@@ -37,21 +37,27 @@ Gem::Specification.new do |s|
|
|
37
37
|
"lib/deepl/exceptions/limit_exceeded.rb",
|
38
38
|
"lib/deepl/exceptions/request_error.rb",
|
39
39
|
"lib/deepl/requests/base.rb",
|
40
|
-
"lib/deepl/requests/
|
40
|
+
"lib/deepl/requests/translate.rb",
|
41
|
+
"lib/deepl/requests/usage.rb",
|
41
42
|
"lib/deepl/resources/base.rb",
|
42
43
|
"lib/deepl/resources/text.rb",
|
44
|
+
"lib/deepl/resources/usage.rb",
|
43
45
|
"spec/api/api_spec.rb",
|
44
46
|
"spec/api/configuration_spec.rb",
|
45
47
|
"spec/api/deepl_spec.rb",
|
46
48
|
"spec/fixtures/vcr_cassettes/deepl_translate.yml",
|
49
|
+
"spec/fixtures/vcr_cassettes/deepl_usage.yml",
|
47
50
|
"spec/fixtures/vcr_cassettes/translate_texts.yml",
|
48
|
-
"spec/
|
51
|
+
"spec/fixtures/vcr_cassettes/usage.yml",
|
52
|
+
"spec/requests/translate_spec.rb",
|
53
|
+
"spec/requests/usage_spec.rb",
|
49
54
|
"spec/resources/text_spec.rb",
|
55
|
+
"spec/resources/usage_spec.rb",
|
50
56
|
"spec/spec_helper.rb"
|
51
57
|
]
|
52
58
|
s.homepage = "http://github.com/wikiti/deepl-rb".freeze
|
53
59
|
s.licenses = ["MIT".freeze]
|
54
|
-
s.rubygems_version = "2.6.
|
60
|
+
s.rubygems_version = "2.6.14".freeze
|
55
61
|
s.summary = "A simple ruby wrapper for the DeepL API".freeze
|
56
62
|
|
57
63
|
if s.respond_to? :specification_version then
|
data/lib/deepl.rb
CHANGED
@@ -11,11 +11,13 @@ require 'deepl/exceptions/limit_exceeded'
|
|
11
11
|
|
12
12
|
# -- Requests
|
13
13
|
require 'deepl/requests/base'
|
14
|
-
require 'deepl/requests/
|
14
|
+
require 'deepl/requests/translate'
|
15
|
+
require 'deepl/requests/usage'
|
15
16
|
|
16
17
|
# -- Responses and resources
|
17
18
|
require 'deepl/resources/base'
|
18
19
|
require 'deepl/resources/text'
|
20
|
+
require 'deepl/resources/usage'
|
19
21
|
|
20
22
|
# -- Other wrappers
|
21
23
|
require 'deepl/api'
|
@@ -33,7 +35,12 @@ module DeepL
|
|
33
35
|
|
34
36
|
def translate(text, source_lang, target_lang, options = {})
|
35
37
|
configure if @configuration.nil?
|
36
|
-
Requests::
|
38
|
+
Requests::Translate.new(api, text, source_lang, target_lang, options).request
|
39
|
+
end
|
40
|
+
|
41
|
+
def usage(options = {})
|
42
|
+
configure if @configuration.nil?
|
43
|
+
Requests::Usage.new(api, options).request
|
37
44
|
end
|
38
45
|
|
39
46
|
# -- Configuration
|
data/lib/deepl/requests/base.rb
CHANGED
@@ -16,10 +16,22 @@ module DeepL
|
|
16
16
|
|
17
17
|
private
|
18
18
|
|
19
|
+
def option?(name)
|
20
|
+
options.key?(name.to_s) || options.key?(name.to_sym)
|
21
|
+
end
|
22
|
+
|
19
23
|
def option(name)
|
20
24
|
options[name.to_s] || options[name.to_sym]
|
21
25
|
end
|
22
26
|
|
27
|
+
def set_option(name, value)
|
28
|
+
if options.key?(name.to_sym)
|
29
|
+
options[name.to_sym] = value
|
30
|
+
else
|
31
|
+
options[name.to_s] = value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
23
35
|
def post(payload)
|
24
36
|
request = Net::HTTP::Post.new(uri.request_uri)
|
25
37
|
request.set_form_data(payload.reject { |_, v| v.nil? })
|
@@ -29,6 +41,14 @@ module DeepL
|
|
29
41
|
[request, response]
|
30
42
|
end
|
31
43
|
|
44
|
+
def get
|
45
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
46
|
+
response = http.request(request)
|
47
|
+
|
48
|
+
validate_response!(request, response)
|
49
|
+
[request, response]
|
50
|
+
end
|
51
|
+
|
32
52
|
def http
|
33
53
|
@http ||= begin
|
34
54
|
http = Net::HTTP.new(uri.host, uri.port)
|
@@ -1,6 +1,11 @@
|
|
1
1
|
module DeepL
|
2
2
|
module Requests
|
3
|
-
class
|
3
|
+
class Translate < Base
|
4
|
+
OPTIONS_CONVERSIONS = {
|
5
|
+
split_sentences: { true => '1', false => '0' },
|
6
|
+
preserve_formatting: { true => '1', false => '0' }
|
7
|
+
}.freeze
|
8
|
+
|
4
9
|
attr_reader :text, :source_lang, :target_lang
|
5
10
|
|
6
11
|
def initialize(api, text, source_lang, target_lang, options = {})
|
@@ -8,6 +13,8 @@ module DeepL
|
|
8
13
|
@text = text
|
9
14
|
@source_lang = source_lang
|
10
15
|
@target_lang = target_lang
|
16
|
+
|
17
|
+
tweak_parameters!
|
11
18
|
end
|
12
19
|
|
13
20
|
def request
|
@@ -17,6 +24,13 @@ module DeepL
|
|
17
24
|
|
18
25
|
private
|
19
26
|
|
27
|
+
def tweak_parameters!
|
28
|
+
OPTIONS_CONVERSIONS.each do |param, converter|
|
29
|
+
next unless option?(param) && converter[option(param)]
|
30
|
+
set_option(param, converter[option(param)])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
20
34
|
def build_texts(request, response)
|
21
35
|
data = JSON.parse(response.body)
|
22
36
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module DeepL
|
2
|
+
module Requests
|
3
|
+
class Usage < Base
|
4
|
+
def initialize(api, options = {})
|
5
|
+
super(api, options)
|
6
|
+
end
|
7
|
+
|
8
|
+
def request
|
9
|
+
build_usage(*get)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def build_usage(request, response)
|
15
|
+
data = JSON.parse(response.body)
|
16
|
+
Resources::Usage.new(data['character_count'], data['character_limit'], request, response)
|
17
|
+
end
|
18
|
+
|
19
|
+
def path
|
20
|
+
'usage'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module DeepL
|
2
|
+
module Resources
|
3
|
+
class Usage < Base
|
4
|
+
attr_reader :character_count, :character_limit
|
5
|
+
|
6
|
+
def initialize(character_count, character_limit, *args)
|
7
|
+
super(*args)
|
8
|
+
|
9
|
+
@character_count = character_count
|
10
|
+
@character_limit = character_limit
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
"#{character_count} / #{character_limit}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def quota_exceeded?
|
18
|
+
character_count >= character_limit
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/spec/api/deepl_spec.rb
CHANGED
@@ -49,7 +49,7 @@ describe DeepL do
|
|
49
49
|
|
50
50
|
context 'When translating a text' do
|
51
51
|
it 'should create and call a request object' do
|
52
|
-
expect(DeepL::Requests::
|
52
|
+
expect(DeepL::Requests::Translate).to receive(:new)
|
53
53
|
.with(subject.api, input, source_lang, target_lang, options).and_call_original
|
54
54
|
|
55
55
|
text = subject.translate(input, source_lang, target_lang, options)
|
@@ -57,4 +57,22 @@ describe DeepL do
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
60
|
+
|
61
|
+
describe '#usage' do
|
62
|
+
let(:options) { {} }
|
63
|
+
|
64
|
+
around do |example|
|
65
|
+
VCR.use_cassette('deepl_usage') { example.call }
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'When checking usage' do
|
69
|
+
it 'should create and call a request object' do
|
70
|
+
expect(DeepL::Requests::Usage).to receive(:new)
|
71
|
+
.with(subject.api, options).and_call_original
|
72
|
+
|
73
|
+
usage = subject.usage(options)
|
74
|
+
expect(usage).to be_a(DeepL::Resources::Usage)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
60
78
|
end
|
@@ -1,112 +1,5 @@
|
|
1
1
|
---
|
2
2
|
http_interactions:
|
3
|
-
- request:
|
4
|
-
method: post
|
5
|
-
uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
|
6
|
-
body:
|
7
|
-
encoding: US-ASCII
|
8
|
-
string: source_lang=EN&target_lang=ES
|
9
|
-
headers:
|
10
|
-
Accept-Encoding:
|
11
|
-
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
12
|
-
Accept:
|
13
|
-
- "*/*"
|
14
|
-
User-Agent:
|
15
|
-
- Ruby
|
16
|
-
Content-Type:
|
17
|
-
- application/x-www-form-urlencoded
|
18
|
-
response:
|
19
|
-
status:
|
20
|
-
code: 400
|
21
|
-
message: Bad Request
|
22
|
-
headers:
|
23
|
-
Server:
|
24
|
-
- nginx
|
25
|
-
Date:
|
26
|
-
- Sun, 10 Dec 2017 21:20:25 GMT
|
27
|
-
Content-Length:
|
28
|
-
- '45'
|
29
|
-
Connection:
|
30
|
-
- keep-alive
|
31
|
-
body:
|
32
|
-
encoding: UTF-8
|
33
|
-
string: '{"message":"Parameter ''text'' not specified."}'
|
34
|
-
http_version:
|
35
|
-
recorded_at: Sun, 10 Dec 2017 21:20:25 GMT
|
36
|
-
- request:
|
37
|
-
method: post
|
38
|
-
uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
|
39
|
-
body:
|
40
|
-
encoding: US-ASCII
|
41
|
-
string: text=Abc&source_lang=EN&target_lang=ES
|
42
|
-
headers:
|
43
|
-
Accept-Encoding:
|
44
|
-
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
45
|
-
Accept:
|
46
|
-
- "*/*"
|
47
|
-
User-Agent:
|
48
|
-
- Ruby
|
49
|
-
Content-Type:
|
50
|
-
- application/x-www-form-urlencoded
|
51
|
-
response:
|
52
|
-
status:
|
53
|
-
code: 200
|
54
|
-
message: OK
|
55
|
-
headers:
|
56
|
-
Server:
|
57
|
-
- nginx
|
58
|
-
Date:
|
59
|
-
- Sun, 10 Dec 2017 21:21:23 GMT
|
60
|
-
Content-Type:
|
61
|
-
- application/json
|
62
|
-
Content-Length:
|
63
|
-
- '65'
|
64
|
-
Connection:
|
65
|
-
- keep-alive
|
66
|
-
Access-Control-Allow-Origin:
|
67
|
-
- "*"
|
68
|
-
body:
|
69
|
-
encoding: UTF-8
|
70
|
-
string: '{"translations":[{"detected_source_language":"EN","text":"ABC"}]}'
|
71
|
-
http_version:
|
72
|
-
recorded_at: Sun, 10 Dec 2017 21:21:23 GMT
|
73
|
-
- request:
|
74
|
-
method: post
|
75
|
-
uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
|
76
|
-
body:
|
77
|
-
encoding: US-ASCII
|
78
|
-
string: text=Sample&source_lang=EN&target_lang=ES
|
79
|
-
headers:
|
80
|
-
Accept-Encoding:
|
81
|
-
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
82
|
-
Accept:
|
83
|
-
- "*/*"
|
84
|
-
User-Agent:
|
85
|
-
- Ruby
|
86
|
-
Content-Type:
|
87
|
-
- application/x-www-form-urlencoded
|
88
|
-
response:
|
89
|
-
status:
|
90
|
-
code: 200
|
91
|
-
message: OK
|
92
|
-
headers:
|
93
|
-
Server:
|
94
|
-
- nginx
|
95
|
-
Date:
|
96
|
-
- Sun, 10 Dec 2017 21:22:11 GMT
|
97
|
-
Content-Type:
|
98
|
-
- application/json
|
99
|
-
Content-Length:
|
100
|
-
- '69'
|
101
|
-
Connection:
|
102
|
-
- keep-alive
|
103
|
-
Access-Control-Allow-Origin:
|
104
|
-
- "*"
|
105
|
-
body:
|
106
|
-
encoding: UTF-8
|
107
|
-
string: '{"translations":[{"detected_source_language":"EN","text":"Muestra"}]}'
|
108
|
-
http_version:
|
109
|
-
recorded_at: Sun, 10 Dec 2017 21:22:11 GMT
|
110
3
|
- request:
|
111
4
|
method: post
|
112
5
|
uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN¶m=fake
|
@@ -130,7 +23,7 @@ http_interactions:
|
|
130
23
|
Server:
|
131
24
|
- nginx
|
132
25
|
Date:
|
133
|
-
-
|
26
|
+
- Tue, 08 May 2018 16:31:34 GMT
|
134
27
|
Content-Type:
|
135
28
|
- application/json
|
136
29
|
Content-Length:
|
@@ -143,5 +36,5 @@ http_interactions:
|
|
143
36
|
encoding: UTF-8
|
144
37
|
string: '{"translations":[{"detected_source_language":"EN","text":"Muestra"}]}'
|
145
38
|
http_version:
|
146
|
-
recorded_at:
|
39
|
+
recorded_at: Tue, 08 May 2018 16:31:33 GMT
|
147
40
|
recorded_with: VCR 4.0.0
|
@@ -0,0 +1,34 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: https://api.deepl.com/v1/usage?auth_key=VALID_TOKEN
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
Accept-Encoding:
|
11
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
12
|
+
Accept:
|
13
|
+
- "*/*"
|
14
|
+
User-Agent:
|
15
|
+
- Ruby
|
16
|
+
response:
|
17
|
+
status:
|
18
|
+
code: 200
|
19
|
+
message: OK
|
20
|
+
headers:
|
21
|
+
Server:
|
22
|
+
- nginx
|
23
|
+
Date:
|
24
|
+
- Tue, 08 May 2018 16:31:35 GMT
|
25
|
+
Content-Length:
|
26
|
+
- '52'
|
27
|
+
Connection:
|
28
|
+
- keep-alive
|
29
|
+
body:
|
30
|
+
encoding: UTF-8
|
31
|
+
string: '{"character_count":802127,"character_limit":1000000}'
|
32
|
+
http_version:
|
33
|
+
recorded_at: Tue, 08 May 2018 16:31:33 GMT
|
34
|
+
recorded_with: VCR 4.0.0
|
@@ -23,21 +23,20 @@ http_interactions:
|
|
23
23
|
Server:
|
24
24
|
- nginx
|
25
25
|
Date:
|
26
|
-
-
|
26
|
+
- Tue, 08 May 2018 16:31:36 GMT
|
27
27
|
Content-Type:
|
28
28
|
- application/json
|
29
29
|
Content-Length:
|
30
|
-
- '
|
30
|
+
- '74'
|
31
31
|
Connection:
|
32
32
|
- keep-alive
|
33
33
|
Access-Control-Allow-Origin:
|
34
34
|
- "*"
|
35
35
|
body:
|
36
36
|
encoding: UTF-8
|
37
|
-
string: '{"translations":[{"detected_source_language":"EN","text":"Texto de
|
38
|
-
|
39
|
-
|
40
|
-
recorded_at: Sun, 10 Dec 2017 20:34:26 GMT
|
37
|
+
string: '{"translations":[{"detected_source_language":"EN","text":"Texto de muestra"}]}'
|
38
|
+
http_version:
|
39
|
+
recorded_at: Tue, 08 May 2018 16:31:34 GMT
|
41
40
|
- request:
|
42
41
|
method: post
|
43
42
|
uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
|
@@ -61,7 +60,7 @@ http_interactions:
|
|
61
60
|
Server:
|
62
61
|
- nginx
|
63
62
|
Date:
|
64
|
-
-
|
63
|
+
- Tue, 08 May 2018 16:31:37 GMT
|
65
64
|
Content-Type:
|
66
65
|
- application/json
|
67
66
|
Content-Length:
|
@@ -73,14 +72,14 @@ http_interactions:
|
|
73
72
|
body:
|
74
73
|
encoding: UTF-8
|
75
74
|
string: '{"translations":[{"detected_source_language":"EN","text":"Muestra"},{"detected_source_language":"EN","text":"Palabra"}]}'
|
76
|
-
http_version:
|
77
|
-
recorded_at:
|
75
|
+
http_version:
|
76
|
+
recorded_at: Tue, 08 May 2018 16:31:35 GMT
|
78
77
|
- request:
|
79
78
|
method: post
|
80
|
-
uri: https://api.deepl.com/v1/translate?auth_key=
|
79
|
+
uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
|
81
80
|
body:
|
82
81
|
encoding: US-ASCII
|
83
|
-
string: text
|
82
|
+
string: text=%3Cp%3ESample+text%3C%2Fp%3E&source_lang=EN&target_lang=ES
|
84
83
|
headers:
|
85
84
|
Accept-Encoding:
|
86
85
|
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
@@ -92,28 +91,33 @@ http_interactions:
|
|
92
91
|
- application/x-www-form-urlencoded
|
93
92
|
response:
|
94
93
|
status:
|
95
|
-
code:
|
96
|
-
message:
|
94
|
+
code: 200
|
95
|
+
message: OK
|
97
96
|
headers:
|
98
97
|
Server:
|
99
98
|
- nginx
|
100
99
|
Date:
|
101
|
-
-
|
100
|
+
- Tue, 08 May 2018 16:31:37 GMT
|
101
|
+
Content-Type:
|
102
|
+
- application/json
|
102
103
|
Content-Length:
|
103
|
-
- '
|
104
|
+
- '85'
|
104
105
|
Connection:
|
105
106
|
- keep-alive
|
107
|
+
Access-Control-Allow-Origin:
|
108
|
+
- "*"
|
106
109
|
body:
|
107
110
|
encoding: UTF-8
|
108
|
-
string: '
|
109
|
-
|
110
|
-
|
111
|
+
string: '{"translations":[{"detected_source_language":"EN","text":"<p>Texto
|
112
|
+
de muestra</p>"}]}'
|
113
|
+
http_version:
|
114
|
+
recorded_at: Tue, 08 May 2018 16:31:36 GMT
|
111
115
|
- request:
|
112
116
|
method: post
|
113
|
-
uri: https://api.deepl.com/v1/translate?auth_key=
|
117
|
+
uri: https://api.deepl.com/v1/translate?auth_key=invalid
|
114
118
|
body:
|
115
119
|
encoding: US-ASCII
|
116
|
-
string: source_lang=EN&target_lang=ES
|
120
|
+
string: text=Sample+text&source_lang=EN&target_lang=ES
|
117
121
|
headers:
|
118
122
|
Accept-Encoding:
|
119
123
|
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
@@ -125,28 +129,28 @@ http_interactions:
|
|
125
129
|
- application/x-www-form-urlencoded
|
126
130
|
response:
|
127
131
|
status:
|
128
|
-
code:
|
129
|
-
message:
|
132
|
+
code: 403
|
133
|
+
message: Forbidden
|
130
134
|
headers:
|
131
135
|
Server:
|
132
136
|
- nginx
|
133
137
|
Date:
|
134
|
-
-
|
138
|
+
- Tue, 08 May 2018 16:31:38 GMT
|
135
139
|
Content-Length:
|
136
|
-
- '
|
140
|
+
- '0'
|
137
141
|
Connection:
|
138
142
|
- keep-alive
|
139
143
|
body:
|
140
144
|
encoding: UTF-8
|
141
|
-
string: '
|
142
|
-
http_version:
|
143
|
-
recorded_at:
|
145
|
+
string: ''
|
146
|
+
http_version:
|
147
|
+
recorded_at: Tue, 08 May 2018 16:31:37 GMT
|
144
148
|
- request:
|
145
149
|
method: post
|
146
150
|
uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
|
147
151
|
body:
|
148
152
|
encoding: US-ASCII
|
149
|
-
string:
|
153
|
+
string: source_lang=EN&target_lang=ES
|
150
154
|
headers:
|
151
155
|
Accept-Encoding:
|
152
156
|
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
@@ -164,22 +168,22 @@ http_interactions:
|
|
164
168
|
Server:
|
165
169
|
- nginx
|
166
170
|
Date:
|
167
|
-
-
|
171
|
+
- Tue, 08 May 2018 16:31:38 GMT
|
168
172
|
Content-Length:
|
169
|
-
- '
|
173
|
+
- '45'
|
170
174
|
Connection:
|
171
175
|
- keep-alive
|
172
176
|
body:
|
173
177
|
encoding: UTF-8
|
174
|
-
string: '{"message":"Parameter ''
|
175
|
-
http_version:
|
176
|
-
recorded_at:
|
178
|
+
string: '{"message":"Parameter ''text'' not specified."}'
|
179
|
+
http_version:
|
180
|
+
recorded_at: Tue, 08 May 2018 16:31:37 GMT
|
177
181
|
- request:
|
178
182
|
method: post
|
179
183
|
uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
|
180
184
|
body:
|
181
185
|
encoding: US-ASCII
|
182
|
-
string: text
|
186
|
+
string: text=Sample+text&source_lang=EN
|
183
187
|
headers:
|
184
188
|
Accept-Encoding:
|
185
189
|
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
@@ -191,25 +195,20 @@ http_interactions:
|
|
191
195
|
- application/x-www-form-urlencoded
|
192
196
|
response:
|
193
197
|
status:
|
194
|
-
code:
|
195
|
-
message:
|
198
|
+
code: 400
|
199
|
+
message: Bad Request
|
196
200
|
headers:
|
197
201
|
Server:
|
198
202
|
- nginx
|
199
203
|
Date:
|
200
|
-
-
|
201
|
-
Content-Type:
|
202
|
-
- application/json
|
204
|
+
- Tue, 08 May 2018 16:31:39 GMT
|
203
205
|
Content-Length:
|
204
|
-
- '
|
206
|
+
- '52'
|
205
207
|
Connection:
|
206
208
|
- keep-alive
|
207
|
-
Access-Control-Allow-Origin:
|
208
|
-
- "*"
|
209
209
|
body:
|
210
210
|
encoding: UTF-8
|
211
|
-
string: '{"
|
212
|
-
|
213
|
-
|
214
|
-
recorded_at: Thu, 04 Jan 2018 18:09:49 GMT
|
211
|
+
string: '{"message":"Parameter ''target_lang'' not specified."}'
|
212
|
+
http_version:
|
213
|
+
recorded_at: Tue, 08 May 2018 16:31:37 GMT
|
215
214
|
recorded_with: VCR 4.0.0
|
@@ -0,0 +1,34 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: https://api.deepl.com/v1/usage?auth_key=VALID_TOKEN
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
Accept-Encoding:
|
11
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
12
|
+
Accept:
|
13
|
+
- "*/*"
|
14
|
+
User-Agent:
|
15
|
+
- Ruby
|
16
|
+
response:
|
17
|
+
status:
|
18
|
+
code: 200
|
19
|
+
message: OK
|
20
|
+
headers:
|
21
|
+
Server:
|
22
|
+
- nginx
|
23
|
+
Date:
|
24
|
+
- Tue, 08 May 2018 20:42:44 GMT
|
25
|
+
Content-Length:
|
26
|
+
- '52'
|
27
|
+
Connection:
|
28
|
+
- keep-alive
|
29
|
+
body:
|
30
|
+
encoding: UTF-8
|
31
|
+
string: '{"character_count":805718,"character_limit":1000000}'
|
32
|
+
http_version:
|
33
|
+
recorded_at: Tue, 08 May 2018 20:42:43 GMT
|
34
|
+
recorded_with: VCR 4.0.0
|
@@ -1,16 +1,60 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe DeepL::Requests::
|
3
|
+
describe DeepL::Requests::Translate do
|
4
4
|
let(:api) { build_deepl_api }
|
5
5
|
let(:text) { 'Sample text' }
|
6
6
|
let(:source_lang) { 'EN' }
|
7
7
|
let(:target_lang) { 'ES' }
|
8
|
-
subject { DeepL::Requests::
|
8
|
+
subject { DeepL::Requests::Translate.new(api, text, source_lang, target_lang) }
|
9
9
|
|
10
10
|
describe '#initialize' do
|
11
11
|
context 'When building a request' do
|
12
12
|
it 'should create a request object' do
|
13
|
-
expect(subject).to be_a(DeepL::Requests::
|
13
|
+
expect(subject).to be_a(DeepL::Requests::Translate)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when using `split_sentences` options' do
|
18
|
+
it 'should convert `true` to `1`' do
|
19
|
+
request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: true)
|
20
|
+
expect(request.options[:split_sentences]).to eq('1')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should convert `false` to `0`' do
|
24
|
+
request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: false)
|
25
|
+
expect(request.options[:split_sentences]).to eq('0')
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should leave `0` as is' do
|
29
|
+
request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: '0')
|
30
|
+
expect(request.options[:split_sentences]).to eq('0')
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should leave `1` as is' do
|
34
|
+
request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: '1')
|
35
|
+
expect(request.options[:split_sentences]).to eq('1')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'when using `preserve_formatting` options' do
|
40
|
+
it 'should convert `true` to `1`' do
|
41
|
+
request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: true)
|
42
|
+
expect(request.options[:preserve_formatting]).to eq('1')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should convert `false` to `0`' do
|
46
|
+
request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: false)
|
47
|
+
expect(request.options[:preserve_formatting]).to eq('0')
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should leave `0` as is' do
|
51
|
+
request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: '0')
|
52
|
+
expect(request.options[:preserve_formatting]).to eq('0')
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should leave `1` as is' do
|
56
|
+
request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: '1')
|
57
|
+
expect(request.options[:preserve_formatting]).to eq('1')
|
14
58
|
end
|
15
59
|
end
|
16
60
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DeepL::Requests::Usage do
|
4
|
+
let(:api) { build_deepl_api }
|
5
|
+
let(:options) { {} }
|
6
|
+
subject { DeepL::Requests::Usage.new(api, options) }
|
7
|
+
|
8
|
+
describe '#initialize' do
|
9
|
+
context 'When building a request' do
|
10
|
+
it 'should create a request object' do
|
11
|
+
expect(subject).to be_a(DeepL::Requests::Usage)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#request' do
|
17
|
+
around do |example|
|
18
|
+
VCR.use_cassette('usage') { example.call }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'When performing a valid request' do
|
22
|
+
it 'should return an usage object' do
|
23
|
+
usage = subject.request
|
24
|
+
|
25
|
+
expect(usage).to be_a(DeepL::Resources::Usage)
|
26
|
+
expect(usage.character_count).to be_a(Numeric)
|
27
|
+
expect(usage.character_limit).to be_a(Numeric)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DeepL::Resources::Usage do
|
4
|
+
subject { DeepL::Resources::Usage.new(3, 5, nil, nil) }
|
5
|
+
|
6
|
+
describe '#initialize' do
|
7
|
+
context 'When building a basic object' do
|
8
|
+
it 'should create a resource' do
|
9
|
+
expect(subject).to be_a(DeepL::Resources::Usage)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should assign the attributes' do
|
13
|
+
expect(subject.character_count).to eq(3)
|
14
|
+
expect(subject.character_limit).to eq(5)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should not exceed the quota' do
|
18
|
+
expect(subject.quota_exceeded?).to be_falsey
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'When building a quota exceeded object' do
|
23
|
+
subject { DeepL::Resources::Usage.new(5, 5, nil, nil) }
|
24
|
+
|
25
|
+
it 'should exceed the quota' do
|
26
|
+
expect(subject.quota_exceeded?).to be_truthy
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
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:
|
4
|
+
version: 2.0.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: 2018-
|
11
|
+
date: 2018-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: juwelier
|
@@ -51,16 +51,22 @@ files:
|
|
51
51
|
- lib/deepl/exceptions/limit_exceeded.rb
|
52
52
|
- lib/deepl/exceptions/request_error.rb
|
53
53
|
- lib/deepl/requests/base.rb
|
54
|
-
- lib/deepl/requests/
|
54
|
+
- lib/deepl/requests/translate.rb
|
55
|
+
- lib/deepl/requests/usage.rb
|
55
56
|
- lib/deepl/resources/base.rb
|
56
57
|
- lib/deepl/resources/text.rb
|
58
|
+
- lib/deepl/resources/usage.rb
|
57
59
|
- spec/api/api_spec.rb
|
58
60
|
- spec/api/configuration_spec.rb
|
59
61
|
- spec/api/deepl_spec.rb
|
60
62
|
- spec/fixtures/vcr_cassettes/deepl_translate.yml
|
63
|
+
- spec/fixtures/vcr_cassettes/deepl_usage.yml
|
61
64
|
- spec/fixtures/vcr_cassettes/translate_texts.yml
|
62
|
-
- spec/
|
65
|
+
- spec/fixtures/vcr_cassettes/usage.yml
|
66
|
+
- spec/requests/translate_spec.rb
|
67
|
+
- spec/requests/usage_spec.rb
|
63
68
|
- spec/resources/text_spec.rb
|
69
|
+
- spec/resources/usage_spec.rb
|
64
70
|
- spec/spec_helper.rb
|
65
71
|
homepage: http://github.com/wikiti/deepl-rb
|
66
72
|
licenses:
|
@@ -82,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
88
|
version: '0'
|
83
89
|
requirements: []
|
84
90
|
rubyforge_project:
|
85
|
-
rubygems_version: 2.6.
|
91
|
+
rubygems_version: 2.6.14
|
86
92
|
signing_key:
|
87
93
|
specification_version: 4
|
88
94
|
summary: A simple ruby wrapper for the DeepL API
|